Observe

Observe and wait for elements

目前為 2025-10-11 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/552301/1675893/Observe.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Observe
// @license      MIT
// @version      1.2
// @description  Observe and wait for elements
// @author       tukars
// @match        *://*/*
// ==/UserScript==

(function () {
  // Define the namespace structure
  const NAMESPACE_ROOT = "userscript";
  const NAMESPACE_DOMAIN = "com";
  const NAMESPACE_AUTHOR = "tukars";
  const NAMESPACE_LIB_NAME = "Observe";

  window[NAMESPACE_ROOT] = window[NAMESPACE_ROOT] || {};
  window[NAMESPACE_ROOT][NAMESPACE_DOMAIN] =
    window[NAMESPACE_ROOT][NAMESPACE_DOMAIN] || {};
  window[NAMESPACE_ROOT][NAMESPACE_DOMAIN][NAMESPACE_AUTHOR] =
    window[NAMESPACE_ROOT][NAMESPACE_DOMAIN][NAMESPACE_AUTHOR] || {};

  if (
    window[NAMESPACE_ROOT][NAMESPACE_DOMAIN][NAMESPACE_AUTHOR][
      NAMESPACE_LIB_NAME
    ]
  ) {
    return;
  }

  const timeStamp = function () {
    const now = new Date();
    const pad = (num, size) => String(num).padStart(size, "0");

    const hours = pad(now.getHours(), 2);
    const minutes = pad(now.getMinutes(), 2);
    const seconds = pad(now.getSeconds(), 2);
    const milliseconds = pad(now.getMilliseconds(), 3);

    return `${hours}:${minutes}:${seconds}.${milliseconds}`;
  };

  const contextPrint = function (message, ENABLE_DEBUG_LOGGING) {
    const messageTime = () => `[${message}] ${timeStamp()} -`;

    const log = function (...args) {
      console.log(messageTime(), ...args);
    };
    const warn = function (...args) {
      console.warn(messageTime(), ...args);
    };
    const error = function (...args) {
      console.error(messageTime(), ...args);
    };
    const info = function (...args) {
      console.info(messageTime(), ...args);
    };
    var debug = function (...args) {
      console.debug(messageTime(), ...args);
    };
    if (!ENABLE_DEBUG_LOGGING) {
      debug = function () {};
    }

    return { log, warn, error, info, debug };
  };

  const observeChildren = function (element, config, callback) {
    const observer = new MutationObserver((mutationsList) => {
      for (const mutation of mutationsList) {
        if (mutation.type === "childList") {
          callback(Array.from(mutation.addedNodes));
        }
      }
    });
    const use_config = config || { childList: true, subtree: false };
    observer.observe(element, use_config);
    return observer;
  };

  const observeChildrenWithFilter = function (element, config, filter, callback) {
    return observeChildren(element, config, (addedNodes) =>
      callback(addedNodes.filter(filter))
    );
  };

  const observeChildrenWithTags = function (element, config, filterTags, callback) {
    return observeChildrenWithFilter(
      element,
      config,
      (node) =>
        node.nodeType === Node.ELEMENT_NODE &&
        filterTags.includes(node.tagName.toLowerCase()),
      callback
    );
  };

  const observeAndHandle = function (element, tags, fun, config) {
    const use_config = config || { childList: true, subtree: true };
    observeChildrenWithTags(element, use_config, tags, (nodes) =>
      nodes.forEach((node) => fun(node))
    );
  };

  function waitForElement(selector, callback) {
    const element = document.querySelector(selector);
    if (element) {
      callback(element);
    } else {
      const observer = new MutationObserver((_, obs) => {
        const el = document.querySelector(selector);
        if (el) {
          obs.disconnect();
          callback(el);
        }
      });
      observer.observe(document.body, { childList: true, subtree: true });
    }
  }

  window[NAMESPACE_ROOT][NAMESPACE_DOMAIN][NAMESPACE_AUTHOR][NAMESPACE_LIB_NAME] =
    {
      timeStamp,
      contextPrint,
      observeChildren,
      observeChildrenWithFilter,
      observeChildrenWithTags,
      observeAndHandle,
      waitForElement,
    };
})();