Waits for elements
Version vom
Dieses Skript sollte nicht direkt installiert werden. Es handelt sich hier um eine Bibliothek für andere Skripte, welche über folgenden Befehl in den Metadaten eines Skriptes eingebunden wird // @require https://update.greasyfork.org/scripts/432418/974314/Wait%20For%20Selector.js
// ==UserScript==
// @name Wait For Selector
// @namespace http://tampermonkey.net/
// @version 0.1.0
// @description Waits for elements
// @author Kumirei
// @grant none
// ==/UserScript==
(function($, wfs) {
// Create new observer on body to monitor all DOM changes
let observer = new MutationObserver(mutationHandler)
observer.observe(document.getElementsByTagName('body')[0], {childList: true, subtree: true})
// Interface for interacting with the library
let interface = {
version: GM_info.script.version,
observer: observer,
wait: waitForSelector,
unwait: unwaitID,
waits: {},
waitsByID: {},
nextID: 0
}
// Start
installInterface()
// Creates a new entry to search for whenever a new element is added to the DOM
function waitForSelector(selector, callback, ignoreExisting=false) {
if (!interface.waits[selector]) interface.waits[selector] = {}
interface.waits[selector][interface.nextID] = callback
interface.waitsByID[interface.nextID] = selector
return interface.nextID++
}
// Deletes a previously registered selector
function unwaitID(ID) {
delete interface.waits[interface.waitsByID[ID]][ID]
delete interface.waitsByID[ID]
}
// Makes sure that the public interface is the newest version and the same as the local one
function installInterface() {
if (!wfs) window.wfs = interface
else if (wfs.version < interface.version) {
wfs.version = interface.version
wfs.observer.disconnect()
wfs.observer = interface.observer
wfs.wait = interface.wait
wfs.unwait = interface.unwait
}
interface = wfs || interface
}
// Waits until there has been more than 300 ms between mutations and then checks for new elements
let lastMutationDate = 0 // Epoch of last mutation event
function mutationHandler(mutations) {
let duration = Date.now() - lastMutationDate
lastMutationDate = Date.now()
if (duration > 300) {
for (let selector in interface.waits) {
$(selector).each((i, e)=>{
let callbacks = Object.values(interface.waits[selector])
if (!e.WFSFound || e.WFSFound == lastMutationDate) {
for (let callback of callbacks) callback(e)
e.WFSFound = lastMutationDate
}
})
}
}
}
})(window.jQuery, window.wfs);