myUtils

for offline

Versione datata 26/01/2023. Vedi la nuova versione l'ultima versione.

Questo script non dovrebbe essere installato direttamente. È una libreria per altri script da includere con la chiave // @require https://update.greasyfork.org/scripts/435697/1142257/myUtils.js

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         myUtils
// @run-at       document-start
// @match        *://*/*
// @description  for offline
// ==/UserScript==
my=new class {
    constructor(){//dependency:after,zone
        const e=document.head.nextElementSibling;
        if(e.hasAttribute('my-zone')) this.zone=e;
        else this.zone=this.after('div',document.head,'',`my-zone`,'',`class`,'fucking-zhihu-sucks');
        this.addStyle(`#my-btn-container{display:flex;flex-wrap:wrap;z-index:1650729359811;position:fixed!important;right:0;top:0}.my-btn{user-select: none!important}`,'my-css-for-btns fucking-zhihu-sucks');
    }

    addStyle(css,className='fucking-zhihu-sucks'){//dependency:after,zone
        return this.append('style',this.zone,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;
        dom instanceof EventTarget?dom.append(son):0;
        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;
        dom instanceof EventTarget?dom.after(bro):0;
        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;
        dom instanceof EventTarget?dom.before(bro):0;
        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')

    switchStyle(...styleQueryRule){//enable or disable styleTag found in my.zone
        styleQueryRule.forEach(e=>{
            this.zone.querySelectorAll('style'+e).forEach(e=>{
                e.type!='0'?e.type=0:e.type="";
            });
        })
    }
    switchDisplay(...queryRule){//show or hide elements found in my.zone
        queryRule.forEach(e=>{
            this.zone.querySelectorAll(e).forEach(e=>{
                e.style.display=='none'?e.style.display='initial':e.style.display='none';
            });
        })
    }
    
    seconds2date(seconds){
        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.seconds2date(new Date().getTime())

    fuckTiebaLogin(userName,password){//sign in tieba
        document.getElementById('TANGRAM__PSP_5__footerULoginBtn').click();
        document.getElementById('TANGRAM__PSP_5__userName').outerHTML+='';
        document.getElementById('TANGRAM__PSP_5__password').outerHTML+='';
        document.getElementById('TANGRAM__PSP_5__userName').value=userName;
        document.getElementById('TANGRAM__PSP_5__password').value=password;
        return document.getElementById('TANGRAM__PSP_5__submit').click();
    }

    getValue(k,aDefault){
        const val=localStorage.getItem("XB_"+k)
        if (!val && 'undefined' != typeof aDefault) return aDefault;
        return val;
    }

    setValue(k, v){
        localStorage.setItem("XB_"+k, v);
    }//setValue('k', 'v')

    deleteValue(k){
        if(k.indexOf("XB_")!=-1) localStorage.removeItem(k);
    }//deleteValue('k')

    listValues(){
        let list=[],j=0,k,l=localStorage.length;
        for (let i=0;i<l;i++) {
            k=localStorage.key(i);
            if(k.indexOf("XB_")!=-1) list[j++]=localStorage.key(i)
        }return list
    }

    clearValues(){//dependency:listValues
        this.listValues().forEach(e=>{
            localStorage.removeItem(e)
        })
    }//get len of json obj

    getJsonLen(jsonObj){
        let jsonLen=0, key;
        for (key in jsonObj) {
            if (jsonObj.hasOwnProperty(key)) jsonLen++;
        }return jsonLen;
    }

    log(text){//syn console.log
        console.log(JSON.stringify(text))
    }

    addBtns(...params){//dependency:addStyle,append
        const z=this.zone,myDiv=z.querySelector('#my-btn-container')||this.append('div',z,'',`id`,'my-btn-container',`class`,'fucking-zhihu-sucks'),btns=[],l=params.length;
        for(let i=0;i<l;i+=2){
            const btn= params[i]==""?
                  this.append('button',myDiv,params[i+1].name+'|','class','my-btn').addEventListener('click',params[i+1]):
            this.append('button',myDiv,params[i]+'|','class','my-btn').addEventListener('click',params[i+1]);
            btns[btns.length]=btn
        }return btns
    }//my.addBtns(()=>{},"func1",e=>{confirm(e.target.id)},function(e){prompt(e.target.outerHTML)},function test(e){return 1})

    addAs(...args){//dependency:addStyle,append
        const z=this.zone,myDiv=z.querySelector('#my-btn-container')||this.append('div',z,'',`id`,'my-btn-container',`class`,'fucking-zhihu-sucks');
        for(let i=0;i<args.length;i+=2){
            this.append('a',myDiv,args[i]+'|',`href`,args[i+1]);
        }
    }//my.addAs("bilibili","https://www.bilibili.com/","baidu","https://www.baidu.com/")

    fixTitle(){//stop title be changed
        Object.defineProperty(document,"title",{
            writable:false
        })
    }

    fixConsoleLog(){//stop console be used
        this.cl=console.log;
        console.log=()=>{};
        Object.defineProperty(console,"log",{
            writable:false
        })
    }

}