// ==UserScript==
// @name Syntax
// @version 2025/04/23
// @author Canaan HS
// @description Library for simplifying code logic and syntax
// @namespace https://greasyfork.org/users/989635
// @match *://*/*
// @license MPL-2.0
// ==/UserScript==
const Syn=(()=>{function x(a=document,b,c){const d=b[0],e=b.slice(1);if(/[ #.\[:]/.test(e))return c?a.querySelectorAll(b):a.querySelector(b);if(!c&&"#"===d)return document.getElementById(e);if("."===b[0])return a=a.getElementsByClassName(e),c?[...a]:a[0];a=a.getElementsByTagName(b);return c?[...a]:a[0]}async function I(a,b,c,d){let e=document.getElementById(c);if(!e)e=document.createElement(a),e.id=c,document.head.appendChild(e);else if(!d)return;e.textContent+=b}function J(a,b,c,d){let e;return null!=c?K[t(c)](b,a,c):(e=b.getItem(a),void 0!=e?K[t(JSON.parse(e))](b,e):d)}function C(a,b){let c=0;return(...d)=>{const e=Date.now();e-c>=b&&(c=e,a(...d))}}function D(a,b){let c=null;return(...d)=>{clearTimeout(c);c=setTimeout(function(){a(...d)},b)}}const y={},R=new DOMParser,t=a=>Object.prototype.toString.call(a).slice(8,-1);[Document.prototype,Element.prototype].forEach(a=>{a.$q=function(b){return x(this,b,!1)};a.$qa=function(b){return x(this,b,!0)}});const S={$q:document.$q.bind(document),$qa:document.$qa.bind(document),$html:document.documentElement,$head:document.head,$body:document.body,$img:document.images,$link:document.links,$script:document.scripts,$style:document.styleSheets,$url:()=>location.href,$origin:()=>location.origin,$domain:()=>location.hostname,$lang:()=>navigator.language,$agen:()=>navigator.userAgent,$title:(a=null)=>null!==a?document.title=a:document.title,$cookie:(a=null)=>null!==a?document.cookie=a:document.cookie,$createUrl:a=>URL.createObjectURL(a),$createFragment:()=>document.createDocumentFragment(),$createElement(a,b,c){const [d,e,g={}]="string"===typeof a?[null,a,b]:[a,b,c];if(e){var {id:h,title:f,class:k,text:n="",rows:l,cols:p,style:q={},attr:u={},...z}=g,m=Object.assign(document.createElement(e),{textContent:n});h&&(m.id=h);f&&(m.title=f);k&&(m.className=k);void 0!==l&&(m.rowSpan=l);void 0!==p&&(m.colSpan=p);Object.assign(m,z);Object.assign(m.style,"string"===typeof q?{cssText:q}:q);Object.entries(u).forEach(([v,L])=>m.setAttribute(v,L));return d instanceof HTMLElement?d.appendChild(m):m}}},E={$text(a=null){return null!==a?this.textContent=a?.trim():this.textContent?.trim()},$copy(a=!0){return this.cloneNode(a)},$iHtml(a=null){return null!==a?this.innerHTML=a:this.innerHTML},$oHtml(a=null){return null!==a?this.outerHTML=a:this.outerHTML},$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)}},M={$EventRecord:new Map,async $one(a,b,c={},d=null){try{this.addEventListener(a,b,c),d&&d(!0)}catch{d&&d(!1)}},async $onEvent(a,b,c={}){const {mark:d,...e}=c;c=d??this;const g=window.$EventRecord.get(c);g?.has(a)||(this.addEventListener(a,b,e),g||window.$EventRecord.set(c,new Map),window.$EventRecord.get(c).set(a,b))},async $offEvent(a,b){b=b??this;const c=window.$EventRecord.get(b)?.get(a);c&&(this.removeEventListener(a,c),window.$EventRecord.get(b).delete(a))}};Object.assign(window,M);Object.assign(Node.prototype,E);Object.assign(EventTarget.prototype,M);const N=Object.keys(E)[0];Object.defineProperty(Object.prototype,N,{value:function(a=null){return E[N].call(this,a)},writable:!0,configurable:!0});const O={log:a=>console.log(a),warn:a=>console.warn(a),trace:a=>console.trace(a),error:a=>console.error(a),count:a=>console.count(a)};var T=a=>{a=a.map(b=>x(document,b));return a.every(Boolean)&&a},U=(a,b)=>{a=x(document,a,b);return(b?0<a.length:a)&&a};const K={String:(a,b,c)=>null!=c?(a.setItem(b,JSON.stringify(c)),!0):JSON.parse(b),Number:(a,b,c)=>null!=c?(a.setItem(b,JSON.stringify(c)),!0):Number(b),Array:(a,b,c)=>null!=c?(a.setItem(b,JSON.stringify(c)),!0):(b=JSON.parse(b),Array.isArray(b[0])?new Map(b):b),Object:(a,b,c)=>null!=c?(a.setItem(b,JSON.stringify(c)),!0):JSON.parse(b),Boolean:(a,b,c)=>null!=c?(a.setItem(b,JSON.stringify(c)),!0):JSON.parse(b),Date:(a,b,c)=>null!=c?(a.setItem(b,JSON.stringify(c)),!0):new Date(b),Map:(a,b,c)=>(a.setItem(b,JSON.stringify([...c])),!0)},P={Process:(a,b,c=null)=>{a=a[b.toLowerCase()];return"Function"===t(a)?a(c):void 0!==a?a:"None"}},F={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"},G=a=>void 0===a||null===a?null:a;return{sX:()=>window.scrollX,sY:()=>window.scrollY,iW:()=>window.innerWidth,iH:()=>window.innerHeight,_Cache:void 0,Platform:function(){this._Cache||(void 0!==navigator.userAgentData?.mobile?this._Cache=navigator.userAgentData.mobile?"Mobile":"Desktop":window.matchMedia?.("(max-width: 767px), (pointer: coarse)")?.matches?this._Cache="Mobile":/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)?this._Cache="Mobile":this._Cache="Desktop");return this._Cache},...S,AddStyle:(a,b,c=!0)=>I("style",a,b,c),AddScript:(a,b,c=!0)=>I("script",a,b,c),Session:(a,{value:b=null,error:c}={})=>J(a,sessionStorage,b,c),Local:(a,{value:b=null,error:c}={})=>J(a,localStorage,b,c),dV:a=>GM_deleteValue(a),lV:()=>G(GM_listValues()),sV:(a,b)=>GM_setValue(a,b),gV:(a,b)=>G(GM_getValue(a,b)),sJV:(a,b,c=0)=>GM_setValue(a,JSON.stringify(b,null,c)),gJV:(a,b)=>{try{return JSON.parse(G(GM_getValue(a,b)))}catch{return b}},Type:t,Log:async function(a=null,b="print",{dev:c=!0,type:d="log",collapsed:e=!0}={}){c&&(c=O[d]||O.log,null==a?c(b):(e?console.groupCollapsed(a):console.group(a),c(b),console.groupEnd()))},onUrlChange:function(a,b=15){function c(){clearTimeout(d);d=setTimeout(()=>{a({url:location.href,domain:location.hostname})},Math.max(15,b))}let d=null;const e=history.pushState;history.pushState=function(){e.apply(this,arguments);c()};const g=history.replaceState;history.replaceState=function(){g.apply(this,arguments);c()};window.addEventListener("popstate",c);return function(){clearTimeout(d);history.pushState=e;history.replaceState=g;window.removeEventListener("popstate",c)}},Observer:async function(a,b,c={},d=null){const {mark:e="",debounce:g=0,throttle:h=100,subtree:f=!0,childList:k=!0,attributes:n=!0,characterData:l=!1}=c??{};if(e){if(y[e])return;y[e]=!0}const [p,q]=0<g?[D,g]:[C,h];c={subtree:f,childList:k,attributes:n,characterData:l};const u=new MutationObserver(p(()=>{b()},q));u.observe(a,c);d&&d({ob:u,op:c})},WaitElem:async function(a,b=null,c={}){const d=Array.isArray(a)?T:U,{raf:e=!1,all:g=!1,timeout:h=8,throttle:f=0,debounce:k=50,visibility:n=!0,subtree:l=!0,childList:p=!0,attributes:q=!0,characterData:u=!1,timeoutResult:z=!1,root:m=document}=c??{};return new Promise((v,L)=>{const Q=async function(){let A,r;if(e){let w;const B=()=>{(r=d(a,g))?(cancelAnimationFrame(w),clearTimeout(A),b&&b(r),v(r)):w=requestAnimationFrame(B)};w=requestAnimationFrame(B);A=setTimeout(()=>{cancelAnimationFrame(w);z&&(b&&b(r),v(r))},1E3*h)}else{const [w,B]=0<f?[C,f]:[D,k],H=new MutationObserver(w(()=>{if(r=d(a,g))H.disconnect(),clearTimeout(A),b&&b(r),v(r)},B));H.observe(m,{subtree:l,childList:p,attributes:q,characterData:u});A=setTimeout(()=>{H.disconnect();z&&(b&&b(r),v(r))},1E3*h)}};n&&"hidden"===document.visibilityState?document.addEventListener("visibilitychange",()=>Q(),{once:!0}):Q()})},Throttle:C,Debounce:D,ScopeParsing:function(a,b){if("string"!==typeof a||""===a.trim())return b;const c=b.length,d=new Set,e=new Set,g=(f,k,n)=>{const l=f<=k;for(;l?f<=k:f>=k;l?f++:f--)n.add(f)};let h;for(h of a.split(/\s*[\.,|/]\s*/))if(/^(!|-)?\d+(~\d+|-\d+)?$/.test(h)){a=h.slice(1);const f=/^[!-]/.test(h),k=/[~-]/.test(a),[n,l]=f?[e,a]:[d,h],[p,q]=k?l.split(/-|~/):[l,l];p==q?n.add(+p-1):g(+p-1,+q-1,n)}return[...d].filter(f=>!e.has(f)&&f<c&&0<=f).sort((f,k)=>f-k).map(f=>b[f])},FormatTemplate:function(a,b){if("Object"!==t(a))return"Template must be an object";a=Object.fromEntries(Object.entries(a).map(([c,d])=>[c.toLowerCase(),d]));return"String"===t(b)?b.replace(/\{\s*([^}\s]+)\s*\}/g,(c,d)=>P.Process(a,d)):"Object"===t(b)?Object.entries(b).map(([c,d])=>P.Process(a,c,d)):{"Unsupported format":b}},OutputTXT:async function(a,b,c=null){try{b="string"!==typeof b?"Anonymous.txt":b.endsWith(".txt")?b:`${b}.txt`;const d=new Blob([a],{type:"text/plain"}),e=document.createElement("a");e.href=URL.createObjectURL(d);e.download=b;e.click();URL.revokeObjectURL(e.href);e.remove();c&&c({State:!0})}catch(d){c&&c({State:!1,Info:d})}},OutputJson:async function(a,b,c=null){try{a="string"!==typeof a?JSON.stringify(a,null,4):a;b="string"!==typeof b?"Anonymous.json":b.endsWith(".json")?b:`${b}.json`;const d=new Blob([a],{type:"application/json"}),e=document.createElement("a");e.href=URL.createObjectURL(d);e.download=b;e.click();URL.revokeObjectURL(e.href);e.remove();c&&c({State:!0})}catch(d){c&&c({State:!1,Info:d})}},Runtime:function(a=null,{lable:b="Elapsed Time:",log:c=!0,format:d=!0,style:e="\u001b[1m\u001b[36m%s\u001b[0m"}={}){if(!a)return performance.now();a=d?`${((performance.now()-a)/1E3).toPrecision(3)}s`:performance.now()-a;return c?console.log(e,`${b} ${a}`):a},GetDate:function(a=null){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(d=>d.replace(/{([^}]+)}/g,(e,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[F[b]]??a[F[c]]??a[F["en-US"]]},Menu:async function(a,b="Menu",c=1){for(const [d,e]of Object.entries(a))GM_registerMenuCommand(d,()=>{e.func()},{title:e.desc,id:`${b}-${c++}`,autoClose:e.close,accessKey:e.hotkey})},StoreListen:async function(a,b){a.forEach(c=>{y[c]||(y[c]=!0,GM_addValueChangeListener(c,function(d,e,g,h){b({key:d,ov:e,nv:g,far:h})}))})},WorkerCreation:a=>{a=new Blob([a],{type:"application/javascript"});return new Worker(URL.createObjectURL(a))},Sleep:a=>new Promise(b=>setTimeout(b,a)),DomParse:a=>R.parseFromString(a,"text/html"),NameFilter:a=>a.replace(/[\/\?<>\\:\*\|":]/g,""),GetFill:a=>Math.max(2,`${a}`.length),ExtensionName:a=>{try{return a.match(/\.([^.]+)$/)[1].toLowerCase()||"webp"}catch{return"webp"}},Mantissa:function(a,b,c="0",d=null){return d?`${++a}`.padStart(b,c)+`.${this.ExtensionName(d)}`:`${++a}`.padStart(b,c)}}})();