b-live-random-send-test

定时从设置的字幕中随机取出一条在B站直播间发送,需先登录B站账号

目前為 2023-10-21 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// 1.5.8

const blobURL = URL.createObjectURL(
        new Blob(
            [
                '(',
                function () {
                    const ids = {};
                    // 监听message 开始执行定时器或者销毁
                    self.onmessage = (e) => {
                        switch (e.data.command) {
                            case 'interval:start': // 开启定时器
                                const intervalId = setInterval(() => postMessage({ message: 'interval:tick', id: e.data.id }), e.data.interval);
                                // postMessage({ message: 'interval:started', id: e.data.id });
                                ids[e.data.id] = intervalId;
                                break;
                            case 'interval:clear': // 销毁
                                clearInterval(ids[e.data.id]);
                                postMessage({ message: 'interval:cleared', id: e.data.id });
                                delete ids[e.data.id];
                                break;
                            case 'timeout:start':
                                const timeoutId = setTimeout(() => postMessage({ message: 'timeout:tick', id: e.data.id }), e.data.timeout);
                                // postMessage({ message: 'timeout:started', id: e.data.id });
                                ids[e.data.id] = timeoutId;
                                break;
                            case 'timeout:clear':
                                clearTimeout(ids[e.data.id]);
                                postMessage({ message: 'timeout:cleared', id: e.data.id });
                                delete ids[e.data.id];
                                break;
                        }
                    };
                }.toString(),
                ')()',
            ],
            { type: 'application/javascript' },
        ),
    );
    const worker = new Worker(blobURL);
    URL.revokeObjectURL(blobURL); //用完释放URL对象
    const workerTimer = {
        id: 0,
        callbacks: {},
        setInterval: (cb, interval, context) => {
            const id = ++workerTimer.id;
            workerTimer.callbacks[id] = { fn: cb, context: context };
            worker.postMessage({ command: 'interval:start', interval: interval, id: id });
            return id;
        },
        setTimeout: (cb, timeout, context) => {
            const id = ++workerTimer.id;
            workerTimer.callbacks[id] = { fn: cb, context: context };
            worker.postMessage({ command: 'timeout:start', timeout: timeout, id: id });
            return id;
        },

        // 监听worker 里面的定时器发送的message 然后执行回调函数
        onMessage: (e) => {
            switch (e.data.message) {
                case 'interval:tick':
                case 'timeout:tick':
                    const callbackItem = workerTimer.callbacks[e.data.id];
                    if (callbackItem && callbackItem.fn) {
                        callbackItem.fn.apply(callbackItem.context);
                    }

                    break;
                case 'interval:cleared':
                case 'timeout:cleared':
                    delete workerTimer.callbacks[e.data.id];
                    break;
            }
        },

        // 往worker里面发送销毁指令
        clearInterval: (id) => worker.postMessage({ command: 'interval:clear', id: id }),
        clearTimeout: (id) => worker.postMessage({ command: 'timeout:clear', id: id }),
    };
    worker.onmessage = workerTimer.onMessage.bind(workerTimer);