A library containing several functions i use often in my other scripts
当前为
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @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;
}