xx2 发表于 2022-1-9 16:23:39

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

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

```javascript
var flag=true;
triple2.onclick=function(){
    var timer = setInterval(function() {
      if(flag){
            flag = false;
            let t=document.getElementsByClassName("bilibili-player-video-danmaku-input");
            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");
            if(m.value == "1111"){
                console.log("自己停止");
                clearInterval(timer);//跳出setInterval定时器
            }
      },3000)
      triple3.onclick=function(){
            //停止
            console.log("已经停止");
            clearInterval(timer);//跳出setInterval定时器
      }
    },6000*(ia+1))
}

```

李恒道 发表于 2022-1-9 17:23:40

建议发个脚本...代码贴不全没法跑

xx2 发表于 2022-1-9 19:09:03

李恒道 发表于 2022-1-9 17:23
建议发个脚本...代码贴不全没法跑

// ==UserScript==
// @name         弹幕
// @namespace    http://tampermonkey.net/
// @version      0.1
// @descriptiontry 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");
      let evt = document.createEvent('HTMLEvents');
      evt.initEvent('input', true, true);
      //
      var myselect=document.getElementById("id001");
      var index=myselect.selectedIndex;
      if (myselect.options.value==1){
            t.value=data;
      }else if(myselect.options.value==2){
            t.value=data2;
      }
      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");
            let evt = document.createEvent('HTMLEvents');
            evt.initEvent('input', true, true);
            //
            var myselect=document.getElementById("id001");
            var index=myselect.selectedIndex;
            if (myselect.options.value==1){
                t.value=data;
            }else if(myselect.options.value==2){
                t.value=data2;
            }
            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");
            console.log(m.value);
            if(m.value ==data){
                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");
      let evt = document.createEvent('HTMLEvents');
      evt.initEvent('input', true, true);
      //
      var myselect=document.getElementById("id001");
      var index=myselect.selectedIndex;
      if (myselect.options.value==1){
            t.value=data;
      }else if(myselect.options.value==2){
            t.value=data2;
      }
      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");
            console.log(m.value);
            if(m.value ==data){
                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';
}

xx2 发表于 2022-1-9 19:10:07

李恒道 发表于 2022-1-9 17:23
建议发个脚本...代码贴不全没法跑

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

李恒道 发表于 2022-1-9 19:38:06

大概调了一下...感觉好像是触发了油猴的函数错误...可以考虑把绑定事件挪到外部不要放循环里,代码尽量干净一点看看....具体错误我也不知道

xx2 发表于 2022-1-9 19:54:14

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

好的,我试调一下

王一之 发表于 2022-1-10 00:18:32

Firefox?edge没出现

李恒道 发表于 2022-1-10 10:04:26

好像还真是...chrome没有这个错误,ff有

xx2 发表于 2022-1-20 16:37:42

王一之 发表于 2022-1-10 00:18
Firefox?edge没出现

报错都是在firefox,edge没试过{:4_96:}

xx2 发表于 2022-1-20 16:38:50

李恒道 发表于 2022-1-10 10:04
好像还真是...chrome没有这个错误,ff有

之前都没咋注意这个居然还与浏览器有关的{:4_88:}
页: [1]
查看完整版本: B站自动化发弹幕功能,发送弹幕到第10条就停止了