Commonly used variables and functions
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/428386/943654/Variables%20and%20Functions.js
//jshint esversion: 6
//jshint asi: true
const url = new URL(location.href)
const page = (offset = 0) => url.pathname.split('/')[1 + offset]
const urlObj = url.searchParams
const domain = url.host
const setFname = str => {
console.log(str)
return str
}
const createBtn = (selector, count = 12) => {
$(selector).append(`<button id="open-links" data-count="${count}">OpenLinks (${count})</button>`)
}
const ensureDomLoaded = callback => {
if (['interactive', 'complete'].includes(document.readyState)) {
callback()
return
}
let triggered = false
document.addEventListener('DOMContentLoaded', () => {
if (!triggered) {
triggered = true
setTimeout(callback, 1)
}
})
}
const awaitElement = function (query, callback, time = null, err = null) {
ensureDomLoaded(() => {
let t = setInterval(() => {
const e = $(query)
if (e.length) {
callback(e)
clearInterval(t)
return
}
if (time !== null) {
setTimeout(() => {
clearInterval(t)
err(e)
return
}, time)
}
}, 10)
})
}
const awaitTitleChange = (value, callback) => {
let t = setInterval(() => {
e = document.title
if (e !== value) {
callback(e)
clearInterval(t)
}
}, 10)
}
const keyboardEvent = (callback, key = 'F19') => {
document.addEventListener('keydown', e => {
if (e.key.toLowerCase() === key.toLowerCase()) callback()
})
}
const onFocus = (callback, persistent = false) => {
if (persistent) {
$(window).on('focus', callback)
return
} else if (document.hasFocus()) {
callback()
return
}
$(window).one('focus', callback)
}
const onBlur = (callback, persistent = false) => {
if (persistent) {
$(window).on('blur', callback)
return
} else if (!document.hasFocus()) {
callback()
return
}
$(window).one('blur', callback)
}
const onBlur_closeWindow = (persistent = false) => onBlur(window.close, persistent)
const onFocus_setClipboard = (data, persistent = false) => onFocus(() => setClipboard(data), persistent)
const setClipboard = data => GM_setClipboard(data)
const defaultCase = () => console.log(domain)