mylib

我的工具

Verze ze dne 26. 06. 2024. Zobrazit nejnovější verzi.

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greasyfork.org/scripts/435697/1400867/mylib.js

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name mylib
// @description 我的工具
// @dependency unsafeWindow GM_setValue GM_getValue
// ==/UserScript==
(function(win){
    const e=document.documentElement.firstElementChild,sv=GM_setValue,gv=GM_getValue;
    let z,myBtns;
    // win.gv=gv;win.sv=sv
    win.my=new class {
        constructor(){
            if(e.tagName==='Z') z=this.zone=e;
            else {
                z=this.zone=this.before('z',e,'',`class`,'rwf');
                win.cl=win.console.log;win.al=win.alert;
            }
            if(z.querySelector('my-btns')) myBtns=z.querySelector('my-btns');
            else{
                this.addStyle(`my-btns{display:block;z-index:9195129;position:fixed;height: min-content;}.my-btn{user-select: none;font-size: initial !important;}`,'my-btns');
                myBtns=this.append('my-btns',z,'',`class`,'rwf');
                myBtns.style.left=gv('left',0);
                myBtns.style.top=gv('top',0);
                // myBtns.style.display='flex';
                if(!gv('isFixed',0) || top!==self) this.bindDrag(myBtns);
                myBtns.onmouseup=e=>{
                    sv('left',myBtns.style.left);
                    sv('top',myBtns.style.top);
                }
            }
        }
        addStyle(css,className='rwf'){//depend:after,zone
            return this.append('style',z,css,`class`,className);
        }
        append(tag,dom,content){
            if(!tag) return;
            const son=typeof tag==='string'?document.createElement(tag):tag instanceof EventTarget?tag:0;
            let len=arguments.length;
            if(dom instanceof EventTarget)dom.append(son);
            if(content)son.append(content);
            while(len>3){
                son.setAttribute([arguments[len-2]],arguments[len-1]);len-=2;
            }return son;
        }//my.append('tag',document.body,'content','idk','true','data-s')
        after(tag,dom,content){
            if(!tag) return;
            const bro=typeof tag==='string'?document.createElement(tag):tag instanceof EventTarget?tag:0;
            let len=arguments.length;
            if(dom instanceof EventTarget)dom.after(bro);
            if(content)bro.append(content);
            while(len>3){
                bro.setAttribute([arguments[len-2]],arguments[len-1]);len-=2;
            }return bro;
        }//my.after('div',document.body,'','suck','1','dick',000)
        before(tag,dom,content){
            if(!tag) return;
            const bro=typeof tag==='string'?
                  document.createElement(tag):tag instanceof EventTarget?
                  tag:0;
            let len=arguments.length;
            if(dom instanceof EventTarget)dom.before(bro);
            if(content)bro.append(content);
            while(len>3){
                bro.setAttribute([arguments[len-2]],arguments[len-1]);len-=2;
            }return bro;
        }// my.before('div',document.head,'','suck','1','dick')

        eods(){//enable or disable style in my.zone
            z.querySelectorAll('style').forEach(e=>e.type==='0'?e.type='':e.type=0);
        }
        sohe(...args){//show or hide elements
            args.forEach(e=>{
                if(typeof e!=="string") e.style.display==='none'?e.style.display='initial':e.style.display='none';
                else document.querySelectorAll(e).forEach(e=>e.style.display==='none'?e.style.display='initial':e.style.display='none');
            });
        }
        s2d(seconds){//seconds2date
            const date=new Date(seconds),year=date.getFullYear(),month=date.getMonth()+1,day=date.getDate(),
                  hour=date.getHours(),minute=date.getMinutes(),second=date.getSeconds(),milliseconds=date.getMilliseconds(),currentTime=year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second+":"+milliseconds;
            return currentTime;
        }//my.s2d(new Date().getTime())
        tieba(uname,pwd){
            document.querySelector('[name="userName"]').value=uname;
            document.querySelector('[name="password"]').value=pwd;
            return document.querySelector('[value="登录"]').click();
        }
        fixTitle(){//stop title be changed
            Object.defineProperty(document,"title",{
                writable:false
            });
        }
        addBtns(...args){//depend:addStyle,append
            const len=args.length;
            for(let i=0;i<len;i++){
                const btn= this.append('input',myBtns,'','type','button','class','my-btn','value',args[i].name+' ');
                btn.addEventListener('click',args[i]);
            }
            return myBtns;
        }//my.addBtns(()=>{},e=>{confirm(e.target.id)},function(e){prompt(e.target.outerHTML)},function test(e){return 1})
        addAs(...args){//depend:addStyle,append
            const len=args.length;
            for(let i=0;i<args.length;i+=2){
                this.append('a',myBtns,args[i]+'|',`href`,args[i+1],'class','my-btn');
            }
        }//my.addAs("bilibili","https://www.bilibili.com/","baidu","https://www.baidu.com/")
        bindDrag(ele){//鼠标拖动
            ele.onmousedown=function(ev){
                //if(ev.target.tagName==='TEXTAREA') return;
                const diffX=ev.clientX-ele.offsetLeft,diffY=ev.clientY-ele.offsetTop,iw=win.innerWidth,ih=win.innerHeight;
                document.onmousemove=function(ev){
                    let moveX=ev.clientX-diffX,moveY=ev.clientY-diffY;
                    moveX<0?moveX=0:moveX>iw-ele.offsetWidth?moveX=iw-ele.offsetWidth:0
                    moveY<0?moveY=0:moveY>ih-ele.offsetHeight?moveY=ih-ele.offsetHeight:0;
                    ele.style.left=moveX/iw*100+'%'//moveX + 'px';
                    ele.style.top=moveY/ih*100+'%'//moveY + 'px'
                }
                document.onmouseup=function(ev){
                    this.onmousemove=null;
                    this.onmouseup=null;
                }
            }
        }
        sleep(time){
            return new Promise((resolve) => setTimeout(resolve, time));
        }
        waitForElement(selector, timeout = 3e4) {//查找ele
            return new Promise((resolve, reject) => {
                const intervalTime = 500;
                const startTime = Date.now();
                const interval = setInterval(() => {
                    const element = document.querySelector(selector);
                    if (element) {
                        clearInterval(interval);
                        resolve(element);//找到则返回函数
                    } else if (Date.now() - startTime > timeout) {
                        clearInterval(interval);
                        reject(`Timeout waiting for element ${selector}`);//超时则返回字符串
                    }//继续寻找
                }, intervalTime);
            });
        }
        waitForElements(selector, timeout = 3e4) {//查找eles
            return new Promise((resolve, reject) => {
                const intervalTime = 500;
                const startTime = Date.now();
                const interval = setInterval(() => {
                    const elements = document.querySelectorAll(selector);
                    if (elements) {
                        clearInterval(interval);
                        resolve(elements);//找到则返回函数
                    } else if (Date.now() - startTime > timeout) {
                        clearInterval(interval);
                        reject(`Timeout waiting for element ${selector}`);//超时则返回字符串
                    }//继续寻找
                }, intervalTime);
            });
        }
    }()
})(unsafeWindow);