您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Waits for an element using the MutationObserver API
当前为
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/523012/1516081/WaitForKeyElement.js
- // ==UserScript==
- // @name WaitForKeyElement
- // @namespace Violentmonkey Scripts
- // @version 1.0
- // @description Waits for an element using the MutationObserver API
- // @author PaywallDespiser
- // @grant none
- // ==/UserScript==
- /**
- * Waits for a element of a given selector.
- *
- * @param {string} selector
- * @param {Element} target
- * @returns {Promise<Element>}
- */
- function waitForKeyElement(selector, target = document.body) {
- return new Promise((resolve) => {
- {
- const element = target.querySelector(selector);
- if (element) {
- return resolve(element);
- }
- }
- const observer = new MutationObserver((mutations) => {
- for (const mutation of mutations) {
- for (const node of mutation.addedNodes) {
- if (!(node instanceof HTMLElement)) continue;
- if (node.matches(selector)) {
- observer.disconnect();
- resolve(node);
- return;
- }
- const childElement = node.querySelector(selector);
- if (childElement) {
- observer.disconnect();
- resolve(childElement);
- return;
- }
- }
- }
- });
- observer.observe(target, {
- childList: true,
- subtree: true,
- attributes: false,
- characterData: false,
- });
- });
- }