TetteLib

A library containing several functions i use often in my other scripts

目前為 2023-10-27 提交的版本,檢視 最新版本

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

// ==UserScript==
// @name         TetteLib
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  A library containing several functions i use often in my other scripts
// @author       TetteDev
// @match        *://*/*
// @grant        none
// ==/UserScript==

function waitForElement(selector) {
  return new Promise((resolve, reject) => {
    const el = document.querySelector(selector);
    if (el) {resolve(el);}
    new MutationObserver((mutationRecords, observer) => {
      // Query for elements matching the specified selector
      Array.from(document.querySelectorAll(selector)).forEach((element) => {
        resolve(element);
        //Once we have resolved we don't need the observer anymore.
        observer.disconnect();
      });
    })
      .observe(document.documentElement, {
        childList: true,
        subtree: true
      });
  });
}
function traverseParentsUntil(startElement, predicateUntil) {
	if (!startElement) return null;
	if (!predicateUntil || typeof predicateUntil !== "function") return null;
	if (!startElement.parentElement) return predicateUntil(startElement) ? startElement : null;

	while (startElement.parentElement) {
		if (predicateUntil(startElement.parentElement)) return startElement.parentElement;
		else startElement = startElement.parentElement;
	}
	return null;
}
function traverseChildrenUntil(startElement, predicateUntil) {
	if (!startElement) return null;
	if (!predicateUntil || typeof predicateUntil !== "function") return null;
	if (!startElement.firstChild) return predicateUntil(startElement) ? startElement : null;

	while (startElement.firstChild) {
		if (predicateUntil(startElement.firstChild)) return startElement.firstChild;
		else startElement = startElement.firstChild;
	}
	return null;
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址