b-live-random-send-诗词

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

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

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/447936/1073574/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.5
  6. // @note 22-07-24 1.2.5 优化设置面板样式
  7. // @note 22-07-23 1.2.4 修复弹幕分组数据都为空时,旧数据重复合并的BUG
  8. // @note 22-07-16 1.2.3 修复设置弹幕时旧(初始)数据没有清空的BUG
  9. // @note 22-06-24 1.2.2 优化部分代码
  10. // @note 22-06-20 1.2.1 修复定时停止不能再开始的BUG
  11. // @note 22-06-19 1.2.0 添加弹幕设置功能
  12. // ==/UserScript==
  13.  
  14.  
  15. (function () {
  16. window.autoSendDanmuModuleLoaded = false;
  17. let source = {
  18. version: 2,
  19. data1: { available: true, values: ['绿豆', '红豆', '黄豆', '蓝猫'] },
  20. data2: { available: true, values: ['红', '接天莲叶无穷碧,映日荷花别样红。'] },
  21. data3: { available: true, values: ['黄', '儿童急走追黄蝶,飞入菜花无处寻。'] },
  22. data4: { available: true, values: ['蓝', '沧海月明珠有泪,蓝田日暖玉生烟。'] },
  23. data5: { available: true, values: ['绿', '惟有绿荷红菡萏,卷舒开合任天真。'] }
  24. };
  25. let waiters = [], data = [], timer = null, signInContent = '签到';
  26. let storeDataCallback = (obj) => { }, removeDataCallback = (obj) => { };
  27. let count = 0, lastSent = 0, arrayIndex = 0, isRandom = true, default_timeout = 600;
  28. let dmButtonSend, dmInput, divSetting, dataText1, dataText2, dataText3, dataText4, dataText5, spanApplyTip, rdCheckbox;
  29. const waitCount = 10,
  30. initCss = () => {
  31. let linkElement = document.createElement('link');
  32. linkElement.rel = 'stylesheet';
  33. linkElement.href = 'https://unpkg.com/element-ui@2.15.9/lib/theme-chalk/index.css';
  34. document.head.appendChild(linkElement);
  35.  
  36. // 图标库 https://ionic.io/ionicons
  37. // let scriptElement = document.createElement('script');
  38. // scriptElement.src = 'https://unpkg.com/ionicons@5.5.2/dist/ionicons.js';
  39. // document.head.appendChild(scriptElement);
  40.  
  41. let customerStyle = document.createElement('style');
  42. customerStyle.setAttribute('type', 'text/css');
  43. customerStyle.innerHTML = '.danmu_group_title{font-size:14px;padding-left:2px;color:rgb(18, 56, 141);}.danmu_group_textarea{width:98%;min-height:100px;height:16%;margin:1px 0px 4px;border:0px;resize:none;}.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;}';
  44. document.head.appendChild(customerStyle);
  45. },
  46. getCurrentTimestamp = () => new Date().getTime(),
  47. send = (msg, index) => {
  48. let dmTextArea = document.getElementById('aside-area-vm').getElementsByClassName('chat-input border-box')[0];
  49. if (!dmTextArea) {
  50. alert('找不到输入弹幕文本框,请尝试刷新页面');
  51. return;
  52. }
  53.  
  54. let btnSend = document.getElementsByClassName('bl-button live-skin-highlight-button-bg live-skin-button-text bl-button--primary bl-button--small')[0];
  55. if (!btnSend) {
  56. alert('找不到发送按钮,请尝试刷新页面');
  57. return;
  58. }
  59. // if (getCurrentTimestamp() - lastSent < 5000) {
  60. // setTimeout(() => send(msg, index), 5000);
  61. // console.log('++++++++++++++++++++++> 发送间隔少于5秒,进入递归处理 <++++++++++++++++++++++');
  62. // } else {
  63. dmTextArea.value = msg;
  64. // 定义事件, 定义才可发送
  65. dmTextArea.dispatchEvent(new Event('input', { "bubbles": true, "cancelable": true }));
  66. btnSend.click();
  67. lastSent = getCurrentTimestamp();
  68. ++count;
  69. console.log('=================> ' + new Date().toLocaleString() + ' 弹幕发送成功 ' + count + ' 次,第【' + index + '】条数据 === ' + msg);
  70. // }
  71. },
  72. isNull = (str) => {
  73. if (!str || str == "")
  74. return true;
  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. send(data[arrayIndex], arrayIndex);
  154. ++arrayIndex;
  155. if (arrayIndex >= data.length)
  156. arrayIndex = 0;
  157. },
  158. offOrOn = () => {
  159. let timeout = 0;
  160. if (timer) {
  161. console.log('=================> ' + new Date().toLocaleString() + ' 停止发送弹幕');
  162. clearInterval(timer);
  163. timer = null;
  164. console.log('=================> ' + new Date().toLocaleString() + ' 停止成功!!');
  165. dmButtonSend.style.background = 'rgba(217,157,27,1)';
  166. dmButtonSend.textContent = '开始';
  167. dmInput.removeAttribute("disabled");
  168. } else {
  169. timeout = isNull(dmInput.value) ? default_timeout * 1000 : dmInput.value * 1000;
  170. danmu();
  171. console.log('=================> ' + new Date().toLocaleString() + ' 开启定时器');
  172. timer = setInterval(danmu, timeout);
  173. console.log('=================> ' + new Date().toLocaleString() + ' 开启成功!!');
  174. dmButtonSend.style.background = '#ff0000';
  175. dmButtonSend.textContent = '停止';
  176. dmInput.setAttribute('disabled', 'disabled');
  177. }
  178. },
  179. pageFullyLoaded = () => {
  180. let divButton = document.getElementsByClassName('bottom-actions p-relative')[0];
  181. if (!divButton) {
  182. console.error('没能找到发送按钮上层的div');
  183. --waitCount;
  184. if (0 >= waitCount) {
  185. clearWaiters();
  186. }
  187. return;
  188. }
  189. clearWaiters();
  190.  
  191. /* ----------------------------------------- head ----------------------------------------- */
  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. /* ----------------------------------------- head ----------------------------------------- */
  207.  
  208. /* ----------------------------------------- textarea ----------------------------------------- */
  209. let divText1 = document.createElement('div');
  210. divText1.textContent = '分组 1 :'
  211. divText1.classList.add('danmu_group_title');
  212.  
  213. dataText1 = document.createElement('textarea');
  214. dataText1.classList.add('danmu_group_textarea');
  215. dataText1.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  216.  
  217. let divText2 = document.createElement('div');
  218. divText2.textContent = '分组 2 :'
  219. divText2.classList.add('danmu_group_title');
  220.  
  221. dataText2 = document.createElement('textarea');
  222. dataText2.classList.add('danmu_group_textarea');
  223. dataText2.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  224.  
  225. let divText3 = document.createElement('div');
  226. divText3.textContent = '分组 3 :'
  227. divText3.classList.add('danmu_group_title');
  228.  
  229. dataText3 = document.createElement('textarea');
  230. dataText3.classList.add('danmu_group_textarea');
  231. dataText3.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  232.  
  233. let divText4 = document.createElement('div');
  234. divText4.textContent = '分组 4 :'
  235. divText4.classList.add('danmu_group_title');
  236.  
  237. dataText4 = document.createElement('textarea');
  238. dataText4.classList.add('danmu_group_textarea');
  239. dataText4.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  240.  
  241. let divText5 = document.createElement('div');
  242. divText5.textContent = '分组 5 :'
  243. divText5.classList.add('danmu_group_title');
  244.  
  245. dataText5 = document.createElement('textarea');
  246. dataText5.classList.add('danmu_group_textarea');
  247. dataText5.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  248. /* ----------------------------------------- textarea ----------------------------------------- */
  249.  
  250. /* ----------------------------------------- random chackbox ----------------------------------------- */
  251. let descCheckbox = document.createElement('span');
  252. descCheckbox.textContent = '随机';
  253. descCheckbox.title = '将合并所有分组数据,从中随机选出一条发送';
  254. descCheckbox.style.fontSize = '16px';
  255. descCheckbox.style.verticalAlign = 'middle';
  256. descCheckbox.style.marginRight = '4px';
  257. descCheckbox.style.color = '#095ca2';
  258.  
  259. rdCheckbox = document.createElement('input');
  260. rdCheckbox.type = 'checkbox';
  261. rdCheckbox.id = 'rdmCheckbox';
  262. rdCheckbox.checked = isRandom;
  263. // rdCheckbox.addEventListener('click', selectRandom);
  264.  
  265. let lblCheckbox = document.createElement('label');
  266. lblCheckbox.setAttribute('for', 'rdmCheckbox');
  267.  
  268. let divCheckbox = document.createElement('div');
  269. divCheckbox.classList.add('random-check');
  270. divCheckbox.style.marginLeft = '10px';
  271. divCheckbox.appendChild(descCheckbox);
  272. divCheckbox.appendChild(rdCheckbox);
  273. divCheckbox.appendChild(lblCheckbox);
  274. /* ----------------------------------------- random chackbox ----------------------------------------- */
  275.  
  276. /* ----------------------------------------- div tip ----------------------------------------- */
  277. spanApplyTip = document.createElement('span');
  278. spanApplyTip.textContent = '设置成功'
  279. spanApplyTip.style.fontSize = '16px';
  280. spanApplyTip.style.color = '#128712';
  281. spanApplyTip.style.display = 'none';
  282.  
  283. let divApplyTip = document.createElement('div');
  284. divApplyTip.style.textAlign = 'center';
  285. divApplyTip.style.display = 'inline-block';
  286. divApplyTip.style.verticalAlign = 'middle';
  287. divApplyTip.style.width = '34%';
  288. divApplyTip.appendChild(spanApplyTip);
  289. /* ----------------------------------------- div tip ----------------------------------------- */
  290.  
  291. /* ----------------------------------------- appley and close button ----------------------------------------- */
  292. let btnApplySetting = document.createElement('i');
  293. btnApplySetting.setAttribute('title', '应用');
  294. btnApplySetting.classList.add('el-button');
  295. btnApplySetting.classList.add('el-icon-check');
  296. btnApplySetting.classList.add('is-circle');
  297. btnApplySetting.addEventListener('click', applySetting);
  298.  
  299. let btnCloseSetting = document.createElement('i');
  300. btnCloseSetting.setAttribute('title', '关闭');
  301. btnCloseSetting.classList.add('el-button');
  302. btnCloseSetting.classList.add('el-icon-close');
  303. btnCloseSetting.classList.add('is-circle');
  304. btnCloseSetting.addEventListener('click', closeSetting);
  305.  
  306. let divSettingButton = document.createElement('div');
  307. divSettingButton.style.display = 'inline-block';
  308. divSettingButton.style.verticalAlign = 'middle';
  309. divSettingButton.appendChild(btnApplySetting);
  310. divSettingButton.appendChild(btnCloseSetting);
  311. /* ----------------------------------------- appley and close button ----------------------------------------- */
  312.  
  313. /* ----------------------------------------- container ----------------------------------------- */
  314. let divBottomContainer = document.createElement('div');
  315. divBottomContainer.style.width = '100%';
  316. divBottomContainer.style.lineHeight = '35px';
  317. divBottomContainer.appendChild(divCheckbox);
  318. divBottomContainer.appendChild(divApplyTip);
  319. divBottomContainer.appendChild(divSettingButton);
  320.  
  321. let divContainer = document.createElement('div');
  322. divContainer.style.height = 'calc(98% - 30px - 25px)';
  323. divContainer.appendChild(divText1);
  324. divContainer.appendChild(dataText1);
  325. divContainer.appendChild(divText2);
  326. divContainer.appendChild(dataText2);
  327. divContainer.appendChild(divText3);
  328. divContainer.appendChild(dataText3);
  329. divContainer.appendChild(divText4);
  330. divContainer.appendChild(dataText4);
  331. divContainer.appendChild(divText5);
  332. divContainer.appendChild(dataText5);
  333. divContainer.appendChild(divBottomContainer);
  334. /* ----------------------------------------- container ----------------------------------------- */
  335.  
  336. divSetting = document.createElement('div');
  337. divSetting.style.backgroundColor = '#d4f2e0';
  338. divSetting.style.borderRadius = '2px';
  339. divSetting.style.width = '100%';
  340. divSetting.style.height = '100%';
  341. divSetting.style.overflowY = 'auto';
  342. divSetting.style.position = 'absolute';
  343. divSetting.style.left = '0';
  344. divSetting.style.top = '0';
  345. divSetting.style.zIndex = '999';
  346. divSetting.style.display = 'none';
  347. divSetting.appendChild(divSettingTitle);
  348. divSetting.appendChild(divTip);
  349. divSetting.appendChild(divContainer);
  350.  
  351. let asideAreaVm = document.getElementById('aside-area-vm');
  352. asideAreaVm.appendChild(divSetting);
  353.  
  354. /* ----------------------------------------- function ----------------------------------------- */
  355. dmButtonSend = document.createElement('button');
  356. dmButtonSend.textContent = '开始';
  357. dmButtonSend.style.minWidth = '65px';
  358. dmButtonSend.style.height = '24px';
  359. dmButtonSend.style.fontSize = '12px';
  360. dmButtonSend.style.borderRadius = '4px';
  361. dmButtonSend.style.color = '#ffffff';
  362. dmButtonSend.style.background = 'rgba(217,157,27,1)';
  363. dmButtonSend.style.border = '0';
  364. dmButtonSend.style.cursor = 'pointer';
  365. //dmButtonSend.onclick = function() { alert('Hello world');}
  366. dmButtonSend.addEventListener('click', offOrOn);
  367.  
  368. let beforeSpan = document.createElement('span');
  369. beforeSpan.textContent = '每';
  370. beforeSpan.style.color = '#ffffff';
  371. beforeSpan.style.fontSize = '12px';
  372. beforeSpan.style.marginLeft = '4px';
  373. beforeSpan.style.backgroundColor = '#ec6c1b';
  374.  
  375. dmInput = document.createElement('input');
  376. dmInput.value = default_timeout;
  377. dmInput.style.width = '25px';
  378. dmInput.style.height = '15px';
  379. dmInput.style.margin = '0 3px';
  380. dmInput.style.border = '0';
  381. dmInput.style.borderRadius = '3px';
  382. dmInput.setAttribute('oninput', "this.value = this.value.replace(/[^0-9]/g, '')");
  383.  
  384. let afterSpan = document.createElement('span');
  385. afterSpan.textContent = '秒发送';
  386. afterSpan.style.color = '#ffffff';
  387. afterSpan.style.fontSize = '12px';
  388. afterSpan.style.backgroundColor = '#ec6c1b';
  389. afterSpan.style.marginRight = '4px';
  390.  
  391. let iElement = document.createElement('i');
  392. iElement.classList.add('el-icon-setting');
  393.  
  394. let btnSetting = document.createElement('button');
  395. btnSetting.title = '设置';
  396. btnSetting.classList.add('el-button');
  397. btnSetting.classList.add('el-button--mini');
  398. btnSetting.classList.add('is-circle');
  399. btnSetting.addEventListener('click', openSetting);
  400. btnSetting.appendChild(iElement);
  401.  
  402. // let btnSetting = document.createElement('ion-icon');
  403. // btnSetting.setAttribute('name', 'settings-sharp');
  404. // btnSetting.classList.add('el-button');
  405. // btnSetting.classList.add('el-button--mini');
  406. // btnSetting.classList.add('is-circle');
  407. // btnSetting.addEventListener('click', openSetting);
  408.  
  409. let div = document.createElement('div');
  410. div.style.position = 'absolute';
  411. div.appendChild(dmButtonSend);
  412. div.appendChild(beforeSpan);
  413. div.appendChild(dmInput);
  414. div.appendChild(afterSpan);
  415. div.appendChild(btnSetting);
  416. divButton.appendChild(div);
  417. /* ----------------------------------------- function ----------------------------------------- */
  418.  
  419. window.autoSendDanmuModuleLoaded = true;
  420. },
  421. arrayInfo = () => console.info(data),
  422. setDanmuData = (obj) => {
  423. if (obj) {
  424. if (source.version === obj.version)
  425. source = obj;
  426. else {
  427. source.data1.values = obj.data1 ? obj.data1 : source.data1.values;
  428. source.data2.values = obj.data2 ? obj.data2 : source.data2.values;
  429. source.data3.values = obj.data3 ? obj.data3 : source.data3.values;
  430. source.data4.values = obj.data4 ? obj.data4 : source.data4.values;
  431. source.data5.values = obj.data5 ? obj.data5 : source.data5.values;
  432. storeDataCallback(source);
  433. }
  434. }
  435.  
  436. dataText1.value = source.data1.values.join('|');
  437. dataText2.value = source.data2.values.join('|');
  438. dataText3.value = source.data3.values.join('|');
  439. dataText4.value = source.data4.values.join('|');
  440. dataText5.value = source.data5.values.join('|');
  441. initData();
  442. },
  443. storeDanmuData = (cb) => storeDataCallback = cb,
  444. removeDanmuData = (cb) => removeDataCallback = cb;
  445.  
  446. initCss();
  447. window.addEventListener("load", () => {
  448. waiters[waiters.length] = setInterval(pageFullyLoaded, 1500);
  449. window.arrayInfo = arrayInfo;
  450. window.setDanmuData = setDanmuData;
  451. window.storeDanmuData = storeDanmuData;
  452. window.removeDanmuData = removeDanmuData;
  453. signIn();
  454. });
  455. })();