Syntax

Library for simplifying code logic and syntax

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

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/495339/1561345/Syntax.js

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Syntax
// @version      2025/03/28
// @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 y(a=document,b,c){const e=b[0],d=b.slice(1);if(/[ #.\[:]/.test(d))return c?a.querySelectorAll(b):a.querySelector(b);if(!c&&"#"===e)return document.getElementById(d);if("."===b[0])return a=a.getElementsByClassName(d),c?[...a]:a[0];a=a.getElementsByTagName(b);return c?[...a]:a[0]}async function H(a,b,c,e){let d=document.getElementById(c);if(!d)d=document.createElement(a),d.id=c,document.head.appendChild(d);else if(!e)return;d.textContent+=b}function I(a,b,c,e){let d;return null!=c?J[t(c)](b,a,c):(d=b.getItem(a),void 0!=d?J[t(JSON.parse(d))](b,d):e)}function D(a,b){let c=null;return(...e)=>{clearTimeout(c);c=setTimeout(function(){a(...e)},b)}}const z={},O=new DOMParser,x=new Map,t=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)}});const P={$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,$createFragment:()=>document.createDocumentFragment(),$createElement:(a,b,c)=>{const [e,d,g={}]="string"===typeof a?[null,a,b]:[a,b,c];if(d){var {id:k="",class:f="",text:l="",title:m="",style:h={},attributes:u={},rowspan:p,colspan:A,...v}=g,q=Object.assign(document.createElement(d),{id:k,className:f,textContent:l,title:m});void 0!==p&&(q.rowSpan=p);void 0!==A&&(q.colSpan=A);Object.assign(q,v);Object.assign(q.style,"string"===typeof h?{cssText:h}:h);Object.entries(u).forEach(([B,w])=>q.setAttribute(B,w));return e instanceof HTMLElement?e.appendChild(q):q}}},C={$text(a=null){return null!==a?this.textContent=a?.trim():this.textContent?.trim()},$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)},$copy(a=!0){return this.cloneNode(a)}},E={$one(a,b,c={},e=null){try{this.addEventListener(a,b,c),e&&e(!0)}catch{e&&e(!1)}},$onEvent(a,b,c={}){const {mark:e,...d}=c;c=e??this;const g=x.get(c);g?.has(a)||(this.addEventListener(a,b,d),g||x.set(c,new Map),x.get(c).set(a,b))},$offEvent(a,b){b=b??this;const c=x.get(b)?.get(a);c&&(this.removeEventListener(a,c),x.get(b).delete(a))}};Object.assign(window,E);Object.assign(Node.prototype,C);Object.assign(EventTarget.prototype,E);const K=Object.keys(C)[0];Object.defineProperty(Object.prototype,K,{value:function(a=null){return C[K].call(this,a)},writable:!0,configurable:!0});const L={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 Q=a=>{a=a.map(b=>y(document,b));return a.every(Boolean)&&a},R=(a,b)=>{a=y(document,a,b);return(b?0<a.length:a)&&a};const J={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)},M={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?a:"";return{sX:()=>window.scrollX,sY:()=>window.scrollY,iW:()=>window.innerWidth,iH:()=>window.innerHeight,_Cache:void 0,Platform:function(){return this._Cache=this._Cache?this._Cache:this._Cache=/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(this.Agen)||768>this.iW?"Mobile":"Desktop"},...P,ElementFunctions:Object.keys(C),EventFunctions:Object.keys(E),AddStyle:(a,b,c=!0)=>H("style",a,b,c),AddScript:(a,b,c=!0)=>H("script",a,b,c),Session:(a,{value:b=null,error:c}={})=>I(a,sessionStorage,b,c),Local:(a,{value:b=null,error:c}={})=>I(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)=>GM_setValue(a,JSON.stringify(b,null,4)),gJV:(a,b)=>JSON.parse(G(GM_getValue(a,b))),Type:t,Log:async function(a=null,b="print",{dev:c=!0,type:e="log",collapsed:d=!0}={}){c&&(c=L[e]||L.log,null==a?c(b):(d?console.groupCollapsed(a):console.group(a),c(b),console.groupEnd()))},Observer:async function(a,b,c={},e=null){const {mark:d="",debounce:g=0,subtree:k=!0,childList:f=!0,attributes:l=!0,characterData:m=!1}=c??{};if(d){if(z[d])return;z[d]=!0}c={subtree:k,childList:f,attributes:l,characterData:m};const h=new MutationObserver(D(()=>{b()},g));h.observe(a,c);e&&e({ob:h,op:c})},WaitElem:async function(a,b=null,c={}){const e=Array.isArray(a)?Q:R,{raf:d=!1,all:g=!1,timeout:k=8,debounce:f=50,subtree:l=!0,childList:m=!0,attributes:h=!0,characterData:u=!1,timeoutResult:p=!1,root:A=document}=c??{};return new Promise((v,q)=>{const B=async function(){let w,n;if(d){let r;const N=()=>{(n=e(a,g))?(cancelAnimationFrame(r),clearTimeout(w),b&&b(n),v(n)):r=requestAnimationFrame(N)};r=requestAnimationFrame(N);w=setTimeout(()=>{cancelAnimationFrame(r);p&&(b&&b(n),v(n))},1E3*k)}else{const r=new MutationObserver(D(()=>{if(n=e(a,g))r.disconnect(),clearTimeout(w),b&&b(n),v(n)},f));r.observe(A,{subtree:l,childList:m,attributes:h,characterData:u});w=setTimeout(()=>{r.disconnect();p&&(b&&b(n),v(n))},1E3*k)}};"hidden"===document.visibilityState?document.$one("visibilitychange",()=>B(),{once:!0}):B()})},Throttle:function(a,b){let c=0;return(...e)=>{const d=Date.now();d-c>=b&&(c=d,a(...e))}},Debounce:D,ScopeParsing:function(a,b){if("string"!==typeof a||""===a.trim())return b;const c=b.length,e=new Set,d=new Set,g=(f,l,m)=>{const h=f<=l;for(;h?f<=l:f>=l;h?f++:f--)m.add(f)};let k;for(k of a.split(/\s*[\.,|/]\s*/))if(/^(!|-)?\d+(~\d+|-\d+)?$/.test(k)){a=k.slice(1);const f=/^[!-]/.test(k),l=/[~-]/.test(a),[m,h]=f?[d,a]:[e,k],[u,p]=l?h.split(/-|~/):[h,h];u==p?m.add(+u-1):g(+u-1,+p-1,m)}return[...e].filter(f=>!d.has(f)&&f<c&&0<=f).sort((f,l)=>f-l).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,e])=>[c.toLowerCase(),e]));return"String"===t(b)?b.replace(/\{\s*([^}\s]+)\s*\}/g,(c,e)=>M.Process(a,e)):"Object"===t(b)?Object.entries(b).map(([c,e])=>M.Process(a,c,e)):{"Unsupported format":b}},OutputTXT:async function(a,b,c=null){try{b="string"!==typeof b?"Anonymous.txt":b.endsWith(".txt")?b:`${b}.txt`;const e=new Blob([a],{type:"text/plain"}),d=document.createElement("a");d.href=URL.createObjectURL(e);d.download=b;d.click();URL.revokeObjectURL(d.href);d.remove();c&&c({State:!0})}catch(e){c&&c({State:!1,Info:e})}},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 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&&c({State:!0})}catch(e){c&&c({State:!1,Info:e})}},Runtime:function(a=null,{lable: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=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(e=>e.replace(/{([^}]+)}/g,(d,g)=>c[g]??"Error"))("string"===typeof a?a:"{year}-{month}-{date} {hour}:{minute}:{second}")},TranslMatcher:function(a,b,c="en-US"){return a[F[b]]??a[F[c]]??a[F["en-US"]]},Menu:async function(a,b="Menu",c=1){for(const [e,d]of Object.entries(a))GM_registerMenuCommand(e,()=>{d.func()},{title:d.desc,id:`${b}-${c++}`,autoClose:d.close,accessKey:d.hotkey})},StoreListen:async function(a,b){a.forEach(c=>{z[c]||(z[c]=!0,GM_addValueChangeListener(c,function(e,d,g,k){b({key:e,ov:d,nv:g,far:k})}))})},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=>O.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",e=null){return e?`${++a}`.padStart(b,c)+`.${this.ExtensionName(e)}`:`${++a}`.padStart(b,c)}}})();