b-live-random-send-诗词

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

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

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/447936/1073532/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.margin = '4px 0px';
  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.marginBottom = '8px';
  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 divTextContainer = document.createElement('div');
  285. divTextContainer.appendChild(divText1);
  286. divTextContainer.appendChild(dataText1);
  287. divTextContainer.appendChild(divText2);
  288. divTextContainer.appendChild(dataText2);
  289. divTextContainer.appendChild(divText3);
  290. divTextContainer.appendChild(dataText3);
  291. divTextContainer.appendChild(divText4);
  292. divTextContainer.appendChild(dataText4);
  293. divTextContainer.appendChild(divText5);
  294. divTextContainer.appendChild(dataText5);
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301. let descCheckbox = document.createElement('span');
  302. descCheckbox.textContent = '随机';
  303. descCheckbox.title = '将合并所有分组数据,从中随机选出一条发送';
  304. descCheckbox.style.fontSize = '16px';
  305. descCheckbox.style.verticalAlign = 'middle';
  306. descCheckbox.style.marginRight = '4px';
  307. descCheckbox.style.color = '#095ca2';
  308.  
  309. rdCheckbox = document.createElement('input');
  310. rdCheckbox.type = 'checkbox';
  311. rdCheckbox.id = 'rdmCheckbox';
  312. rdCheckbox.checked = isRandom;
  313. // rdCheckbox.addEventListener('click', selectRandom);
  314.  
  315. let lblCheckbox = document.createElement('label');
  316. lblCheckbox.setAttribute('for', 'rdmCheckbox');
  317.  
  318. let divCheckbox = document.createElement('div');
  319. divCheckbox.classList.add('random-check');
  320. divCheckbox.style.marginLeft = '10px';
  321. divCheckbox.appendChild(descCheckbox);
  322. divCheckbox.appendChild(rdCheckbox);
  323. divCheckbox.appendChild(lblCheckbox);
  324.  
  325. spanApplyTip = document.createElement('span');
  326. spanApplyTip.textContent = '设置成功'
  327. spanApplyTip.style.fontSize = '16px';
  328. spanApplyTip.style.color = '#128712';
  329. spanApplyTip.style.display = 'none';
  330.  
  331. let divApplyTip = document.createElement('div');
  332. divApplyTip.style.textAlign = 'center';
  333. divApplyTip.style.display = 'inline-block';
  334. divApplyTip.style.verticalAlign = 'middle';
  335. divApplyTip.style.width = '34%';
  336. divApplyTip.appendChild(spanApplyTip);
  337.  
  338. let btnApplySetting = document.createElement('i');
  339. btnApplySetting.setAttribute('title', '应用');
  340. btnApplySetting.classList.add('el-button');
  341. btnApplySetting.classList.add('el-icon-check');
  342. btnApplySetting.classList.add('is-circle');
  343. btnApplySetting.addEventListener('click', applySetting);
  344.  
  345. let btnCloseSetting = document.createElement('i');
  346. btnCloseSetting.setAttribute('title', '关闭');
  347. btnCloseSetting.classList.add('el-button');
  348. btnCloseSetting.classList.add('el-icon-close');
  349. btnCloseSetting.classList.add('is-circle');
  350. btnCloseSetting.addEventListener('click', closeSetting);
  351.  
  352. let divSettingButton = document.createElement('div');
  353. divSettingButton.style.display = 'inline-block';
  354. divSettingButton.style.verticalAlign = 'middle';
  355. divSettingButton.appendChild(btnApplySetting);
  356. divSettingButton.appendChild(btnCloseSetting);
  357.  
  358. let divBottomContainer = document.createElement('div');
  359. divBottomContainer.style.width = '100%';
  360. divBottomContainer.style.height = '25px';
  361. divBottomContainer.style.margin = '4px 0 8px 0';
  362. // divBottomContainer.style.position = 'absolute';
  363. // divBottomContainer.style.bottom = '0';
  364. divBottomContainer.appendChild(divCheckbox);
  365. divBottomContainer.appendChild(divApplyTip);
  366. divBottomContainer.appendChild(divSettingButton);
  367.  
  368. divSetting = document.createElement('div');
  369. divSetting.style.backgroundColor = '#d4f2e0';
  370. divSetting.style.borderRadius = '2px';
  371. divSetting.style.width = '100%';
  372. divSetting.style.height = '100%';
  373. divSetting.style.overflowY = 'auto';
  374. divSetting.style.position = 'absolute';
  375. divSetting.style.left = '0';
  376. divSetting.style.top = '0';
  377. divSetting.style.zIndex = '999';
  378. divSetting.style.display = 'none';
  379. divSetting.appendChild(divSettingTitle);
  380. divSetting.appendChild(divTip);
  381. divSetting.appendChild(divTextContainer);
  382. divSetting.appendChild(divBottomContainer);
  383.  
  384. let asideAreaVm = document.getElementById('aside-area-vm');
  385. asideAreaVm.appendChild(divSetting);
  386.  
  387. dmButtonSend = document.createElement('button');
  388. dmButtonSend.textContent = '开始';
  389. dmButtonSend.style.minWidth = '65px';
  390. dmButtonSend.style.height = '24px';
  391. dmButtonSend.style.fontSize = '12px';
  392. dmButtonSend.style.borderRadius = '4px';
  393. dmButtonSend.style.color = '#ffffff';
  394. dmButtonSend.style.background = 'rgba(217,157,27,1)';
  395. dmButtonSend.style.border = '0';
  396. dmButtonSend.style.cursor = 'pointer';
  397. //dmButtonSend.onclick = function() { alert('Hello world');}
  398. dmButtonSend.addEventListener('click', offOrOn);
  399.  
  400. let beforeSpan = document.createElement('span');
  401. beforeSpan.textContent = '每';
  402. beforeSpan.style.color = '#ffffff';
  403. beforeSpan.style.fontSize = '12px';
  404. beforeSpan.style.marginLeft = '4px';
  405. beforeSpan.style.backgroundColor = '#ec6c1b';
  406.  
  407. dmInput = document.createElement('input');
  408. dmInput.value = default_timeout;
  409. dmInput.style.width = '25px';
  410. dmInput.style.height = '15px';
  411. dmInput.style.margin = '0 3px';
  412. dmInput.style.border = '0';
  413. dmInput.style.borderRadius = '3px';
  414. dmInput.setAttribute('oninput', "this.value = this.value.replace(/[^0-9]/g, '')");
  415.  
  416. let afterSpan = document.createElement('span');
  417. afterSpan.textContent = '秒发送';
  418. afterSpan.style.color = '#ffffff';
  419. afterSpan.style.fontSize = '12px';
  420. afterSpan.style.backgroundColor = '#ec6c1b';
  421. afterSpan.style.marginRight = '4px';
  422.  
  423. let iElement = document.createElement('i');
  424. iElement.classList.add('el-icon-setting');
  425.  
  426. let btnSetting = document.createElement('button');
  427. btnSetting.title = '设置';
  428. btnSetting.classList.add('el-button');
  429. btnSetting.classList.add('el-button--mini');
  430. btnSetting.classList.add('is-circle');
  431. btnSetting.addEventListener('click', openSetting);
  432. btnSetting.appendChild(iElement);
  433.  
  434. // let btnSetting = document.createElement('ion-icon');
  435. // btnSetting.setAttribute('name', 'settings-sharp');
  436. // btnSetting.classList.add('el-button');
  437. // btnSetting.classList.add('el-button--mini');
  438. // btnSetting.classList.add('is-circle');
  439. // btnSetting.addEventListener('click', openSetting);
  440.  
  441. let div = document.createElement('div');
  442. div.style.position = 'absolute';
  443. div.appendChild(dmButtonSend);
  444. div.appendChild(beforeSpan);
  445. div.appendChild(dmInput);
  446. div.appendChild(afterSpan);
  447. div.appendChild(btnSetting);
  448. divButton.appendChild(div);
  449.  
  450. window.autoSendDanmuModuleLoaded = true;
  451. },
  452. arrayInfo = () => console.info(data),
  453. setDanmuData = (obj) => {
  454. if (!obj) return;
  455. if (source.version === obj.version)
  456. source = obj;
  457. else {
  458. source.data1.values = obj.data1;
  459. source.data2.values = obj.data2;
  460. source.data3.values = obj.data3;
  461. source.data4.values = obj.data4;
  462. source.data5.values = obj.data5;
  463. storeDataCallback(source);
  464. }
  465.  
  466. dataText1.value = source.data1.values.join('|');
  467. dataText2.value = source.data2.values.join('|');
  468. dataText3.value = source.data3.values.join('|');
  469. dataText4.value = source.data4.values.join('|');
  470. dataText5.value = source.data5.values.join('|');
  471. initData();
  472. },
  473. storeDanmuData = (cb) => storeDataCallback = cb,
  474. removeDanmuData = (cb) => removeDataCallback = cb;
  475.  
  476. initCss();
  477. window.addEventListener("load", () => {
  478. waiters[waiters.length] = setInterval(pageFullyLoaded, 1500);
  479. window.arrayInfo = arrayInfo;
  480. window.setDanmuData = setDanmuData;
  481. window.storeDanmuData = storeDanmuData;
  482. window.removeDanmuData = removeDanmuData;
  483. signIn();
  484. });
  485. })();