Library for simplifying code logic and syntax (Lite)
Tätä skriptiä ei tulisi asentaa suoraan. Se on kirjasto muita skriptejä varten sisällytettäväksi metadirektiivillä // @require https://update.greasyfork.org/scripts/487608/1878573/SyntaxLite_min.js.
// ==UserScript==
// @name SyntaxLite
// @version 2026.07.18
// @author Canaan HS
// @description Library for simplifying code logic and syntax (Lite)
// @namespace https://greasyfork.org/users/989635
// @match *://*/*
// @license MPL-2.0
// ==/UserScript==
const Lib=(()=>{function y(a=document,b,c){if(1!==a?.nodeType&&9!==a?.nodeType)return c?[]:void 0;const e=b[0],d=b.slice(1);if(/[ .#:[\]>+~*,()^$=]/.test(d))return c?a.querySelectorAll(b):a.querySelector(b);if(!c&&"#"===e)return 9===a.nodeType?a.getElementById(d):a.querySelector(b);if("."===b[0])return a=a.getElementsByClassName(d),c?[...a]:a[0];a=a.getElementsByTagName(b);return c?[...a]:a[0]}async function V(a,b,c=crypto.randomUUID(),e=!0){let d=B.get(c);if(e||!d)d||(d=document.createElement(a),
d.id=c,(a=K.head)?a.appendChild(d):W("head").then(g=>{g.appendChild(d)})),d.textContent+=b,B.set(c,d)}async function W(a,b,c={}){const e=Array.isArray(a)?X.queryMap:X.queryElement,{raf:d=!1,all:g=!1,timeout:h=8,throttle:p=0,debounce:r=50,visibility:f=!0,subtree:k=!0,childList:m=!0,attributes:q=!0,characterData:n=!1,timeoutResult:l=!1,root:w=document}=c||{};return new Promise((t,u)=>{const v=async function(){let C,x;if(d){let D;const I=()=>{(x=e(a,g))?(cancelAnimationFrame(D),clearTimeout(C),b?.(x),
t(x)):D=requestAnimationFrame(I)};D=requestAnimationFrame(I);C=setTimeout(()=>{cancelAnimationFrame(D);l&&(b?.(x),t(x))},1E3*h)}else{const [D,I]=0<p?[L,p]:[M,r],N=new MutationObserver(D(()=>{if(x=e(a,g))N.disconnect(),clearTimeout(C),b?.(x),t(x)},I));N.observe(w,{subtree:k,childList:m,attributes:q,characterData:n});C=setTimeout(()=>{N.disconnect();l&&(b?.(x),t(x))},1E3*h)}};f&&"hidden"===document.visibilityState?document.addEventListener("visibilitychange",()=>v(),{once:!0}):v()})}function Y(a){if(!a?.trim())return 0;
if(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(a)){var b=Date.parse(a);if(!isNaN(b))return Math.floor(b/1E3)}let c=!1;b={years:0,months:0,days:0,hours:0,minutes:0,seconds:0};const e={y:"years",d:"days",h:"hours",s:"seconds"};for(var d of a.matchAll(/(\d+)\s*([YyMDdhms])/g)){c=!0;a=parseInt(d[1],10);const g=d[2];"M"===g?b.months+=a:"m"===g?b.minutes+=a:b[e[g.toLowerCase()]]+=a}if(!c)return 0;d=new Date;d.setFullYear(d.getFullYear()+b.years);d.setMonth(d.getMonth()+b.months);d.setDate(d.getDate()+
b.days);d.setHours(d.getHours()+b.hours);d.setMinutes(d.getMinutes()+b.minutes);d.setSeconds(d.getSeconds()+b.seconds);return Math.floor(d.getTime()/1E3)}function Z(a,b,c,{space:e=0,expireStr:d}={}){const g=E(c);c={type:g,data:O[g]?.(c)??c};(d=Y(d))&&(c.expire=d);a(b,JSON.stringify(c,null,e))}function aa(a,b,c,{autoRemove:e=!1,remover:d}={}){a=a(b);if(null==a)return c;try{a=JSON.parse(a);const g=a instanceof Object;if(g&&a.expire&&Date.now()>1E3*a.expire)return d(b),c;let h,p;g?(h=a.type??"Object",
p=a.data??a):(h=E(a),p=a);const r=ba[h]?.(p)??p;e&&d(b);return r}catch{return"object"===typeof a&&K.isEmpty(a)||"string"===typeof a&&a.startsWith("[object")?c:a}}function L(a,b){let c=0;return(...e)=>{const d=Date.now();d-c>=b&&(c=d,a(...e))}}function M(a,b){let c;return(...e)=>{clearTimeout(c);c=setTimeout(function(){a(...e)},b)}}function ca(a){a=new Blob([a],{type:"application/javascript"});const b=URL.createObjectURL(a),c=new Worker(b),e=c.terminate;c.terminate=function(){e.call(c);URL.revokeObjectURL(b)};
return c}function P(a,b="https://cdnjs.cloudflare.com/ajax/libs/pako/2.1.0/pako.min.js"){function c(f){return new Promise((k,m)=>{const q=new Blob([f],{type:"application/octet-stream"}),n=new FileReader;n.onload=function(l){l=l.target.result;l=l.substring(l.indexOf(",")+1);k(l)};n.onerror=function(l){m(l)};n.readAsDataURL(q)})}function e(f,k,m){return new Promise(q=>{const n=p++;r.set(n,q);d.postMessage({type:f,data:k,level:m,requestId:n},[k.buffer])})}let d=ca(`${a||`importScripts('${b}')`}onmessage = function(e) {const { type, data, level, requestId } = e.data;const bytes = type === "compress" ? pako.deflate(data, { level }) : pako.inflate(data);postMessage({ data: bytes, requestId }, [bytes.buffer]);}`);
const g=new TextEncoder,h=new TextDecoder;let p=0;const r=new Map;d.onmessage=f=>{const {requestId:k,data:m}=f.data;r.has(k)&&(r.get(k)(m),r.delete(k))};return{async destroyWorker(){d&&(d.terminate(),d=null)},async compress(f,{level:k=5,base64:m=!0,stringify:q=!0}={}){if(null==f)return f;f=await e("compress",g.encode(q?JSON.stringify(f):f),k);return m?await c(f):f},async decompress(f,k=!0){if(null==f)return f;if(!(f instanceof Uint8Array)){f=atob(f);const m=f.length,q=new Uint8Array(m);
for(let n=0;n<m;n++)q[n]=f.charCodeAt(n);f=q}f=await e("decompress",f);f=h.decode(f);return k?JSON.parse(f):f}}}function da(a){if(null==a){for(const b of H)GM_unregisterMenuCommand(b);H.clear()}else Array.isArray(a)?a.forEach(b=>{H.delete(b)&&GM_unregisterMenuCommand(b)}):H.delete(a)&&GM_unregisterMenuCommand(a)}const ha=new DOMParser,E=a=>Object.prototype.toString.call(a).slice(8,-1);[Document.prototype,Element.prototype].forEach(a=>{a.$q=function(b){return y(this,b,!1)};a.$qa=function(b){return y(this,
b,!0)}});let z=null;const Q=()=>window.trustedTypes?.getPolicy?.("default")??window.trustedTypes?.createPolicy?.(Symbol("myPolicy").toString(),{createHTML:a=>a}),R={$text(a){return null==a?this?.textContent?.trim():this.textContent=a?.trim()??""},$copy(a=!0){return this.cloneNode(a)},$iHtml(a){return null==a?this.innerHTML:this.innerHTML=a},$safeiHtml(a){if(null==a)return this.innerHTML;try{z??=Q(),z?this.appendChild(document.createRange().createContextualFragment(z.createHTML(a))):this.innerHTML=
a}catch(b){console.error(b)}},$oHtml(a){return null==a?this.outerHTML:this.outerHTML=a},$iAdjacent(a,b="beforeend"){null!=a&&(1===a.nodeType?this.insertAdjacentElement(b,a):this.insertAdjacentHTML(b,a))},$safeiAdjacent(a,b="beforeend"){if(null!=a){if(1===a.nodeType)return this.insertAdjacentElement(b,a);try{z??=Q(),z?this.insertAdjacentHTML(b,z.createHTML(a)):this.insertAdjacentHTML(b,a)}catch(c){console.error(c)}}},$safeiBefore(a,b="beforeend"){if(null!=a)try{z??=Q();if(!z)throw Error("Policy not found");
const c=document.createRange().createContextualFragment(z.createHTML(a));switch(b){case "beforebegin":this.parentNode?.insertBefore(c,this);break;case "afterbegin":this.insertBefore(c,this.firstChild);break;case "beforeend":this.appendChild(c);break;case "afterend":this.parentNode?.insertBefore(c,this.nextSibling)}}catch(c){console.error(c)}},$sAttr(a,b){this.setAttribute(a,b)},$dAttr(a){this.removeAttribute(a)},$gAttr(a){return this.getAttribute(a)},$hAttr(a){return this.hasAttribute(a)},$addClass(...a){this.classList.add(...a)},
$delClass(...a){this.classList.remove(...a)},$toggleClass(a,b){this.classList.toggle(a,b)},$replaceClass(a,b){this.classList.replace(a,b)},$hasClass(a){return this.classList.contains(a)}};Object.assign(Node.prototype,R);const ea=Object.keys(R)[0];Object.defineProperty(Object.prototype,ea,{value:function(a=null){return R[ea].call(this,a)},writable:!0,configurable:!0});const K={$q:document.$q.bind(document),$Q:(a,b)=>y(a,b,!1),$qa:document.$qa.bind(document),$Qa:(a,b)=>y(a,b,!0),html:document.documentElement,
get head(){const a=document.head;a&&Object.defineProperty(this,"head",{value:a,writable:!1});return a},get body(){const a=document.body;a&&Object.defineProperty(this,"body",{value:a,writable:!1});return a},img:document.images,link:document.links,script:document.scripts,style:document.styleSheets,$url:location.href,get url(){return location.href},$origin:location.origin,get origin(){return location.origin},$domain:location.hostname,get domain(){return location.hostname},$lang:navigator.language,get lang(){return navigator.language},
$agen:navigator.userAgent,get agen(){return navigator.userAgent},isEmpty(a){for(const b in a)return!1;return!0},createDomFragment:a=>document.createRange().createContextualFragment(a),get createFragment(){return document.createDocumentFragment()},title:a=>null==a?document.title:document.title=a,cookie:a=>null==a?document.cookie:document.cookie=a,createUrl:a=>URL.createObjectURL(a),_on:(a,b)=>{const c={};for(const e of Object.keys(b)){let d=b[e];d&&("function"===typeof d&&(d={listen:d}),d.listen&&
(a.addEventListener(e,d.listen,d.add),c[e]=()=>a.removeEventListener(e,d.listen,d.add)))}return c},createElement(a,b,c,e){const [d,g,h={},p="beforeend"]="string"===typeof a?[void 0,a,b,c]:[a,b,c,e];if(g){var {class:r,text:f,html:k,rows:m,cols:q,on:n={},style:l={},attr:w={},...t}=h;a=document.createElement(g);r&&(a.className=r);f&&(a.textContent=f);k&&a.$safeiHtml(k);null!=m&&(a.rowSpan=m);null!=q&&(a.colSpan=q);Object.assign(a,t);"string"===typeof l?a.style.cssText=l:Object.assign(a.style,l);for(const v in w)a.setAttribute(v,
w[v]);var u="object"!==typeof n||this.isEmpty(n)?{}:this._on(a,n);Object.assign(a,{on(v,C,x){this.addEventListener(v,C,x);Object.assign(u,{[v]:()=>this.removeEventListener(v,C,x)})},off(v){u[v]?.();delete u[v]},offAll(){Object.keys(u).forEach(v=>{u[v]();delete u[v]})}});return 1===d?.nodeType?d.insertAdjacentElement(p,a):a}}},A=new Map,S={log:(...a)=>console.log(...a),warn:(...a)=>console.warn(...a),table:(...a)=>console.table(...a),trace:(...a)=>console.trace(...a),debug:(...a)=>console.debug(...a),
error:(...a)=>console.error(...a),count:a=>console.count(a)},B=new Map,T=new Map,X={queryMap:(a,b)=>{a=a.map(c=>y(document,c,b));return b?a.every(c=>0<c.length)&&a:a.every(Boolean)&&a},queryElement:(a,b)=>{a=y(document,a,b);return(b?0<a.length:a)&&a}},ba={Set:a=>new Set(a),Map:a=>new Map(a),Date:a=>new Date(a)},O={Set:a=>a instanceof Set?[...a]:a,Map:a=>a instanceof Map?[...a]:a,Date:a=>a instanceof Date?a.toISOString():a};var J=(a,b)=>({[`del${a}`]:b.del,[`set${a}`]:(c,e,d)=>Z(b.set,c,e,{expireStr:d}),
[`get${a}`]:(c,e,d)=>aa(b.get,c,e,{autoRemove:d,remover:b.del})});const fa=a=>({del:a.removeItem.bind(a),set:a.setItem.bind(a),get:a.getItem.bind(a)});J={...J("Local",fa(localStorage)),...J("Session",fa(sessionStorage))};let F;const U={ko:"Korea","ko-KR":"Korea",ja:"Japan","ja-JP":"Japan",ru:"Russia","ru-RU":"Russia",en:"English","en-US":"English","en-GB":"English","en-AU":"English","en-CA":"English","en-NZ":"English","en-IE":"English","en-ZA":"English","en-IN":"English",zh:"Simplified","zh-CN":"Simplified",
"zh-SG":"Simplified","zh-MY":"Simplified","zh-TW":"Traditional","zh-HK":"Traditional","zh-MO":"Traditional"},H=new Set,G=new Map;return function(...a){const b={};for(const c of a)c&&Object.defineProperties(b,Object.getOwnPropertyDescriptors(c));return b}({get sX(){return window.scrollX},get sY(){return window.scrollY},get iW(){return window.innerWidth},get iH(){return window.innerHeight},get platform(){const a={desktop:!1,mobile:!1};window.matchMedia?.("(pointer: coarse)")?.matches||window.matchMedia?.("(hover: none)")?.matches?
a.mobile=!0:/Android|iPhone|iPad|iPod|webOS|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)?a.mobile=!0:a.desktop=!0;Object.defineProperty(this,"platform",{value:a,writable:!1});return a}},K,{addStyle:(a,b,c)=>V("style",a,b,c),addScript:(a,b,c)=>V("script",a,b,c),...J,delV:a=>GM_deleteValue(a),allV:()=>GM_listValues()??void 0,setV:(a,b)=>GM_setValue(a,b),getV:(a,b)=>GM_getValue(a,b)??void 0,setJV:(a,b,{space:c,expireStr:e}={})=>Z(GM_setValue,a,b,{space:c,expireStr:e}),getJV:(a,b,c)=>aa(GM_getValue,
a,b,{autoRemove:c,remover:GM_deleteValue}),eventRecord:A,addRecord:B,observerRecord:T,type:E,onE:async function(a,b,c,e={},d){try{"string"===typeof a&&(a=y(document,a));let g=b.split(/\s*[,|/]\s*/).filter(Boolean);if(0===g.length)throw Error("No event types provided");if(1===g.length)a.addEventListener(b,c,e);else{let h;const p=r=>{clearTimeout(h);h=setTimeout(()=>c(r),15)};g.forEach(r=>a.addEventListener(r,p,e))}d?.(!0)}catch{d?.(!1)}},onEvent:async function(a,b,c,e={}){const {mark:d,...g}=e;"string"===
typeof a&&(a=y(document,a));e=d??a;const h=A.get(e);h?.has(b)||(a.addEventListener(b,c,g),h||A.set(e,new Map),A.get(e).set(b,{element:a,type:b,listener:c}))},offEvent:async function(a,b,c){if(null==a&&null==b&&null==c){for(const e of A.values())for(const {element:d,type:g,listener:h}of e.values())d.removeEventListener(g,h);A.clear()}else if(a=c??("string"===typeof a?y(document,a):a),c=A.get(a))if(b=c.get(b)){const {element:e,type:d,listener:g}=b;e.removeEventListener(d,g);A.get(a).delete(d)}else{for(const {element:e,
type:d,listener:g}of c.values())e.removeEventListener(d,g);A.delete(a)}},onUrlChange:function(a,b=15){function c(k){clearTimeout(d);h||"urlchange"!==k||(h=!0);d=setTimeout(()=>{h&&e(!1,!0);a({type:k,url:location.href,domain:location.hostname})},Math.max(15,b))}function e(k=!0,m=!1){m&&g||(clearTimeout(d),history.pushState=p,history.replaceState=r,window.removeEventListener("popstate",f.popstate),window.removeEventListener("hashchange",f.hashchange),k&&window.removeEventListener("urlchange",f.urlchange),
g=!0)}let d,g=!1,h=!1;const p=history.pushState,r=history.replaceState,f={urlchange:()=>c("urlchange"),popstate:()=>c("popstate"),hashchange:()=>c("hashchange")};window.addEventListener("urlchange",f.urlchange);window.addEventListener("popstate",f.popstate);window.addEventListener("hashchange",f.hashchange);history.pushState=function(){p.apply(this,arguments);c("pushState")};history.replaceState=function(){r.apply(this,arguments);c("replacestate")};return{off:e}},log:function(...a){if(0!==a.length){var b=
{},c=a,e={dev:!0,group:null,collapsed:!0};if(1<a.length){const f=a[0],k=a.at(-1),m=f?.constructor===Object,q=k?.constructor===Object;if(q&&!m)b=k,c=a.slice(0,-1);else if(m&&!q)b=f,c=a.slice(1);else if(m&&q){const n=new Set(Object.keys(e));b=Object.keys(f).filter(l=>n.has(l)).length;Object.keys(k).filter(l=>n.has(l)).length>b?(b=k,c=a.slice(0,-1)):(b=f,c=a.slice(1))}}var {dev:d,group:g,collapsed:h}={...e,...b};if(!d||0===c.length)return new Proxy({},{get(){}});var p=f=>{"count"===f?S.count(c[0]):(f=
S[f]||S.log,g?(h?console.groupCollapsed(g):console.group(g),f(...c),console.groupEnd()):f(...c))},r=setTimeout(()=>p("log",c));return new Proxy({},{get(f,k){clearTimeout(r);p(k,c)}})}},delHead:async function(a){if(null==a){for(var b of B.values())b.remove();B.clear()}else if(b=B.get(a))b.remove(),B.delete(a)},observer:async function(a,b,c={},e){const {mark:d="",debounce:g=0,throttle:h=100,subtree:p=!0,childList:r=!0,attributes:f=!0,characterData:k=!1}=c||{};if(d&&(c=T.get(d))){if(c.target===a)return;
c.ob.disconnect()}const [m,q]=0<g?[M,g]:[L,h];c={subtree:p,childList:r,attributes:f,characterData:k};const n=new MutationObserver(m((l,w)=>{b(l,w)},q));n.observe(a,c);d&&T.set(d,{target:a,ob:n});e?.({ob:n,op:c})},waitEl:W,openDB:async function(a="StorageDB",b=1,c){return new Promise((e,d)=>{const g=indexedDB.open(a,b),h=a.replace(/db$/i,"").toLowerCase();g.onupgradeneeded=p=>{p=p.target.result;p.objectStoreNames.contains(h)||p.createObjectStore(h)};g.onsuccess=p=>{async function r(k){return new Promise((m,
q)=>{const n=f.transaction(h,"readwrite").objectStore(h).delete(k);n.onsuccess=()=>m(n.result);n.onerror=()=>q(n.error)})}const f=p.target.result;e({set:async function(k,m,{compress:q=!0,expireStr:n}={}){const l={};q?(F??=P(c),q=E(m),l.type=q,l.data=await F.compress(O[q]?.(m)??m,{base64:!1}),l.compressed=!0):l.data=m;if(m=Y(n))l.expire=m;return new Promise((w,t)=>{const u=f.transaction(h,"readwrite").objectStore(h).put(l,k);u.onsuccess=()=>w(u.result);u.onerror=()=>t(u.error)})},get:async function(k,
m,q=!1){const n=f.transaction(h,"readonly").objectStore(h),l=await new Promise((w,t)=>{const u=n.get(k);u.onsuccess=()=>w(u.result);u.onerror=()=>t(m)});if(null==l)return m;try{const w=l instanceof Object;if(w&&l.expire&&Date.now()>1E3*l.expire)return r(k),m;let t;if(w){if(t=l.data??l,l.compressed){F??=P(c);t=await F.decompress(t);const u=l.type||E(t);t=ba[u]?.(t)??t}}else t=l;q&&r(k);return t}catch{return l}},del:r,destroy:function(){f&&(f.close(),indexedDB.deleteDatabase(a),F.destroyWorker(),setTimeout(()=>
F=null,100))}})};g.onerror=()=>d(g.error)})},throttle:L,debounce:M,createWorker:ca,createStrCompress:P,outputJson:async function(a,b,c){try{a="string"===typeof a?a:JSON.stringify(a,null,4);b="string"===typeof b?b.endsWith(".json")?b:`${b}.json`:`Untitled-${crypto.randomUUID().slice(9,23)}.json`;const e=new Blob([a],{type:"application/json"}),d=document.createElement("a");d.href=URL.createObjectURL(e);d.download=b;d.click();URL.revokeObjectURL(d.href);d.remove();c?.({State:!0})}catch(e){c?.({State:!1,
Info:e})}},runTime:function(a,{label:b="Elapsed Time:",log:c=!0,format:e=!0,style:d="\u001b[1m\u001b[36m%s\u001b[0m"}={}){if(!a)return performance.now();a=e?`${((performance.now()-a)/1E3).toPrecision(3)}s`:performance.now()-a;return c?console.log(d,`${b} ${a}`):a},getDate:function(a){const b=new Date,c={year:b.getFullYear(),month:`${b.getMonth()+1}`.padStart(2,"0"),date:`${b.getDate()}`.padStart(2,"0"),hour:`${b.getHours()}`.padStart(2,"0"),minute:`${b.getMinutes()}`.padStart(2,"0"),second:`${b.getSeconds()}`.padStart(2,
"0")};return(e=>e.replace(/{([^}]+)}/g,(d,g)=>c[g]??"Error"))("string"===typeof a?a:"{year}-{month}-{date} {hour}:{minute}:{second}")},translMatcher:function(a,b=navigator.language,c="en-US"){return a[U[b]]??a[U[c]]??a[U["en-US"]]},regMenu:function(a,b={}){let {name:c="regMenu",index:e=1,reset:d=!1}=b||{};d&&da();for(let [g,h]of Object.entries(a))a=`${c}-${e++}`,"function"===typeof h&&(h={func:h}),GM_registerMenuCommand(g,()=>{h.func()},{id:a,title:h.desc,autoClose:h.close,accessKey:h.hotkey}),H.add(a)},
unMenu:da,storageListen:function(a,b){a.forEach(c=>{if(!G.has(c)){const e=GM_addValueChangeListener(c,function(d,g,h,p){b({key:d,ov:g,nv:h,far:p})});G.set(c,e)}});return{off(c){const e=G.get(c);e&&(GM_removeValueChangeListener(e),G.delete(c))},offAll(){for(const c of G.values())GM_removeValueChangeListener(c);G.clear()}}},sleep:a=>new Promise(b=>setTimeout(b,a)),domParse:a=>ha.parseFromString(a,"text/html"),getFingerprint(a){a=JSON.stringify(O[E(a)]?.(a)??a??"null");let b=1779033703,c=3024733165,
e=3362453630,d=2197574221;for(let g=0,h;g<a.length;g++)h=a.charCodeAt(g),b=c^Math.imul(b^h,597399067),c=e^Math.imul(c^h,2869860233),e=d^Math.imul(e^h,951274213),d=b^Math.imul(d^h,2716044179);return[b,c,e,d].map(g=>(2246822507*(g^g>>>16)>>>0).toString(16).padStart(8,"0")).join("")}})})();