b-live-random-send-诗词

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

当前为 2022-07-24 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/447936/1073539/b-live-random-send-%E8%AF%97%E8%AF%8D.js

  1. // ==UserScript==
  2. // @name b-live-random-send-诗词
  3. // @description 定时从设置的字幕中随机取出一条在B站直播间发送,需先登录B站账号
  4. // @author Gamyou
  5. // @version 1.2.4
  6. // @note 22-07-23 1.2.4 修复弹幕分组数据都为空时,旧数据重复合并的BUG
  7. // @note 22-07-16 1.2.3 修复设置弹幕时旧(初始)数据没有清空的BUG
  8. // @note 22-06-24 1.2.2 优化部分代码
  9. // @note 22-06-20 1.2.1 修复定时停止不能再开始的BUG
  10. // @note 22-06-19 1.2.0 添加弹幕设置功能
  11. // ==/UserScript==
  12.  
  13.  
  14. (function () {
  15. window.autoSendDanmuModuleLoaded = false;
  16. let source = {
  17. version: 2,
  18. data1: { available: true, values: [] },
  19. data2: { available: true, values: [] },
  20. data3: { available: true, values: [] },
  21. data4: { available: true, values: [] },
  22. data5: { available: true, values: [] }
  23. };
  24. let waiters = [], data = [], timer = null, signInContent = '签到';
  25. let storeDataCallback = (obj) => { }, removeDataCallback = (obj) => { };
  26. let count = 0, lastSent = 0, arrayIndex = 0, isRandom = true, default_timeout = 600; // 超时时间,默认10分钟
  27. let dmButtonSend, dmInput, divSetting, dataText1, dataText2, dataText3, dataText4, dataText5, spanApplyTip, rdCheckbox;
  28. const waitCount = 10,
  29. initCss = () => {
  30. let linkElement = document.createElement('link');
  31. linkElement.rel = 'stylesheet';
  32. linkElement.href = 'https://unpkg.com/element-ui@2.15.9/lib/theme-chalk/index.css';
  33. document.head.appendChild(linkElement);
  34.  
  35. // 图标库 https://ionic.io/ionicons
  36. // let scriptElement = document.createElement('script');
  37. // scriptElement.src = 'https://unpkg.com/ionicons@5.5.2/dist/ionicons.js';
  38. // document.head.appendChild(scriptElement);
  39.  
  40. let customerStyle = document.createElement('style');
  41. customerStyle.setAttribute('type', 'text/css');
  42. customerStyle.innerHTML = '.el-button{display:inline-block;line-height:1;white-space:nowrap;cursor:pointer;background:#FFF;border:1px solid #DCDFE6;color:#606266;-webkit-appearance:none;text-align:center;-webkit-box-sizing:border-box;box-sizing:border-box;outline:0;margin:0;-webkit-transition:.1s;transition:.1s;font-weight:500;padding:12px 20px;font-size:14px;border-radius:4px}.el-button.is-circle{border-radius:50%;padding:12px}.el-button--mini.is-circle{padding:3px;}.el-button:focus,.el-button:hover{color:#409EFF;border-color:#c6e2ff;background-color:#ecf5ff}.el-icon-close.is-circle{padding:5px;color:#ff0000;border:1px solid #ff0000;margin-left:20px;}.el-icon-check.is-circle{padding:5px;color:#0000ff;border:1px solid #0000ff;margin-left:20px;}.random-check{display:inline-block;}.random-check label{display:inline-block;vertical-align:middle;border:1px solid #bdc3c7;border-radius:60px;width:40px;height:18px;position:relative;transition:all .3s;}.random-check input{display:none;}.random-check label:before{width:14px;height:14px;content:"";display:inline-block;background-color:#bdc3c7;border-radius:100%;position:absolute;top:2px;left:4px;transition:all .3s;}.random-check :checked ~ label{background-color:#26b22b;border-color:#26b22b;}.random-check :checked ~ label:before{left:22px;background-color:#fff;}';
  43. document.head.appendChild(customerStyle);
  44. },
  45. getCurrentTimestamp = () => new Date().getTime(),
  46. send = (msg, index) => {
  47. let dmTextArea = document.getElementById('aside-area-vm').getElementsByClassName('chat-input border-box')[0];
  48. if (!dmTextArea) {
  49. alert('找不到输入弹幕文本框,请尝试刷新页面');
  50. return;
  51. }
  52.  
  53. let btnSend = document.getElementsByClassName('bl-button live-skin-highlight-button-bg live-skin-button-text bl-button--primary bl-button--small')[0];
  54. if (!btnSend) {
  55. alert('找不到发送按钮,请尝试刷新页面');
  56. return;
  57. }
  58. // if (getCurrentTimestamp() - lastSent < 5000) {
  59. // setTimeout(() => send(msg, index), 5000);
  60. // console.log('++++++++++++++++++++++> 发送间隔少于5秒,进入递归处理 <++++++++++++++++++++++');
  61. // } else {
  62. dmTextArea.value = msg;
  63. // 定义事件, 定义才可发送
  64. dmTextArea.dispatchEvent(new Event('input', { "bubbles": true, "cancelable": true }));
  65. btnSend.click();
  66. lastSent = getCurrentTimestamp();
  67. ++count;
  68. console.log('=================> ' + new Date().toLocaleString() + ' 弹幕发送成功 ' + count + ' 次,第【' + index + '】条数据 === ' + msg);
  69. // }
  70. },
  71. isNull = (str) => {
  72. if (!str || str == "") {
  73. return true;
  74. }
  75.  
  76. let regu = "^[ ]+$";
  77. let re = new RegExp(regu);
  78. return re.test(str);
  79. },
  80. randomSort = (arr) => {
  81. for (let i = 0; i < arr.length; i++) {
  82. const rdIndex = Math.floor(Math.random() * arr.length);
  83. const temp = arr[i];
  84. arr[i] = arr[rdIndex];
  85. arr[rdIndex] = temp;
  86. }
  87.  
  88. return arr;
  89. },
  90. clearWaiters = () => {
  91. for (let i = 0; i < waiters.length; i++) {
  92. clearInterval(waiters[i]);
  93. waiters[i] = null;
  94. }
  95.  
  96. waiters = [];
  97. },
  98. signIn = () => {
  99. let timestamp = new Date(new Date(new Date().setDate(new Date().getDate() + 1)).toDateString()).getTime() - getCurrentTimestamp();
  100. console.log('=================> 设置第二天凌晨签到定时器【' + timestamp + '】');
  101. setTimeout(() => {
  102. send(signInContent, 0);
  103. console.log('++++++++++++++++++++++> 完成签到发送,进入下一轮递归签到发送 <++++++++++++++++++++++');
  104. signIn();
  105. }, timestamp);
  106. },
  107. // selectRandom = () => isRandom = rdCheckbox.checked,
  108. openSetting = () => divSetting.style.display = 'block',
  109. closeSetting = () => {
  110. rdCheckbox.checked = isRandom;
  111. divSetting.style.display = 'none';
  112. },
  113. initData = () => {
  114. if (source.data1.values.length <= 0
  115. && source.data2.values.length <= 0
  116. && source.data3.values.length <= 0
  117. && source.data4.values.length <= 0
  118. && source.data5.values.length <= 0) {
  119. return data ? data : [];
  120. }
  121.  
  122. let result = [];
  123. result = source.data1.available ? result.concat(source.data1.values) : result;
  124. result = source.data2.available ? result.concat(source.data2.values) : result;
  125. result = source.data3.available ? result.concat(source.data3.values) : result;
  126. result = source.data4.available ? result.concat(source.data4.values) : result;
  127. result = source.data5.available ? result.concat(source.data5.values) : result;
  128. data = result;
  129. !rdCheckbox || rdCheckbox.checked ? data = randomSort(result) : arrayIndex = 0;
  130. },
  131. applySetting = () => {
  132. source.data1.values = isNull(dataText1.value) ? [] : dataText1.value.split('|');
  133. source.data2.values = isNull(dataText2.value) ? [] : dataText2.value.split('|');
  134. source.data3.values = isNull(dataText3.value) ? [] : dataText3.value.split('|');
  135. source.data4.values = isNull(dataText4.value) ? [] : dataText4.value.split('|');
  136. source.data5.values = isNull(dataText5.value) ? [] : dataText5.value.split('|');
  137. initData();
  138. storeDataCallback(source);
  139. isRandom = rdCheckbox.checked;
  140. spanApplyTip.style.display = 'block';
  141. setTimeout(() => {
  142. spanApplyTip.style.display = 'none';
  143. }, 1500);
  144. },
  145. danmu = () => {
  146. if (data.length < 1) {
  147. alert('请任意在一个分组里输入一条弹幕');
  148. return;
  149. }
  150. if (isRandom) {
  151. arrayIndex = Math.floor((Math.random() * data.length));
  152. }
  153.  
  154. send(data[arrayIndex], arrayIndex);
  155. ++arrayIndex;
  156. if (arrayIndex >= data.length)
  157. arrayIndex = 0;
  158. },
  159. offOrOn = () => {
  160. let timeout = 0;
  161. if (timer) {
  162. console.log('=================> ' + new Date().toLocaleString() + ' 停止发送弹幕');
  163. clearInterval(timer);
  164. timer = null;
  165. console.log('=================> ' + new Date().toLocaleString() + ' 停止成功!!');
  166. dmButtonSend.style.background = 'rgba(217,157,27,1)';
  167. dmButtonSend.textContent = '开始';
  168. dmInput.removeAttribute("disabled");
  169. } else {
  170. timeout = isNull(dmInput.value) ? default_timeout * 1000 : dmInput.value * 1000;
  171. danmu();
  172. console.log('=================> ' + new Date().toLocaleString() + ' 开启定时器');
  173. timer = setInterval(danmu, timeout);
  174. console.log('=================> ' + new Date().toLocaleString() + ' 开启成功!!');
  175. dmButtonSend.style.background = '#ff0000';
  176. dmButtonSend.textContent = '停止';
  177. dmInput.setAttribute('disabled', 'disabled');
  178. }
  179. },
  180. pageFullyLoaded = () => {
  181. let divButton = document.getElementsByClassName('bottom-actions p-relative')[0];
  182. if (!divButton) {
  183. console.error('没能找到发送按钮上层的div');
  184. --waitCount;
  185. if (0 >= waitCount) {
  186. clearWaiters();
  187. }
  188. return;
  189. }
  190. clearWaiters();
  191.  
  192. let divSettingTitle = document.createElement('div');
  193. divSettingTitle.textContent = '弹幕设置';
  194. divSettingTitle.style.textAlign = 'center';
  195. divSettingTitle.style.fontSize = '16px';
  196. divSettingTitle.style.fontWeight = '700';
  197. divSettingTitle.style.color = '#1c5adc';
  198. divSettingTitle.style.lineHeight = '30px';
  199.  
  200. let divTip = document.createElement('div');
  201. divTip.style.color = '#0b81cc';
  202. divTip.style.textAlign = 'center';
  203. divTip.style.fontStyle = 'italic';
  204. divTip.style.height = '25px';
  205. divTip.innerHTML = '任一分组内输入弹幕即可,多条用<span style="color:#dc6b07;margin:0 2px 0 4px;font-weight:700;font-style:normal;">竖线</span>分隔';
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213. let divText1 = document.createElement('div');
  214. divText1.textContent = '分组 1 :'
  215. divText1.style.fontSize = '14px';
  216. divText1.style.paddingLeft = '2px';
  217. divText1.style.color = '#12388d';
  218. // divText1.style.display = 'inline';
  219.  
  220. dataText1 = document.createElement('textarea');
  221. dataText1.style.width = '98%';
  222. dataText1.style.height = '100px';
  223. dataText1.style.margin = '1px 0 4px 0';
  224. dataText1.style.border = '0';
  225. dataText1.style.resize = 'none';
  226. dataText1.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  227.  
  228. let divText2 = document.createElement('div');
  229. divText2.textContent = '分组 2 :'
  230. divText2.style.fontSize = '14px';
  231. divText2.style.paddingLeft = '2px';
  232. divText2.style.color = '#12388d';
  233.  
  234. dataText2 = document.createElement('textarea');
  235. dataText2.style.width = '98%';
  236. dataText2.style.height = '100px';
  237. dataText2.style.margin = '1px 0 4px 0';
  238. dataText2.style.border = '0';
  239. dataText2.style.resize = 'none';
  240. dataText2.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  241.  
  242. let divText3 = document.createElement('div');
  243. divText3.textContent = '分组 3 :'
  244. divText3.style.fontSize = '14px';
  245. divText3.style.paddingLeft = '2px';
  246. divText3.style.color = '#12388d';
  247.  
  248. dataText3 = document.createElement('textarea');
  249. dataText3.style.width = '98%';
  250. dataText3.style.height = '100px';
  251. dataText3.style.margin = '1px 0 4px 0';
  252. dataText3.style.border = '0';
  253. dataText3.style.resize = 'none';
  254. dataText3.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  255.  
  256. let divText4 = document.createElement('div');
  257. divText4.textContent = '分组 4 :'
  258. divText4.style.fontSize = '14px';
  259. divText4.style.paddingLeft = '2px';
  260. divText4.style.color = '#12388d';
  261.  
  262. dataText4 = document.createElement('textarea');
  263. dataText4.style.width = '98%';
  264. dataText4.style.height = '100px';
  265. dataText4.style.margin = '1px 0 4px 0';
  266. dataText4.style.border = '0';
  267. dataText4.style.resize = 'none';
  268. dataText4.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  269.  
  270. let divText5 = document.createElement('div');
  271. divText5.textContent = '分组 5 :'
  272. divText5.style.fontSize = '14px';
  273. divText5.style.paddingLeft = '2px';
  274. divText5.style.color = '#12388d';
  275.  
  276. dataText5 = document.createElement('textarea');
  277. dataText5.style.width = '98%';
  278. dataText5.style.height = '100px';
  279. dataText5.style.margin = '1px 0 4px 0';
  280. dataText5.style.border = '0';
  281. dataText5.style.resize = 'none';
  282. dataText5.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  283.  
  284. let divContainer = document.createElement('div');
  285. // divContainer.style.height = 'calc(98% - 30px - 25px - 25px)';
  286. divContainer.style.height = 'calc(98% - 30px - 25px)';
  287. divContainer.appendChild(divText1);
  288. divContainer.appendChild(dataText1);
  289. divContainer.appendChild(divText2);
  290. divContainer.appendChild(dataText2);
  291. divContainer.appendChild(divText3);
  292. divContainer.appendChild(dataText3);
  293. divContainer.appendChild(divText4);
  294. divContainer.appendChild(dataText4);
  295. divContainer.appendChild(divText5);
  296. divContainer.appendChild(dataText5);
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303. let descCheckbox = document.createElement('span');
  304. descCheckbox.textContent = '随机';
  305. descCheckbox.title = '将合并所有分组数据,从中随机选出一条发送';
  306. descCheckbox.style.fontSize = '16px';
  307. descCheckbox.style.verticalAlign = 'middle';
  308. descCheckbox.style.marginRight = '4px';
  309. descCheckbox.style.color = '#095ca2';
  310.  
  311. rdCheckbox = document.createElement('input');
  312. rdCheckbox.type = 'checkbox';
  313. rdCheckbox.id = 'rdmCheckbox';
  314. rdCheckbox.checked = isRandom;
  315. // rdCheckbox.addEventListener('click', selectRandom);
  316.  
  317. let lblCheckbox = document.createElement('label');
  318. lblCheckbox.setAttribute('for', 'rdmCheckbox');
  319.  
  320. let divCheckbox = document.createElement('div');
  321. divCheckbox.classList.add('random-check');
  322. divCheckbox.style.marginLeft = '10px';
  323. divCheckbox.appendChild(descCheckbox);
  324. divCheckbox.appendChild(rdCheckbox);
  325. divCheckbox.appendChild(lblCheckbox);
  326.  
  327. spanApplyTip = document.createElement('span');
  328. spanApplyTip.textContent = '设置成功'
  329. spanApplyTip.style.fontSize = '16px';
  330. spanApplyTip.style.color = '#128712';
  331. spanApplyTip.style.display = 'none';
  332.  
  333. let divApplyTip = document.createElement('div');
  334. divApplyTip.style.textAlign = 'center';
  335. divApplyTip.style.display = 'inline-block';
  336. divApplyTip.style.verticalAlign = 'middle';
  337. divApplyTip.style.width = '34%';
  338. divApplyTip.appendChild(spanApplyTip);
  339.  
  340. let btnApplySetting = document.createElement('i');
  341. btnApplySetting.setAttribute('title', '应用');
  342. btnApplySetting.classList.add('el-button');
  343. btnApplySetting.classList.add('el-icon-check');
  344. btnApplySetting.classList.add('is-circle');
  345. btnApplySetting.addEventListener('click', applySetting);
  346.  
  347. let btnCloseSetting = document.createElement('i');
  348. btnCloseSetting.setAttribute('title', '关闭');
  349. btnCloseSetting.classList.add('el-button');
  350. btnCloseSetting.classList.add('el-icon-close');
  351. btnCloseSetting.classList.add('is-circle');
  352. btnCloseSetting.addEventListener('click', closeSetting);
  353.  
  354. let divSettingButton = document.createElement('div');
  355. divSettingButton.style.display = 'inline-block';
  356. divSettingButton.style.verticalAlign = 'middle';
  357. divSettingButton.appendChild(btnApplySetting);
  358. divSettingButton.appendChild(btnCloseSetting);
  359.  
  360. let divBottomContainer = document.createElement('div');
  361. divBottomContainer.style.width = '100%';
  362. divBottomContainer.style.height = '25px';
  363. divBottomContainer.style.margin = '4px 0 8px 0';
  364. // divBottomContainer.style.position = 'absolute';
  365. // divBottomContainer.style.bottom = '0';
  366. divBottomContainer.appendChild(divCheckbox);
  367. divBottomContainer.appendChild(divApplyTip);
  368. divBottomContainer.appendChild(divSettingButton);
  369. divContainer.appendChild(divBottomContainer);
  370.  
  371. divSetting = document.createElement('div');
  372. divSetting.style.backgroundColor = '#d4f2e0';
  373. divSetting.style.borderRadius = '2px';
  374. divSetting.style.width = '100%';
  375. divSetting.style.height = '100%';
  376. divSetting.style.overflowY = 'auto';
  377. divSetting.style.position = 'absolute';
  378. divSetting.style.left = '0';
  379. divSetting.style.top = '0';
  380. divSetting.style.zIndex = '999';
  381. divSetting.style.display = 'none';
  382. divSetting.appendChild(divSettingTitle);
  383. divSetting.appendChild(divTip);
  384. divSetting.appendChild(divContainer);
  385.  
  386. let asideAreaVm = document.getElementById('aside-area-vm');
  387. asideAreaVm.appendChild(divSetting);
  388.  
  389. dmButtonSend = document.createElement('button');
  390. dmButtonSend.textContent = '开始';
  391. dmButtonSend.style.minWidth = '65px';
  392. dmButtonSend.style.height = '24px';
  393. dmButtonSend.style.fontSize = '12px';
  394. dmButtonSend.style.borderRadius = '4px';
  395. dmButtonSend.style.color = '#ffffff';
  396. dmButtonSend.style.background = 'rgba(217,157,27,1)';
  397. dmButtonSend.style.border = '0';
  398. dmButtonSend.style.cursor = 'pointer';
  399. //dmButtonSend.onclick = function() { alert('Hello world');}
  400. dmButtonSend.addEventListener('click', offOrOn);
  401.  
  402. let beforeSpan = document.createElement('span');
  403. beforeSpan.textContent = '每';
  404. beforeSpan.style.color = '#ffffff';
  405. beforeSpan.style.fontSize = '12px';
  406. beforeSpan.style.marginLeft = '4px';
  407. beforeSpan.style.backgroundColor = '#ec6c1b';
  408.  
  409. dmInput = document.createElement('input');
  410. dmInput.value = default_timeout;
  411. dmInput.style.width = '25px';
  412. dmInput.style.height = '15px';
  413. dmInput.style.margin = '0 3px';
  414. dmInput.style.border = '0';
  415. dmInput.style.borderRadius = '3px';
  416. dmInput.setAttribute('oninput', "this.value = this.value.replace(/[^0-9]/g, '')");
  417.  
  418. let afterSpan = document.createElement('span');
  419. afterSpan.textContent = '秒发送';
  420. afterSpan.style.color = '#ffffff';
  421. afterSpan.style.fontSize = '12px';
  422. afterSpan.style.backgroundColor = '#ec6c1b';
  423. afterSpan.style.marginRight = '4px';
  424.  
  425. let iElement = document.createElement('i');
  426. iElement.classList.add('el-icon-setting');
  427.  
  428. let btnSetting = document.createElement('button');
  429. btnSetting.title = '设置';
  430. btnSetting.classList.add('el-button');
  431. btnSetting.classList.add('el-button--mini');
  432. btnSetting.classList.add('is-circle');
  433. btnSetting.addEventListener('click', openSetting);
  434. btnSetting.appendChild(iElement);
  435.  
  436. // let btnSetting = document.createElement('ion-icon');
  437. // btnSetting.setAttribute('name', 'settings-sharp');
  438. // btnSetting.classList.add('el-button');
  439. // btnSetting.classList.add('el-button--mini');
  440. // btnSetting.classList.add('is-circle');
  441. // btnSetting.addEventListener('click', openSetting);
  442.  
  443. let div = document.createElement('div');
  444. div.style.position = 'absolute';
  445. div.appendChild(dmButtonSend);
  446. div.appendChild(beforeSpan);
  447. div.appendChild(dmInput);
  448. div.appendChild(afterSpan);
  449. div.appendChild(btnSetting);
  450. divButton.appendChild(div);
  451.  
  452. window.autoSendDanmuModuleLoaded = true;
  453. },
  454. arrayInfo = () => console.info(data),
  455. setDanmuData = (obj) => {
  456. if (!obj) return;
  457. if (source.version === obj.version)
  458. source = obj;
  459. else {
  460. source.data1.values = obj.data1;
  461. source.data2.values = obj.data2;
  462. source.data3.values = obj.data3;
  463. source.data4.values = obj.data4;
  464. source.data5.values = obj.data5;
  465. storeDataCallback(source);
  466. }
  467.  
  468. dataText1.value = source.data1.values.join('|');
  469. dataText2.value = source.data2.values.join('|');
  470. dataText3.value = source.data3.values.join('|');
  471. dataText4.value = source.data4.values.join('|');
  472. dataText5.value = source.data5.values.join('|');
  473. initData();
  474. },
  475. storeDanmuData = (cb) => storeDataCallback = cb,
  476. removeDanmuData = (cb) => removeDataCallback = cb;
  477.  
  478. initCss();
  479. window.addEventListener("load", () => {
  480. waiters[waiters.length] = setInterval(pageFullyLoaded, 1500);
  481. window.arrayInfo = arrayInfo;
  482. window.setDanmuData = setDanmuData;
  483. window.storeDanmuData = storeDanmuData;
  484. window.removeDanmuData = removeDanmuData;
  485. signIn();
  486. });
  487. })();