Greasy Fork is available in English.

ObjectSyntax

Library for simplifying code logic and syntax (Object Type)

Fra og med 04.12.2024. Se den nyeste version.

Dette script bør ikke installeres direkte. Det er et bibliotek, som andre scripts kan inkludere med metadirektivet // @require https://update.greatest.deepsurf.us/scripts/495339/1496879/ObjectSyntax.js

// ==UserScript==
// @name         ObjectSyntax
// @version      2024/12/05
// @author       Canaan HS
// @description  Library for simplifying code logic and syntax (Object Type)
// @namespace    https://greatest.deepsurf.us/users/989635
// @match        *://*/*
// @license      MIT
// ==/UserScript==
const Syn=(()=>{const z={},H=new DOMParser,x=new Map,u=a=>Object.prototype.toString.call(a).slice(8,-1),D={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)},B={Match:a=>/[ .#=:]/.test(a),"#":(a,b)=>a.getElementById(b.slice(1)),".":(a,b,c)=>{a=a.getElementsByClassName(b.slice(1));return c?[...a]:a[0]},tag:(a,b,c)=>{a=a.getElementsByTagName(b);return c?[...a]:a[0]},"default":(a,b,c)=>c?a.querySelectorAll(b):a.querySelector(b)},E={Process:(a,b,c=null)=>{a=a[b.toLowerCase()];return"Function"===u(a)?a(c):void 0!==a?a:"None"}},A={verify:a=>void 0!==a?a:!1,d:a=>GM_deleteValue(a),a:()=>A.verify(GM_listValues()),s:(a,b)=>GM_setValue(a,b),g:(a,b)=>A.verify(GM_getValue(a,b)),sj:(a,b)=>GM_setValue(a,JSON.stringify(b,null,4)),gj:(a,b)=>JSON.parse(A.verify(GM_getValue(a,b)))},F={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)};return{Type:u,Device:{sX:()=>window.scrollX,sY:()=>window.scrollY,iW:()=>window.innerWidth,iH:()=>window.innerHeight,_Type:void 0,Url:location.href,Orig:location.origin,Host:location.hostname,Path:location.pathname,Lang:navigator.language,Agen:navigator.userAgent,Type:function(){return this._Type=this._Type?this._Type:this._Type=/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(this.Agen)||768>this.iW?"Mobile":"Desktop"}},$$:(a,{all:b=!1,root:c=document}={})=>{const d=B.Match(a)?B.Match(a.slice(1))?"default":a[0]:"tag";return B[d](c,a,b)},Sleep:a=>new Promise(b=>setTimeout(b,a)),Log:async(a=null,b="print",{dev:c=!0,type:d="log",collapsed:e=!0}={})=>{c&&(c=D[d]||D.log,null==a?c(b):(e?console.groupCollapsed(a):console.group(a),c(b),console.groupEnd()))},AddStyle:async(a,b="New-Style",c=!0)=>{let d=document.getElementById(b);if(!d)d=document.createElement("style"),d.id=b,document.head.appendChild(d);else if(!c)return;d.textContent+=a},AddScript:async(a,b="New-Script",c=!0)=>{let d=document.getElementById(b);if(!d)d=document.createElement("script"),d.id=b,document.head.appendChild(d);else if(!c)return;d.textContent+=a},Listen:async(a,b,c,d={},e=null)=>{try{a.addEventListener(b,c,d),e&&e(!0)}catch{e&&e(!1)}},AddListener:async(a,b,c,d={})=>{const {mark:e,...m}=d;d=e??a;const k=x.get(d);k?.has(b)||(a.addEventListener(b,c,m),k||x.set(d,new Map),x.get(d).set(b,c))},RemovListener:(a,b)=>{const c=x.get(a)?.get(b);c&&(a.removeEventListener(b,c),x.get(a).delete(b))},Observer:async function(a,b,{mark:c=!1,throttle:d=0,subtree:e=!0,childList:m=!0,attributes:k=!0,characterData:g=!1}={},l=null){if(c){if(z[c])return;z[c]=!0}c={subtree:e,childList:m,attributes:k,characterData:g};d=new MutationObserver(this.Throttle(()=>{b()},d));d.observe(a,c);l&&l({ob:d,op:c})},WaitElem:async function(a,b=null,{raf:c=!1,all:d=!1,timeout:e=8,throttle:m=50,subtree:k=!0,childList:g=!0,attributes:l=!1,characterData:r=!1,timeoutResult:p=!1,object:v=document.body}={}){const t=this;return new Promise((y,C)=>{const w=async function(){let h,f,q;if(c){let n;const G=()=>{f=d?document.querySelectorAll(a):document.querySelector(a);(q=d?0<f.length:f)?(cancelAnimationFrame(n),clearTimeout(h),b&&b(f),y(f)):n=requestAnimationFrame(G)};n=requestAnimationFrame(G);h=setTimeout(()=>{cancelAnimationFrame(n);p&&(b&&b(f),y(f))},1E3*e)}else{const n=new MutationObserver(t.Throttle(()=>{f=d?document.querySelectorAll(a):document.querySelector(a);if(q=d?0<f.length:f)n.disconnect(),clearTimeout(h),b&&b(f),y(f)},m));n.observe(v,{subtree:k,childList:g,attributes:l,characterData:r});h=setTimeout(()=>{n.disconnect();p&&(b&&b(f),y(f))},1E3*e)}};"hidden"===document.visibilityState?document.addEventListener("visibilitychange",()=>w(),{once:!0}):w()})},async WaitMap(a,b=null,{raf:c=!1,timeout:d=8,throttle:e=50,subtree:m=!0,childList:k=!0,attributes:g=!1,characterData:l=!1,timeoutResult:r=!1,object:p=document.body}={}){const v=this;return new Promise((t,y)=>{const C=async function(){let w,h;if(c){let f;const q=()=>{h=a.map(n=>document.querySelector(n));h.every(n=>null!==n&&"undefined"!==typeof n)?(cancelAnimationFrame(f),clearTimeout(w),b&&b(h),t(h)):f=requestAnimationFrame(q)};f=requestAnimationFrame(q);w=setTimeout(()=>{cancelAnimationFrame(f);r&&(b&&b(h),t(h))},1E3*d)}else{const f=new MutationObserver(v.Throttle(()=>{h=a.map(q=>document.querySelector(q));h.every(q=>null!==q&&"undefined"!==typeof q)&&(f.disconnect(),clearTimeout(w),b&&b(h),t(h))},e));f.observe(p,{subtree:m,childList:k,attributes:g,characterData:l});w=setTimeout(()=>{f.disconnect();r&&(b&&b(h),t(h))},1E3*d)}};"hidden"===document.visibilityState?document.addEventListener("visibilitychange",()=>C(),{once:!0}):C()})},Storage(a,{type:b=sessionStorage,value:c=null,error:d}={}){let e;return null!=c?F[u(c)](b,a,c):(e=b.getItem(a),void 0!=e?F[u(JSON.parse(e))](b,e):d)},DomParse:a=>H.parseFromString(a,"text/html"),NameFilter:a=>a.replace(/[\/\?<>\\:\*\|":]/g,""),GetFill:a=>Math.max(2,`${a}`.length),ExtensionName:a=>{try{return a.match(/\.([^.]+)$/)[1].toLowerCase()||"png"}catch{return"png"}},Mantissa:function(a,b,c="0",d=null){return d?`${++a}`.padStart(b,c)+`.${this.ExtensionName(d)}`:`${++a}`.padStart(b,c)},ScopeParsing:(a,b)=>{if("string"!=typeof a||""===a.trim())return b;const c=b.length,d=new Set,e=new Set,m=(g,l,r)=>{const p=g<=l;for(;p?g<=l:g>=l;p?g++:g--)r.add(g)};let k;for(k of a.split(/\s*[\.,|/]\s*/))if(/^(!|-)?\d+(~\d+|-\d+)?$/.test(k)){a=k.slice(1);const g=/^[!-]/.test(k),l=/[~-]/.test(a),[r,p]=g?[e,a]:[d,k],[v,t]=l?p.split(/-|~/):[p,p];v==t?r.add(+v-1):m(+v-1,+t-1,r)}return[...d].filter(g=>!e.has(g)&&g<c&&0<=g).sort((g,l)=>g-l).map(g=>b[g])},FormatTemplate:(a,b)=>{if("Object"!==u(a))return"Template must be an object";a=Object.fromEntries(Object.entries(a).map(([c,d])=>[c.toLowerCase(),d]));return"String"===u(b)?b.replace(/\{\s*([^}\s]+)\s*\}/g,(c,d)=>E.Process(a,d)):"Object"===u(b)?Object.entries(b).map(([c,d])=>E.Process(a,c,d)):{"Unsupported format":b}},OutputJson:async(a,b,c=null)=>{try{a="string"!==typeof a?JSON.stringify(a,null,4):a;b="string"!==typeof b?"Anonymous":b.replace(".json","");const d=document.createElement("a");d.href=`data:application/json;charset=utf-8,${encodeURIComponent(a)}`;d.download=`${b}.json`;d.click();await new Promise(e=>setTimeout(e,100));d.remove();c&&c({State:!0})}catch(d){c&&c({State:!1,Info:d})}},WorkerCreation:a=>{a=new Blob([a],{type:"application/javascript"});return new Worker(URL.createObjectURL(a))},Runtime:(a=null,{log:b=!0,format:c=!0,lable:d="Elapsed Time:",style:e="\u001b[1m\u001b[36m%s\u001b[0m"}={})=>{if(!a)return performance.now();a=c?`${((performance.now()-a)/1E3).toPrecision(3)}s`:performance.now()-a;return b?console.log(e,`${d} ${a}`):a},GetDate:(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,m)=>c[m]??"Error"))("string"===typeof a?a:"{year}-{month}-{date} {hour}:{minute}:{second}")},Throttle:(a,b)=>{let c=0;return(...d)=>{const e=Date.now();e-c>=b&&(c=e,a(...d))}},Debounce:(a,b=500)=>{let c=null;return(...d)=>{clearTimeout(c);c=setTimeout(function(){a(...d)},b)}},Menu:async(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})},Store:(a,b=null,c=null)=>A[a](b,c),StoreListen:async(a,b)=>{a.forEach(c=>{z[c]||(z[c]=!0,GM_addValueChangeListener(c,function(d,e,m,k){b({key:d,ov:e,nv:m,far:k})}))})}}})();