waitForElement

Waits for an element using the MutationObserver API

لا ينبغي أن لا يتم تثبيت هذا السكريت مباشرة. هو مكتبة لسكبتات لتشمل مع التوجيه الفوقية // @require https://update.greasyfork.org/scripts/528234/1701531/waitForElement.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

// ==UserScript==
// @name         waitForElement
// @namespace    Violentmonkey Scripts
// @version      3.0
// @description  Waits for an element using the MutationObserver API
// @author       maanimis
// @grant        none
// ==/UserScript==

function waitForElement(selector, timeout = 5000) {
  return new Promise((resolve) => {
    const ELEMENT = document.querySelector(selector);
    if (ELEMENT) {
      return resolve(ELEMENT);
    }
    console.log("[not found] selector: %s\nwaiting...", selector);
    const observer = new MutationObserver(() => {
      const ELEMENT = document.querySelector(selector);
      if (ELEMENT) {
        console.log("element found!!");
        resolve(ELEMENT);
        observer.disconnect();
      }
    });
    if (timeout && timeout >= 0) {
      setTimeout(() => {
        console.log("timeout reached, element not found: %s", selector);
        resolve(null); // Resolve with null if timeout is reached
        observer.disconnect(); // Disconnect the observer if the timeout occurs
      }, timeout);
    }
    observer.observe(document.body, {
      childList: true,
      subtree: true,
    });
  });
}