B站直播间定时发随机弹幕

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

目前為 2022-07-23 提交的版本,檢視 最新版本

// ==UserScript==
// @name            B站直播间定时发随机弹幕
// @namespace       https://live.bilibili.com/暂无主页
// @update          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
// @download        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
// @version         2.1.0
// @description     定时从设置的字幕中随机取出一条在B站直播间发送,需先登录B站账号
// @author          Gamyou
// @include         /https?:\/\/live\.bilibili\.com\/[blanc\/]?[^?]*?\d+\??.*/
// @icon            https://www.bilibili.com/favicon.ico
// @license         Apache License, Version 2.0
// @run-at          document-end
// @grant           GM_getValue
// @grant           GM_setValue
// @grant           GM_deleteValue
// @note            22-07-23 2.1.0 增加数据本地存储功能
// @note            22-07-16 2.0.0 实现脚本热更新,爸爸再也不用担心脚本更新不及时
// @note            22-07-16 1.2.3 修复设置弹幕时旧(初始)数据没有清空的BUG
// @note            22-06-24 1.2.2 优化部分代码
// @note            22-06-20 1.2.1 修复定时停止不能再开始的BUG
// @note            22-06-19 1.2.0 添加弹幕设置功能
// ==/UserScript==


window.onload = (function () {
    let objData = {
        data1: ['绿豆', '红豆', '黄豆', '蓝猫'],
        data2: ['红', '接天莲叶无穷碧,映日荷花别样红。'],
        data3: ['黄', '儿童急走追黄蝶,飞入菜花无处寻。'],
        data4: ['蓝', '沧海月明珠有泪,蓝田日暖玉生烟。'],
        data5: ['绿', '惟有绿荷红菡萏,卷舒开合任天真。']
    },
        count = 0,
        nocache = new Date().getTime(),
        url = "https://greasyfork.org/scripts/447937-bilibili-live-random-send/code/bilibili-live-random-send.js";
    const waitCount = 100,
        appendElements = (src) => {
            var head = document.head || document.getElementsByTagName('head')[0];
            if (src.indexOf("js") == -1) { //css
                var style = document.createElement('style');
                style.setAttribute("rel", "stylesheet");
                style.setAttribute("href", src);
                head.appendChild(style);
            } else {
                var script = document.createElement('script');
                script.type = "text/javascript";
                script.setAttribute("src", src);
                head.appendChild(script);
            }
        },
        setValues = (obj) => {
            if (obj) {
                objData = obj;
                GM_setValue('danmu', objData);
            }
        },
        deleteValues = (key) => {
            if (key) {
                GM_deleteValue(key);
            }
        };

    appendElements(url + "?" + nocache);
    let t = setInterval(() => {
        try {
            if (autoSendDanmuModuleLoaded) {
                clearInterval(t);
                storeDanmuData(setValues);
                removeDanmuData(deleteValues);
                setDanmuData(GM_getValue('danmu', objData));
            } else {
                ++count;
                if (waitCount <= count) {
                    clearInterval(t);
                    console.warn('弹幕脚本模块加载超时,请刷新重试');
                }
            }
        } catch (e) {
            ++count;
            if (waitCount <= count) {
                clearInterval(t);
                console.warn('弹幕脚本模块加载超时,请刷新重试');
            }
        }
    }, 300);
})();