上一主题 下一主题
ScriptCat,新一代的脚本管理器脚本站,与全世界分享你的用户脚本油猴脚本开发指南教程目录
返回列表 发新帖

B站自动化发弹幕功能,发送弹幕到第10条就停止了

[复制链接]

该用户从未签到

1

主题

5

回帖

23

积分

助理工程师

积分
23
发表于 2022-1-9 16:23:39 | 显示全部楼层 | 阅读模式

大佬们帮忙看看,主要代码块,跑的时候报这个错误Error: Promised response from onMessage listener went out of scope

var flag=true;
triple2.onclick=function(){
    var timer = setInterval(function() {
        if(flag){
            flag = false;
            let t=document.getElementsByClassName("bilibili-player-video-danmaku-input")[0];
            let evt = document.createEvent('HTMLEvents');
            evt.initEvent('input', true, true);
            t.value='1111';
            t.dispatchEvent(evt)

            evt.initEvent('keydown', true, false)
            evt = Object.assign(evt, {
                ctrlKey: false,
                metaKey: false,
                altKey: false,
                which: 13,
                keyCode: 13,
                key: 'Enter',
                code: 'Enter'
            })
            t.focus()
            t.dispatchEvent(evt)
            setTimeout(function() {
                flag = true;
            }, 6000);
        }
        console.log('刷了多少条: ' + num);
        num++
        setTimeout(function() {
            var m=document.getElementsByClassName("bilibili-player-video-danmaku-input")[0];
            if(m.value == "1111"){
                console.log("自己停止");
                clearInterval(timer);//跳出setInterval定时器
            }
        },3000)
        triple3.onclick=function(){
            //停止
            console.log("已经停止");
            clearInterval(timer);//跳出setInterval定时器
        }
    },6000*(ia+1))
}
  • TA的每日心情
    开心
    2023-2-28 23:59
  • 签到天数: 191 天

    [LV.7]常住居民III

    637

    主题

    5196

    回帖

    6078

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    6078

    荣誉开发者管理员油中2周年生态建设者喜迎中秋

    发表于 2022-1-9 17:23:40 | 显示全部楼层
    建议发个脚本...代码贴不全没法跑
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.net/a/lihengdao666
    个人宣言:この世界で私に胜てる人とコードはまだ生まれていません。死ぬのが怖くなければ来てください。
    回复

    使用道具 举报

    该用户从未签到

    1

    主题

    5

    回帖

    23

    积分

    助理工程师

    积分
    23
    发表于 2022-1-9 19:09:03 | 显示全部楼层
    李恒道 发表于 2022-1-9 17:23
    建议发个脚本...代码贴不全没法跑

    // ==UserScript==
    // @name         弹幕
    // @namespace    http://tampermonkey.net/
    // @version      0.1
    // @description  try to take over the world!
    // @author       You
    // @match        https://www.bilibili.com/*
    // @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
    // @require      https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js
    // @grant        none
    // ==/UserScript==
    /* globals jQuery, $, waitForKeyElements */
    //弹幕数组数据350条
    var data = [
       "1",
      "2",
      "3",
      "4",
      "5",
    ];
    var data2 = [
       "a",
      "b",
      "c",
      "d",
      "e",
    ];

    //下拉框
    let triple1=document.createElement("button");
    triple1.innerHTML='<div id="dg" style="position: fixed ! important; left: 0px; top: 240px"><select id="id001" style="width: 55px;padding:3px;background-color: #00A1D6;bd;color: #fff;-webkit-border-radius: 5px;border-radius: 5px;-khtml-border-radius: 5px;text-align: center;vertical-align: middle;border: 1px solid transparent;font-weight: 900;font-size:100%"><option value="1">普通</option><option value="2" >面试</option></select></div>'

    //开始
    let triple2=document.createElement("button");
    triple2.innerHTML='<div id="dg" style="position: fixed ! important; left: 0px; top: 150px"><button id="id002" type="button" style="width: 46px;padding:3px;background-color: #00A1D6;bd;color: #fff;-webkit-border-radius: 5px;border-radius: 5px;-khtml-border-radius: 5px;text-align: center;vertical-align: middle;border: 1px solid transparent;font-weight: 900;font-size:110%">开始</button></div>'

    //停止
    let triple3=document.createElement("button");
    triple3.innerHTML='<div id="dg" style="position: fixed ! important; left: 0px; top: 180px"><button id="id003" type="button" style="width: 46px;padding:3px;background-color: #00A1D6;bd;color: #fff;-webkit-border-radius: 5px;border-radius: 5px;-khtml-border-radius: 5px;text-align: center;vertical-align: middle;border: 1px solid transparent;font-weight: 900;font-size:110%">停止</button></div>'

    //长期
    let triple4=document.createElement("button");
    triple4.innerHTML='<div id="dg" style="position: fixed ! important; left: 0px; top: 210px"><button id="id004" type="button" style="width: 46px;padding:3px;background-color: #00A1D6;bd;color: #fff;-webkit-border-radius: 5px;border-radius: 5px;-khtml-border-radius: 5px;text-align: center;vertical-align: middle;border: 1px solid transparent;font-weight: 900;font-size:110%">长期</button></div>'

    var flag=true;
    var num = 1;
    function countNum() {
        if(flag){
            flag = false;
            var a = Math.floor(Math.random()*(4 + 1 - 0));
            let t=document.getElementsByClassName("bilibili-player-video-danmaku-input")[0];
            let evt = document.createEvent('HTMLEvents');
            evt.initEvent('input', true, true);
            //
            var myselect=document.getElementById("id001");
            var index=myselect.selectedIndex;
            if (myselect.options[index].value==1){
                t.value=data[a];
            }else if(myselect.options[index].value==2){
                t.value=data2[a];
            }
            t.dispatchEvent(evt);

            evt.initEvent('keydown', true, false)
            evt = Object.assign(evt, {
                ctrlKey: false,
                metaKey: false,
                altKey: false,
                which: 13,
                keyCode: 13,
                key: 'Enter',
                code: 'Enter'
            })
            t.focus()
            t.dispatchEvent(evt)
            setTimeout(function() {
                flag = true;
            }, 5000);
        }
        console.log('刷了多少条: ' + num);
        num++
    }
    //开始按钮
    triple2.onclick=function(){
        countNum();
        var ia=0;
        var timer = setInterval(function() {
            if(flag){
                flag = false;
                var a = Math.floor(Math.random()*(4 + 1 - 0));
                let t=document.getElementsByClassName("bilibili-player-video-danmaku-input")[0];
                let evt = document.createEvent('HTMLEvents');
                evt.initEvent('input', true, true);
                //
                var myselect=document.getElementById("id001");
                var index=myselect.selectedIndex;
                if (myselect.options[index].value==1){
                    t.value=data[a];
                }else if(myselect.options[index].value==2){
                    t.value=data2[a];
                }
                t.dispatchEvent(evt);

                evt.initEvent('keydown', true, false)
                evt = Object.assign(evt, {
                    ctrlKey: false,
                    metaKey: false,
                    altKey: false,
                    which: 13,
                    keyCode: 13,
                    key: 'Enter',
                    code: 'Enter'
                })
                t.focus()
                t.dispatchEvent(evt)
                setTimeout(function() {
                    flag = true;
                }, 5000);
            }
            console.log('刷了多少条: ' + num);
            num++
            setTimeout(function() {
                var m=document.getElementsByClassName("bilibili-player-video-danmaku-input")[0];
                console.log(m.value);
                if(m.value ==data[a]){
                    console.log("自己停止");
                    clearInterval(timer);//跳出setInterval定时器
                }
            },3000)
            triple3.onclick=function(){
                //停止
                console.log("已经停止");
                clearInterval(timer);//跳出setInterval定时器
            }
        },7000)
    }

    //长期
    triple4.onclick=function(){
        //countNum();
        var ia=0;
        var timer = setInterval(function() {
            var a = Math.floor(Math.random()*(4 + 1 - 0));
            let t=document.getElementsByClassName("bilibili-player-video-danmaku-input")[0];
            let evt = document.createEvent('HTMLEvents');
            evt.initEvent('input', true, true);
            //
            var myselect=document.getElementById("id001");
            var index=myselect.selectedIndex;
            if (myselect.options[index].value==1){
                t.value=data[a];
            }else if(myselect.options[index].value==2){
                t.value=data2[a];
            }
            t.dispatchEvent(evt);

            evt.initEvent('keydown', true, false)
            evt = Object.assign(evt, {
                ctrlKey: false,
                metaKey: false,
                altKey: false,
                which: 13,
                keyCode: 13,
                key: 'Enter',
                code: 'Enter'
            })
            t.focus()
            t.dispatchEvent(evt)
            console.log('刷了多少条: ' + num);
            num++
            setTimeout(function() {
                var m=document.getElementsByClassName("bilibili-player-video-danmaku-input")[0];
                console.log(m.value);
                if(m.value ==data[a]){
                    console.log("自己停止");
                    clearInterval(timer);//跳出setInterval定时器
                }
            },3000)
            triple2.onclick=function(){
                //停止
                console.log("已经停止");
                clearInterval(timer);//跳出setInterval定时器
            }
        },20000 * (ia+1))
        }

    //定时器延迟显示两个按钮
    setTimeout(function() {
        document.body.append(triple1);
        document.body.append(triple2);
        document.body.append(triple3);
        document.body.append(triple4);
    },3000)

    //开始按键
    triple2.onmouseover=function(){
        //鼠标移入按钮颜色
        triple2.style.backgroundColor='#1A73E8';
    }
    triple2.onmouseout=function(){
        //鼠标移出按钮颜色
        triple2.style.backgroundColor='#00A1D6';
    }

    //停止按键
    triple3.onmouseover=function(){
        //鼠标移入按钮颜色
        triple3.style.backgroundColor='#1A73E8';
    }
    triple3.onmouseout=function(){
        //鼠标移出按钮颜色
        triple3.style.backgroundColor='#00A1D6';
    }

    //长期按键
    triple4.onmouseover=function(){
        //鼠标移入按钮颜色
        triple4.style.backgroundColor='#1A73E8';
    }
    triple4.onmouseout=function(){
        //鼠标移出按钮颜色
        triple4.style.backgroundColor='#00A1D6';
    }
    //下拉框
    triple1.onmouseover=function(){
        //鼠标移入按钮颜色
        triple1.style.backgroundColor='#1A73E8';
    }
    triple1.onmouseout=function(){
        //鼠标移出按钮颜色
        triple1.style.backgroundColor='#00A1D6';
    }
    回复

    使用道具 举报

    该用户从未签到

    1

    主题

    5

    回帖

    23

    积分

    助理工程师

    积分
    23
    发表于 2022-1-9 19:10:07 | 显示全部楼层
    李恒道 发表于 2022-1-9 17:23
    建议发个脚本...代码贴不全没法跑

    发了大佬,初学者写的有点乱,麻烦了
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2023-2-28 23:59
  • 签到天数: 191 天

    [LV.7]常住居民III

    637

    主题

    5196

    回帖

    6078

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    6078

    荣誉开发者管理员油中2周年生态建设者喜迎中秋

    发表于 2022-1-9 19:38:06 | 显示全部楼层
    大概调了一下...感觉好像是触发了油猴的函数错误...可以考虑把绑定事件挪到外部不要放循环里,代码尽量干净一点看看....具体错误我也不知道
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.net/a/lihengdao666
    个人宣言:この世界で私に胜てる人とコードはまだ生まれていません。死ぬのが怖くなければ来てください。
    回复

    使用道具 举报

    该用户从未签到

    1

    主题

    5

    回帖

    23

    积分

    助理工程师

    积分
    23
    发表于 2022-1-9 19:54:14 | 显示全部楼层
    李恒道 发表于 2022-1-9 19:38
    大概调了一下...感觉好像是触发了油猴的函数错误...可以考虑把绑定事件挪到外部不要放循环里,代码尽量干净 ...

    好的,我试调一下
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2024-3-13 10:14
  • 签到天数: 211 天

    [LV.7]常住居民III

    294

    主题

    3906

    回帖

    3826

    积分

    管理员

    积分
    3826

    管理员荣誉开发者油中2周年生态建设者喜迎中秋油中3周年挑战者 lv2

    发表于 2022-1-10 00:18:32 | 显示全部楼层
    Firefox?edge没出现
    上不慕古,下不肖俗。为疏为懒,不敢为狂。为拙为愚,不敢为恶。/ 微信公众号:一之哥哥
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2023-2-28 23:59
  • 签到天数: 191 天

    [LV.7]常住居民III

    637

    主题

    5196

    回帖

    6078

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    6078

    荣誉开发者管理员油中2周年生态建设者喜迎中秋

    发表于 2022-1-10 10:04:26 | 显示全部楼层
    好像还真是...chrome没有这个错误,ff有
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.net/a/lihengdao666
    个人宣言:この世界で私に胜てる人とコードはまだ生まれていません。死ぬのが怖くなければ来てください。
    回复

    使用道具 举报

    该用户从未签到

    1

    主题

    5

    回帖

    23

    积分

    助理工程师

    积分
    23
    发表于 2022-1-20 16:37:42 | 显示全部楼层
    王一之 发表于 2022-1-10 00:18
    Firefox?edge没出现

    报错都是在firefox,edge没试过
    回复

    使用道具 举报

    该用户从未签到

    1

    主题

    5

    回帖

    23

    积分

    助理工程师

    积分
    23
    发表于 2022-1-20 16:38:50 | 显示全部楼层
    李恒道 发表于 2022-1-10 10:04
    好像还真是...chrome没有这个错误,ff有

    之前都没咋注意这个居然还与浏览器有关的
    回复

    使用道具 举报

    发表回复

    本版积分规则

    快速回复 返回顶部 返回列表