UserUtils

General purpose DOM/GreaseMonkey library: register listeners for when CSS selectors exist, intercept events, create persistent & synchronous data stores, modify the DOM more easily and much more

As of 2023-08-12. See the latest version.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/472956/1234454/UserUtils.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

var UserUtils = (function (exports) {
	'use strict';

	var h=Object.defineProperty,y=Object.defineProperties;var g=Object.getOwnPropertyDescriptors;var p=Object.getOwnPropertySymbols;var w=Object.prototype.hasOwnProperty,v=Object.prototype.propertyIsEnumerable;var f=(n,e,t)=>e in n?h(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,m=(n,e)=>{for(var t in e||(e={}))w.call(e,t)&&f(n,t,e[t]);if(p)for(var t of p(e))v.call(e,t)&&f(n,t,e[t]);return n},b=(n,e)=>y(n,g(e));var x=(n,e,t)=>new Promise((r,o)=>{var i=s=>{try{a(t.next(s));}catch(l){o(l);}},u=s=>{try{a(t.throw(s));}catch(l){o(l);}},a=s=>s.done?r(s.value):Promise.resolve(s.value).then(i,u);a((t=t.apply(n,e)).next());});function A(n,e,t){return Math.max(Math.min(n,t),e)}function N(n,e,t,r,o){return Number(e)===0&&Number(r)===0?n*(o/t):(n-e)*((o-r)/(t-e))+r}function d(...n){let e,t;if(typeof n[0]=="number"&&typeof n[1]=="number")[e,t]=n;else if(typeof n[0]=="number"&&typeof n[1]!="number")e=0,t=n[0];else throw new TypeError(`Wrong parameter(s) provided - expected: "number" and "number|undefined", got: "${typeof n[0]}" and "${typeof n[1]}"`);if(e=Number(e),t=Number(t),isNaN(e)||isNaN(t))throw new TypeError(`Parameters "min" and "max" can't be NaN`);if(e>t)throw new TypeError(`Parameter "min" can't be bigger than "max"`);return Math.floor(Math.random()*(t-e+1))+e}function H(n){return T(n)[0]}function T(n){if(n.length===0)return [void 0,void 0];let e=d(n.length-1);return [n[e],e]}function I(n){let[e,t]=T(n);if(t!==void 0)return n.splice(t,1),e}function C(n){let e=[...n];if(n.length===0)return n;for(let t=e.length-1;t>0;t--){let r=Math.floor(d(0,1e4)/1e4*(t+1));[e[t],e[r]]=[e[r],e[t]];}return e}function L(){try{return unsafeWindow}catch(n){return window}}function j(n,e){var t;return (t=n.parentNode)==null||t.insertBefore(e,n.nextSibling),e}function R(n,e){let t=n.parentNode;if(!t)throw new Error("Element doesn't have a parent node");return t.replaceChild(e,n),e.appendChild(n),e}function F(n){let e=document.createElement("style");e.innerHTML=n,document.head.appendChild(e);}function W(n,e=!1){let t=n.map(r=>new Promise((o,i)=>{let u=new Image;u.src=r,u.addEventListener("load",()=>o(u)),u.addEventListener("error",a=>e&&i(a));}));return Promise.allSettled(t)}function $(n){let e=document.createElement("a");Object.assign(e,{className:"userutils-open-in-new-tab",target:"_blank",rel:"noopener noreferrer",href:n}),e.style.display="none",document.body.appendChild(e),e.click(),setTimeout(e.remove,50);}function M(n,e,t){typeof Error.stackTraceLimit=="number"&&Error.stackTraceLimit<1e3&&(Error.stackTraceLimit=1e3),function(r){element.__proto__.addEventListener=function(...o){if(!(o[0]===e&&t()))return r.apply(this,o)};}(n.__proto__.addEventListener);}function q(n,e){return M(L(),n,e)}function G(n,e=1){let t=new(window.AudioContext||window.webkitAudioContext),r={mediaElement:n,amplify:o=>{r.gain.gain.value=o;},getAmpLevel:()=>r.gain.gain.value,context:t,source:t.createMediaElementSource(n),gain:t.createGain()};return r.source.connect(r.gain),r.gain.connect(t.destination),r.amplify(e),r}function B(n,e){return (Array.isArray(e)||e instanceof NodeList)&&(e=e.length),`${n}${e===1?"":"s"}`}function U(n){return new Promise(e=>{setTimeout(e,n);})}function D(n,e=300){let t;return function(...r){clearTimeout(t),t=setTimeout(()=>n.apply(this,r),e);}}function J(t){return x(this,arguments,function*(n,e={}){let{timeout:r=1e4}=e,o=new AbortController,i=setTimeout(()=>o.abort(),r),u=yield fetch(n,b(m({},e),{signal:o.signal}));return clearTimeout(i),u})}var c=new Map;function V(n,e){let t=[];c.has(n)&&(t=c.get(n)),t.push(e),c.set(n,t),E(n,t);}function X(n){return c.delete(n)}function E(n,e){let t=[];if(e.forEach((r,o)=>{try{let i=r.all?document.querySelectorAll(n):document.querySelector(n);(i!==null&&i instanceof NodeList&&i.length>0||i!==null)&&(r.listener(i),r.continuous||t.push(o));}catch(i){console.error(`Couldn't call listener for selector '${n}'`,i);}}),t.length>0){let r=e.filter((o,i)=>!t.includes(i));r.length===0?c.delete(n):c.set(n,r);}}function Y(n={}){new MutationObserver(()=>{for(let[t,r]of c.entries())E(t,r);}).observe(document.body,m({subtree:!0,childList:!0},n));}function Z(){return c}

	exports.addGlobalStyle = F;
	exports.addParent = R;
	exports.amplifyMedia = G;
	exports.autoPlural = B;
	exports.clamp = A;
	exports.debounce = D;
	exports.fetchAdvanced = J;
	exports.getSelectorMap = Z;
	exports.getUnsafeWindow = L;
	exports.initOnSelector = Y;
	exports.insertAfter = j;
	exports.interceptEvent = M;
	exports.interceptWindowEvent = q;
	exports.mapRange = N;
	exports.onSelector = V;
	exports.openInNewTab = $;
	exports.pauseFor = U;
	exports.preloadImages = W;
	exports.randRange = d;
	exports.randomItem = H;
	exports.randomItemIndex = T;
	exports.randomizeArray = C;
	exports.removeOnSelector = X;
	exports.takeRandomItem = I;

	return exports;

})({});