myUtils

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

目前為 2022-04-27 提交的版本,檢視 最新版本

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         myUtils
// @description  自分用、dom取得等の機能追加。
// @include        *
// @noframes
// ==/UserScript==
class my{
    //dependency:myDomAppendBro
    static addStyle(css,className=void(0)){
        this.domAppendBro('style',document.head,css,`type`,'text/css',`class`,className)
    };
    //my.domAppendSon('tag',document.body,'content','idk','true','data-s')
    static domAppendSon(tag,dom,content){
        if(!tag) return;
        const son=typeof tag=='string'?
              document.createElement(tag):tag instanceof HTMLElement?
              tag:void(0),len=arguments.length;
        if(dom instanceof HTMLElement)dom.append(son);
        if(content)son.append(document.createTextNode(content));
        if (len>3) {
            for(let i=3;i<len;i+=2){
                son.setAttribute([arguments[i]],arguments[i+1])
            }
        }return son;
    };
    //my.domAppendBro('div',document.body,'','suck','1','dick')
    static domAppendBro(tag,dom,content){
        if(!tag) return;
        const bro=typeof tag=='string'?
              document.createElement(tag):tag instanceof HTMLElement?
              tag:void(0),len=arguments.length;
        if(dom instanceof HTMLElement)dom.parentNode.insertBefore(bro,dom.nextElementSibling);
        if(content)bro.append(document.createTextNode(content));
        if (len>3) {
            for(let i=3;i<len;i+=2){
                bro.setAttribute([arguments[i]],arguments[i+1])
            }
        }return bro;
    };
    //enable or disable style with className
    static switchStyle(...styleClassNames){
        styleClassNames.forEach(e=>{
            const style=document.querySelectorAll('style.'+e).forEach(e=>{
                e.type?e.type='':e.type="text/css";
            });
        })
    };
    //my.seconds2date(new Date().getTime())
    static seconds2date(seconds){
        const date=new Date(seconds),year=date.getFullYear(),month=date.getMonth()+1,day=date.getDate();
        const hour=date.getHours(),minute=date.getMinutes(),second=date.getSeconds(),milliseconds=date.getMilliseconds(),currentTime=year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second+":"+milliseconds;
        return currentTime
    };
    //跳出登录框后再执行
    static 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();
    };
    static getValue(k,aDefault){
        const val = localStorage.getItem("XB_"+k)
        if (!val && 'undefined' != typeof aDefault) return aDefault;
        return val;
    };
    //setValue('k', 'v')
    static setValue(k, v){
        localStorage.setItem("XB_"+k, v);
    };
    //deleteValue('k')
    static deleteValue(k){
        if(k.indexOf("XB_")!=-1) localStorage.removeItem(k);
    };
    static listValues(){
        let list=[],j=0,k;
        for (let i=0; i<localStorage.length;i++) {
            k=localStorage.key(i);
            if(k.indexOf("XB_")!=-1) list[j++] = localStorage.key(i);
        }return list;
    };
    //dependency:myXbListValues
    static clearValues(){
        this.listValues().forEach(e=>{
            localStorage.removeItem(e)
        })
    };
    //get len of json obj
    static getJsonLen(jsonObj){
        let jsonLen=0, key;
        for (key in jsonObj) {
            if (jsonObj.hasOwnProperty(key)) jsonLen++;
        }return jsonLen;
    };
    //syn console.log
    static log(text){
        console.log(JSON.stringify(text))
    };
    //dependency:myAddStyle,myDomAppendSon
    //my.addBtns(()=>{},a=e=>{confirm(e.target.id)},function(e){prompt(e.target.outerHTML)},function test(e){return 1})
    static addBtns(...funcs){
        funcs.forEach(e=>{
            if(typeof e !='function') return console.log(e);
        });
        let myDiv=document.querySelector('#myBtnContainerDiv'),btns=[];
        if(!myDiv) myDiv=this.domAppendSon(document.body,'div','',`id`,'myBtnContainerDiv');
        this.addStyle(`#myBtnContainerDiv{z-index:1650729359811!important;position: fixed!important;right:0!important;top:0!important}`);
        for(let i=0;i<funcs.length;i++){
            let fname=funcs[i].name;
            if(!fname) fname="noname";
            const btn = this.domAppendSon(myDiv,'input','','value',fname,'type','button','class','myQuickBtn');
            btn.addEventListener('click',funcs[i]),btns[i]=btn;
        }return btns
    };
    //prevent document.title from being changed
    static fixTitle(){
        Object.defineProperty(document,"title",{
            writable:false
        });
    };
}