MyJSCodeLibrary

my JavaScript Code Library

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         我的常用js代码库
// @namespace    http://tampermonkey.net/
// @version      0.64
// @description  我常用的js代码库
// @author       zyb
// @match        *://*/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// ==/UserScript==

class MyJSCodeLibrary {
    constructor() {

    }
    /**
     * 创建css样式
     * @param {string} styleStr css样式
     */
    createStyleFuc(styleStr = "") {
        // 创建style节点
        const style = document.createElement("style");
        style.setAttribute("type", "text/css");
        style.appendChild(document.createTextNode(styleStr));
        document.head.appendChild(style);
    }

    /**
     * 异步获取dom节点
     * @param {string} selector dom节点的选择器文本
     * @param {number} time 间隔时间
     * @returns
     */
    getDomByIntervalAsyncFuc(selector, time = 100) {
        let dom = document.querySelectorAll(selector)[0];
        let timeId = null;
        let times = 0;
        return new Promise((res) => {
            timeId = setInterval(() => {
                times++;
                if (dom || times > 10) {
                    res(dom);
                    clearInterval(timeId);
                } else {
                    dom = document.querySelectorAll(selector)[0];
                }
            }, time)
        })
    }

    /**
     * 异步获取dom节点
     * @param {string} selector dom节点的选择器文本
     * @param {number} time 间隔时间
     * @returns
     */
    getDomListByIntervalAsyncFuc(selector, time = 100) {
        let dom = document.querySelectorAll(selector)[0];
        let timeId = null;
        let times = 0;
        return new Promise((res) => {
            timeId = setInterval(() => {
                times++;
                if (dom.length || times > 10) {
                    res(dom);
                    clearInterval(timeId);
                } else {
                    dom = document.querySelectorAll(selector);
                }
            }, time)
        })
    }

    /**
     * 异步获取dom节点
     * @param {string} selector dom节点的选择器文本
     * @param {number} time 间隔时间
     * @returns
     */
    getDomByTimeoutAsyncFuc(selector = "", time = 2000) {
        return new Promise((res) => {
            setTimeout(() => {
                let dom = document.querySelectorAll(selector)[0];
                res(dom);
            }, time)
        })
    }

    /**
     * 异步获取dom节点
     * @param {string} selector dom节点的选择器文本
     * @param {number} time 间隔时间
     * @returns
     */
    getDomListByTimeoutAsyncFuc(selector = "", time = 2000) {
        return new Promise((res) => {
            setTimeout(() => {
                let dom = document.querySelectorAll(selector);
                res(dom);
            }, time)
        })
    }

    /**
     * 针对返回值为html时的解析函数
     * @param {Object} obj 请求头参数
     * 
     * {
     *      "charset": "gbk",
     *      "url": "https://www.88yydstxt426.com/s.php",
     *      "headers": {
     *          "content-type": "application/x-www-form-urlencoded",
     *      },
     *      "body": "objectType=2&type=articlename&s=%C3%C3%C3%C3",
     *      "method": "POST",
     * }
     */
    decodeHtmlAsyncFuc(obj = {}) {
        const url = obj.url || '';
        const headers = obj.headers || obj.header || {
            "content-type": "application/x-www-form-urlencoded",
        };
        const body = obj.body;
        const method = obj.method || 'GET';
        const charset = obj.charset || 'utf-8';
        let domArr = [];

        const decoder = new TextDecoder(charset);
        return fetch(url, {
            headers,
            body,
            method,
        }).then(response => {
            const reader = response.body.getReader();

            return reader.read().then(function process({ done, value }) {
                if (done) {
                    console.log('Stream finished');
                    return new Promise((res) => {
                        res(domArr);
                    });
                }

                // 解码数据
                const text = decoder.decode(value);
                const dom = document.createElement('div');
                dom.innerHTML = text;
                domArr.push(dom);

                return reader.read().then(process);
            });
        });
    }

    /**
     * 将值复制到剪贴板
     * @param {string} value 复制到剪贴板的值
     */
    copy(value = '') {
        if (!value) {
            return
        }
        navigator.clipboard.writeText(value);
    }

    /**
     * 监听函数,监听传入的对象的第一层字段值的变化
     */
    watch(dataObj, fuc) {
        const obj = { ...dataObj };

        //汇总对象中所有的属性形成一个数组
        const keys = Object.keys(dataObj);
        //遍历
        keys.forEach((k) => {
            Object.defineProperty(dataObj, k, {
                get() {
                    return obj[k];
                },
                set(val) {
                    console.log(`${k}被修改了,值为`, val);
                    fuc && fuc();
                    obj[k] = val;
                }
            })
        })
    }
}

if(!window.MyJSCodeLibrary){
    window.MyJSCodeLibrary = MyJSCodeLibrary;
}