ClassSyntax

Library for simplifying code logic and syntax (Class Type)

目前為 2025-03-26 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/487608/1560531/ClassSyntax.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         ClassSyntax
// @version      2025/03/27
// @author       Canaan HS
// @description  Library for simplifying code logic and syntax (Class Type)
// @namespace    https://greasyfork.org/users/989635
// @match        *://*/*
// @license      MPL-2.0
// ==/UserScript==
class Syntax{constructor(){this.Mark={};this.Parser=new DOMParser;this.ListenerRecord=new Map;this.Type=a=>Object.prototype.toString.call(a).slice(8,-1);this.Print={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)};this.WaitCore={queryMap:a=>{a=a.map(b=>document.querySelector(b));return a.every(Boolean)&&a},queryElement:(a,b)=>{a=b?document.querySelectorAll(a):document.querySelector(a);return(b?0<a.length:a)&&a}};this.TemplateMatch={Process:(a,b,c=null)=>{a=a[b.toLowerCase()];return"Function"===this.Type(a)?a(c):void 0!==a?a:"None"}};this.StoreMatch={verify:a=>void 0!==a?a:!1,d:a=>GM_deleteValue(a),a:()=>this.StoreMatch.verify(GM_listValues()),s:(a,b)=>GM_setValue(a,b),g:(a,b)=>this.StoreMatch.verify(GM_getValue(a,b)),sj:(a,b)=>GM_setValue(a,JSON.stringify(b,null,4)),gj:(a,b)=>JSON.parse(this.StoreMatch.verify(GM_getValue(a,b)))};this.StorageMatch={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)};this.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}={}){return b||"#"!==a[0]||-1!==a.indexOf(" ")?"."===a[0]&&-1===a.indexOf(" ")?(a=c.getElementsByClassName(a.slice(1)),b?[...a]:a[0]):/[ #.\[:]/.test(a)?b?c.querySelectorAll(a):c.querySelector(a):(a=c.getElementsByTagName(a),b?[...a]:a[0]):document.getElementById(a.slice(1))}Sleep(a){return new Promise(b=>setTimeout(b,a))}async Log(a=null,b="print",{dev:c=!0,type:d="log",collapsed:e=!0}={}){c&&(c=this.Print[d]||this.Print.log,null==a?c(b):(e?console.groupCollapsed(a):console.group(a),c(b),console.groupEnd()))}async AddStyle(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}async AddScript(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}async Listen(a,b,c,d={},e=null){try{a.addEventListener(b,c,d),e&&e(!0)}catch{e&&e(!1)}}async AddListener(a,b,c,d={}){const {mark:e,...h}=d;d=e??a;const g=this.ListenerRecord.get(d);g?.has(b)||(a.addEventListener(b,c,h),g||this.ListenerRecord.set(d,new Map),this.ListenerRecord.get(d).set(b,c))}async RemovListener(a,b){const c=this.ListenerRecord.get(a)?.get(b);c&&(a.removeEventListener(b,c),this.ListenerRecord.get(a).delete(b))}async Observer(a,b,c={},d=null){const {mark:e="",debounce:h=0,subtree:g=!0,childList:f=!0,attributes:k=!0,characterData:n=!1}=c??{};if(e){if(Mark[e])return;Mark[e]=!0}c={subtree:g,childList:f,attributes:k,characterData:n};const l=new MutationObserver(this.Debounce(()=>{b()},h));l.observe(a,c);d&&d({ob:l,op:c})}async WaitElem(a,b=null,c={}){const d=this,e=Array.isArray(a)?WaitCore.queryMap:WaitCore.queryElement,{raf:h=!1,all:g=!1,timeout:f=8,debounce:k=50,subtree:n=!0,childList:l=!0,attributes:q=!0,characterData:r=!1,timeoutResult:v=!1,root:y=document}=c??{};return new Promise((t,z)=>{const x=async function(){let u,m;if(h){let p;const w=()=>{(m=e(a,g))?(cancelAnimationFrame(p),clearTimeout(u),b&&b(m),t(m)):p=requestAnimationFrame(w)};p=requestAnimationFrame(w);u=setTimeout(()=>{cancelAnimationFrame(p);v&&(b&&b(m),t(m))},1E3*f)}else{const p=new MutationObserver(d.Debounce(()=>{if(m=e(a,g))p.disconnect(),clearTimeout(u),b&&b(m),t(m)},k));p.observe(y,{subtree:n,childList:l,attributes:q,characterData:r});u=setTimeout(()=>{p.disconnect();v&&(b&&b(m),t(m))},1E3*f)}};"hidden"===document.visibilityState?document.addEventListener("visibilitychange",()=>x(),{once:!0}):x()})}Storage(a,{type:b=sessionStorage,value:c=null,error:d}={}){let e;return null!=c?this.StorageMatch[this.Type(c)](b,a,c):(e=b.getItem(a),void 0!=e?this.StorageMatch[this.Type(JSON.parse(e))](b,e):d)}DomParse(a){return this.Parser.parseFromString(a,"text/html")}NameFilter(a){return a.replace(/[\/\?<>\\:\*\|":]/g,"")}GetFill(a){return Math.max(2,`${a}`.length)}ExtensionName(a){try{return a.match(/\.([^.]+)$/)[1].toLowerCase()||"png"}catch{return"png"}}Mantissa(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,h=(f,k,n)=>{const l=f<=k;for(;l?f<=k:f>=k;l?f++:f--)n.add(f)};let g;for(g of a.split(/\s*[\.,|/]\s*/))if(/^(!|-)?\d+(~\d+|-\d+)?$/.test(g)){a=g.slice(1);const f=/^[!-]/.test(g),k=/[~-]/.test(a),[n,l]=f?[e,a]:[d,g],[q,r]=k?l.split(/-|~/):[l,l];q==r?n.add(+q-1):h(+q-1,+r-1,n)}return[...d].filter(f=>!e.has(f)&&f<c&&0<=f).sort((f,k)=>f-k).map(f=>b[f])}FormatTemplate(a,b){if("Object"!==this.Type(a))return"Template must be an object";a=Object.fromEntries(Object.entries(a).map(([c,d])=>[c.toLowerCase(),d]));return"String"===this.Type(b)?b.replace(/\{\s*([^}\s]+)\s*\}/g,(c,d)=>this.TemplateMatch.Process(a,d)):"Object"===this.Type(b)?Object.entries(b).map(([c,d])=>this.TemplateMatch.Process(a,c,d)):{"Unsupported format":b}}async OutputTXT(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})}}async OutputJson(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})}}WorkerCreation(a){a=new Blob([a],{type:"application/javascript"});return new Worker(URL.createObjectURL(a))}Runtime(a=null,{log:b=!0,format:c=!0,label: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,h)=>c[h]??"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)}}async Menu(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){return this.StoreMatch[a](b,c)}async StoreListen(a,b){a.forEach(c=>{this.Mark[c]||(this.Mark[c]=!0,GM_addValueChangeListener(c,function(d,e,h,g){b({key:d,ov:e,nv:h,far:g})}))})}};