myUtils

自分用、dom取得等の機能追加。

Tính đến 14-06-2022. Xem phiên bản mới nhất.

Script này sẽ không được không được cài đặt trực tiếp. Nó là một thư viện cho các script khác để bao gồm các chỉ thị meta // @require https://update.greasyfork.org/scripts/435697/1060763/myUtils.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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

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

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

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

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         myUtils
// @description  自分用、dom取得等の機能追加。
// @include        *
// @noframes
// ==/UserScript==
window.my=new class {
    constructor(){
       Element.prototype.on=function(){
          addEventListener.apply(this,arguments);
          return this
       }
       const e=document.head.nextElementSibling;
       if(e.hasAttribute('my-zone')) this.zone=e;
       else{
           const z=document.createElement('div');
           z.setAttribute('my-zone','');
           document.head.after(z);
           this.zone=z
       }
    }
 
    //dependency:after,zone
    addStyle(css,className=void(0)){
        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 HTMLElement||DocumentFragment?
              tag:void(0);
        let len=arguments.length;
        if(dom instanceof HTMLElement||DocumentFragment)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 HTMLElement||DocumentFragment?
              tag:void(0);
        let len=arguments.length;
        if(dom instanceof HTMLElement||DocumentFragment)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 HTMLElement||DocumentFragment?
              tag:void(0);
        let len=arguments.length;
        if(dom instanceof HTMLElement||DocumentFragment)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')

    //enable or disable style with class
    switchStyle(...styleClassStrs){
        styleClassStrs.forEach(e=>{
            this.zone.querySelectorAll('style.'+e).forEach(e=>{
                e.type!='0'?e.type=0:e.type="";
            });
        })
    }

    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){
        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
    }

    //dependency:listValues
    clearValues(){
        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;
    }

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

    //dependency:addStyle,append
    addBtns(...params){
        const z=this.zone,myDiv=z.querySelector('#my-btn-container')||this.append('div',z,'',`id`,'my-btn-container',`class`,void(0)),btns=[],l=params.length;
        if(!z.querySelector('style.my-css'))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 undefined');
        for(let i=0;i<l;){
            const btn=typeof params[i]==="function"?
            this.append('button',myDiv,params[i].name+'|','class','my-btn').on('click',params[i++]):
            this.append('button',myDiv,params[i++]+'|','class','my-btn').on('click',params[i++]);
            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})

    //dependency:addStyle,append
    addAs(...args){
        const z=this.zone,myDiv=z.querySelector('#my-btn-container')||this.append('div',document.body,'',`id`,'my-btn-container',`class`,void(0));
        if(!z.querySelector('style.my-css')) 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 undefined');
        for(let i=0;i<args.length;i+=2){
            this.append('a',myDiv,args[i]+'|',`href`,args[i+1]);
        }
    }
    //my.addAs("哔哩哔哩","https://www.bilibili.com/","百度","https://www.baidu.com/")

    //prevent document.title from being changed
    fixTitle(){
        Object.defineProperty(document,"title",{
            writable:false
        })
    }
}