bilibili-live-random-send

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

Ce script ne devrait pas être installé directement. C'est une librairie créée pour d'autres scripts. Elle doit être inclus avec la commande // @require https://update.greasyfork.org/scripts/447937/1268113/bilibili-live-random-send.js

(function () {
    window.autoSendDanmuModuleLoaded = false;
    let pdata = {}, 
        moduleUrl = "https://gcore.jsdelivr.net/gh/gamyou/bilibili-live-random-send/prod/bilibili-live-random-send.js",
        parentUrl = 'https://greasyfork.org/scripts/446725-b%E7%AB%99%E7%9B%B4%E6%92%AD%E9%97%B4%E5%AE%9A%E6%97%B6%E5%8F%91%E9%9A%8F%E6%9C%BA%E5%BC%B9%E5%B9%95/code/B%E7%AB%99%E7%9B%B4%E6%92%AD%E9%97%B4%E5%AE%9A%E6%97%B6%E5%8F%91%E9%9A%8F%E6%9C%BA%E5%BC%B9%E5%B9%95.user.js',
        gmNotice = obj => { alert('请更新油猴脚本'); window.location.href = parentUrl; },
        getGmValue = (key, defaultValue) => { return null; },
        setGmValue = (key, obj) => { console.warn('===> No implementation "setGmValue" method.'); },
        delGmValue = key => { console.warn('===> No implementation "delGmValue" method.'); };
    const setGmGetValue = callback => getGmValue = callback,
        setGmSetValue = callback => setGmValue = callback,
        setGmDelValue = callback => delGmValue = callback,
        setGmNotice = callback => gmNotice = callback,
        setParentData = obj => pdata = obj,
        createScript = (scriptText) => {
            let script = document.getElementById(pdata.moduleId);
            if (script) {
                script.remove();
                script = null;
            }

            script = document.createElement('script');
            script.id = pdata.moduleId;
            script.type = 'text/javascript';
            script.text = scriptText;
            document.head.appendChild(script);
        },
        updateScript = () => {
            if (pdata.configKey) {
                let config = getGmValue(pdata.configKey, {});
                console.log('===> 进行脚本更新,最后更新时间:' + config.lastUpdate);
                const controller = new AbortController();
                const promise = fetch(moduleUrl, { method: 'GET', signal: controller.signal, cache: 'no-store' });
                const requestTimer = setTimeout(() => controller.abort(), 40e3);
                promise.then(response => {
                        if (response.ok) {
                            return response.text();
                        }
                    }).then(txt => {
                        if (txt) {
                            if (requestTimer) {clearTimeout(requestTimer);}
                            createScript(txt);
                            config.script = txt;
                            config.lastUpdate = new Date().toLocaleDateString() + "/" + new Date().getHours();;
                            setGmValue(pdata.configKey, config);
                            console.log('===> 脚本更新完成,10秒后自动刷新页面');
                            setTimeout(() => {
                                window.location.reload();
                            }, 10e3);
                        }
                    }).catch(error => console.error('===> 网络请求超时,或其它报错', error));
            }
        },
        runStart = () => {
            updateScript();
            if (!window.updateParentFlag) {
                window.updateParentFlag = true;
                window.location.href = parentUrl;
            }
        };

    window.updateParentFlag = false;
    window.setParentData = setParentData;
    window.setGmGetValue = setGmGetValue;
    window.setGmSetValue = setGmSetValue;
    window.setGmDelValue = setGmDelValue;
    window.setGmNotice = setGmNotice;
    window.runStart = runStart;
    window.autoSendDanmuModuleLoaded = true;
})();