Variables and Functions

Commonly used variables and functions

Stan na 24-06-2021. Zobacz najnowsza wersja.

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://update.greasyfork.org/scripts/428386/943654/Variables%20and%20Functions.js

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

//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)