Greasy Fork

SyntaxLite

Library for simplifying code logic and syntax (Lite)

当前为 2025-03-28 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/487608/1561377/SyntaxLite.js

// ==UserScript==
// @name         SyntaxLite
// @version      2025/03/28
// @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 Syn=(()=>{function w(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[B(c)](b,a,c):(d=b.getItem(a),void 0!=d?J[B(JSON.parse(d))](b,d):e)}function C(a,b){let c=null;return(...e)=>{clearTimeout(c);c=setTimeout(function(){a(...e)},b)}}const x={},q=new Map,B=a=>Object.prototype.toString.call(a).slice(8,-1);[Document.prototype,Element.prototype].forEach(a=>{a.$q=function(b){return w(this,b,!1)};a.$qa=function(b){return w(this,b,!0)}});const N={$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,f={}]="string"===typeof a?[null,a,b]:[a,b,c];if(d){var {id:h="",class:r="",text:t="",title:u="",style:k={},attributes:D={},rowspan:v,colspan:y,...n}=f,l=Object.assign(document.createElement(d),{id:h,className:r,textContent:t,title:u});void 0!==v&&(l.rowSpan=v);void 0!==y&&(l.colSpan=y);Object.assign(l,n);Object.assign(l.style,"string"===typeof k?{cssText:k}:k);Object.entries(D).forEach(([z,p])=>l.setAttribute(z,p));return e instanceof HTMLElement?e.appendChild(l):l}}},A={$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 f=q.get(c);f?.has(a)||(this.addEventListener(a,b,d),f||q.set(c,new Map),q.get(c).set(a,b))},$offEvent(a,b){b=b??this;const c=q.get(b)?.get(a);c&&(this.removeEventListener(a,c),q.get(b).delete(a))}};Object.assign(window,E);Object.assign(Node.prototype,A);Object.assign(EventTarget.prototype,E);const K=Object.keys(A)[0];Object.defineProperty(Object.prototype,K,{value:function(a=null){return A[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 O=a=>{a=a.map(b=>w(document,b));return a.every(Boolean)&&a},P=(a,b)=>{a=w(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)},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"},...N,ElementFunctions:Object.keys(A),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:B,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:f=0,subtree:h=!0,childList:r=!0,attributes:t=!0,characterData:u=!1}=c??{};if(d){if(x[d])return;x[d]=!0}c={subtree:h,childList:r,attributes:t,characterData:u};const k=new MutationObserver(C(()=>{b()},f));k.observe(a,c);e&&e({ob:k,op:c})},WaitElem:async function(a,b=null,c={}){const e=Array.isArray(a)?O:P,{raf:d=!1,all:f=!1,timeout:h=8,debounce:r=50,subtree:t=!0,childList:u=!0,attributes:k=!0,characterData:D=!1,timeoutResult:v=!1,root:y=document}=c??{};return new Promise((n,l)=>{const z=async function(){let p,g;if(d){let m;const M=()=>{(g=e(a,f))?(cancelAnimationFrame(m),clearTimeout(p),b&&b(g),n(g)):m=requestAnimationFrame(M)};m=requestAnimationFrame(M);p=setTimeout(()=>{cancelAnimationFrame(m);v&&(b&&b(g),n(g))},1E3*h)}else{const m=new MutationObserver(C(()=>{if(g=e(a,f))m.disconnect(),clearTimeout(p),b&&b(g),n(g)},r));m.observe(y,{subtree:t,childList:u,attributes:k,characterData:D});p=setTimeout(()=>{m.disconnect();v&&(b&&b(g),n(g))},1E3*h)}};"hidden"===document.visibilityState?document.$one("visibilitychange",()=>z(),{once:!0}):z()})},Throttle:function(a,b){let c=0;return(...e)=>{const d=Date.now();d-c>=b&&(c=d,a(...e))}},Debounce:C,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})}},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,f)=>c[f]??"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=>{x[c]||(x[c]=!0,GM_addValueChangeListener(c,function(e,d,f,h){b({key:e,ov:d,nv:f,far:h})}))})}}})();