GrammarSimplified

Simple syntax simplification function

目前為 2024-03-23 提交的版本,檢視 最新版本

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

// ==UserScript==
// @name         GrammarSimplified
// @version      2024/03/23
// @author       HentaiSaru
// @description  Simple syntax simplification function
// @namespace    https://greasyfork.org/users/989635
// @match        *://*/*
// ==/UserScript==
class API{constructor(){this.Buffer=document.createDocumentFragment(),this.ListenerRecord=new Map,this.Parser=new DOMParser,this.Template={log:e=>console.log(e),warn:e=>console.warn(e),error:e=>console.error(e),count:e=>console.count(e)}}$$(e,t=!1,r=document){const n=e.slice(1);switch([".","#"," ","="].some(e=>n.includes(e))?" ":e[0]){case"#":return r.getElementById(n);case" ":return t?r.querySelectorAll(e):r.querySelector(e);case".":e=r.getElementsByClassName(n);break;default:e=r.getElementsByTagName(e)}return t?Array.from(e):e[0]}DomParse(e){return this.Parser.parseFromString(e,"text/html")}GetFill(e){return Math.max(2,(""+e).length)}Mantissa(e,t,r="0"){return(""+ ++e).padStart(t,r)}IllegalCharacters(e){return e.replace(/[\/\?<>\\:\*\|":]/g,"")}ExtensionName(e){try{return e.match(/\.([^.]+)$/)[1].toLowerCase()||"png"}catch{return"png"}}WorkerCreation(e){return e=new Blob([e],{type:"application/javascript"}),new Worker(URL.createObjectURL(e))}sleep(t){return new Promise(e=>setTimeout(e,t))}async AddStyle(e,t="New-Style"){let r=document.getElementById(t);r||((r=document.createElement("style")).id=t,document.head.appendChild(r)),r.appendChild(document.createTextNode(e))}async AddScript(e,t="New-Script"){let r=document.getElementById(t);r||((r=document.createElement("script")).id=t,document.head.appendChild(r)),r.appendChild(document.createTextNode(e))}async AddListener(e,t,r,n={}){this.ListenerRecord.has(e)&&this.ListenerRecord.get(e).has(t)||(e.addEventListener(t,r,n),this.ListenerRecord.has(e)||this.ListenerRecord.set(e,new Map),this.ListenerRecord.get(e).set(t,r))}async RemovListener(e,t){var r;this.ListenerRecord.has(e)&&this.ListenerRecord.get(e).has(t)&&(r=this.ListenerRecord.get(e).get(t),e.removeEventListener(t,r),this.ListenerRecord.get(e).delete(t))}async Listen(e,t,r,n={},s=null){try{e.addEventListener(t,r,n),s&&s(!0)}catch{s&&s(!1)}}async WaitElem(e,t,r,n,s=document.body,l=0){let o,a,i;const c=new MutationObserver(this.Throttle_discard(()=>{a=t?document.querySelectorAll(e):document.querySelector(e),(i=t?0<a.length&&Array.from(a).every(e=>null!=e):a)&&(c.disconnect(),clearTimeout(o),n(a))},l));c.observe(s,{childList:!0,subtree:!0}),o=setTimeout(()=>{c.disconnect()},1e3*r)}async WaitMap(e,t,r,n=document.body,s=0){let l,o;const a=new MutationObserver(this.Throttle_discard(()=>{(o=e.map(e=>document.querySelector(e))).every(e=>null!==e&&"string")&&(a.disconnect(),clearTimeout(l),r(o))},s));a.observe(n,{childList:!0,subtree:!0}),l=setTimeout(()=>{a.disconnect()},1e3*t)}async log(e=null,t="print",r="log"){r="string"==typeof r&&this.Template[r]?r:"log",null==e?this.Template[r](t):(console.groupCollapsed(e),this.Template[r](t),console.groupEnd())}Runtime(e=null,t=!0){return e?t?console.log("%s",`Elapsed Time: ${(Date.now()-e)/1e3}s`):Date.now()-e:Date.now()}Throttle(r,n){let s=null;return function(){let e=this,t=arguments;null==s&&(s=setTimeout(function(){r.apply(e,t),s=null},n))}}Throttle_discard(r,n){let s=0;return function(){var e=arguments,t=Date.now();t-s>=n&&(r.apply(this,e),s=t)}}Storage(t,r,n=null){var e,s={Type:e=>Object.prototype.toString.call(e).slice(8,-1),String:e=>e?JSON.parse(e):(t.setItem(r,JSON.stringify(n)),!0),Number:e=>e?Number(e):(t.setItem(r,JSON.stringify(n)),!0),Array:e=>e?JSON.parse(e):(t.setItem(r,JSON.stringify(n)),!0),Object:e=>e?JSON.parse(e):(t.setItem(r,JSON.stringify(n)),!0),Boolean:e=>e?JSON.parse(e):(t.setItem(r,JSON.stringify(n)),!0),Date:e=>e?new Date(e):(t.setItem(r,JSON.stringify(n)),!0),Map:e=>e?new Map(JSON.parse(e)):(t.setItem(r,JSON.stringify([...n])),!0)};return null!=n?s[s.Type(n)]():!!(e=t.getItem(r))&&s[s.Type(JSON.parse(e))](e)}store(e,t=null,r=null){const n=e=>void 0!==e?e:null;return{del:e=>GM_deleteValue(e),all:()=>n(GM_listValues()),set:(e,t)=>GM_setValue(e,t),get:(e,t)=>n(GM_getValue(e,t)),sjs:(e,t)=>GM_setValue(e,JSON.stringify(t,null,4)),gjs:(e,t)=>JSON.parse(n(GM_getValue(e,t)))}[e](t,r)}async Menu(e,t="Menu",r=1){for(const[n,s]of Object.entries(e))GM_registerMenuCommand(n,()=>{s.func()},{title:s.desc,id:t+"-"+r++,autoClose:s.close,accessKey:s.hotkey})}}