b-live-random-send-诗词

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

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

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/447936/1073531/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.style.display = 'inline';
  206. divTip.innerHTML = '任一分组内输入弹幕即可,多条用<span style="color:#dc6b07;margin:0 2px 0 4px;font-weight:700;font-style:normal;">竖线</span>分隔';
  207.  
  208. let divText1 = document.createElement('div');
  209. divText1.textContent = '分组 1 :'
  210. divText1.style.fontSize = '14px';
  211. divText1.style.paddingLeft = '2px';
  212. divText1.style.color = '#12388d';
  213. // divText1.style.display = 'inline';
  214.  
  215. dataText1 = document.createElement('textarea');
  216. dataText1.style.width = '98%';
  217. dataText1.style.height = '100px';
  218. dataText1.style.margin = '1px 0 4px 0';
  219. dataText1.style.border = '0';
  220. dataText1.style.resize = 'none';
  221. dataText1.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  222.  
  223. let divText2 = document.createElement('div');
  224. divText2.textContent = '分组 2 :'
  225. divText2.style.fontSize = '14px';
  226. divText2.style.paddingLeft = '2px';
  227. divText2.style.color = '#12388d';
  228.  
  229. dataText2 = document.createElement('textarea');
  230. dataText2.style.width = '98%';
  231. dataText2.style.height = '100px';
  232. dataText2.style.margin = '1px 0 4px 0';
  233. dataText2.style.border = '0';
  234. dataText2.style.resize = 'none';
  235. dataText2.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  236.  
  237. let divText3 = document.createElement('div');
  238. divText3.textContent = '分组 3 :'
  239. divText3.style.fontSize = '14px';
  240. divText3.style.paddingLeft = '2px';
  241. divText3.style.color = '#12388d';
  242.  
  243. dataText3 = document.createElement('textarea');
  244. dataText3.style.width = '98%';
  245. dataText3.style.height = '100px';
  246. dataText3.style.margin = '1px 0 4px 0';
  247. dataText3.style.border = '0';
  248. dataText3.style.resize = 'none';
  249. dataText3.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  250.  
  251. let divText4 = document.createElement('div');
  252. divText4.textContent = '分组 4 :'
  253. divText4.style.fontSize = '14px';
  254. divText4.style.paddingLeft = '2px';
  255. divText4.style.color = '#12388d';
  256.  
  257. dataText4 = document.createElement('textarea');
  258. dataText4.style.width = '98%';
  259. dataText4.style.height = '100px';
  260. dataText4.style.margin = '1px 0 4px 0';
  261. dataText4.style.border = '0';
  262. dataText4.style.resize = 'none';
  263. dataText4.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  264.  
  265. let divText5 = document.createElement('div');
  266. divText5.textContent = '分组 5 :'
  267. divText5.style.fontSize = '14px';
  268. divText5.style.paddingLeft = '2px';
  269. divText5.style.color = '#12388d';
  270.  
  271. dataText5 = document.createElement('textarea');
  272. dataText5.style.width = '98%';
  273. dataText5.style.height = '100px';
  274. dataText5.style.margin = '1px 0 4px 0';
  275. dataText5.style.border = '0';
  276. dataText5.style.resize = 'none';
  277. dataText5.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  278.  
  279. let descCheckbox = document.createElement('span');
  280. descCheckbox.textContent = '随机';
  281. descCheckbox.title = '将合并所有分组数据,从中随机选出一条发送';
  282. descCheckbox.style.fontSize = '16px';
  283. descCheckbox.style.verticalAlign = 'middle';
  284. descCheckbox.style.marginRight = '4px';
  285. descCheckbox.style.color = '#095ca2';
  286.  
  287. rdCheckbox = document.createElement('input');
  288. rdCheckbox.type = 'checkbox';
  289. rdCheckbox.id = 'rdmCheckbox';
  290. rdCheckbox.checked = isRandom;
  291. // rdCheckbox.addEventListener('click', selectRandom);
  292.  
  293. let lblCheckbox = document.createElement('label');
  294. lblCheckbox.setAttribute('for', 'rdmCheckbox');
  295.  
  296. let divCheckbox = document.createElement('div');
  297. divCheckbox.classList.add('random-check');
  298. divCheckbox.style.marginLeft = '10px';
  299. divCheckbox.appendChild(descCheckbox);
  300. divCheckbox.appendChild(rdCheckbox);
  301. divCheckbox.appendChild(lblCheckbox);
  302.  
  303. spanApplyTip = document.createElement('span');
  304. spanApplyTip.textContent = '设置成功'
  305. spanApplyTip.style.fontSize = '16px';
  306. spanApplyTip.style.color = '#128712';
  307. spanApplyTip.style.display = 'none';
  308.  
  309. let divApplyTip = document.createElement('div');
  310. divApplyTip.style.textAlign = 'center';
  311. divApplyTip.style.display = 'inline-block';
  312. divApplyTip.style.verticalAlign = 'middle';
  313. divApplyTip.style.width = '34%';
  314. divApplyTip.appendChild(spanApplyTip);
  315.  
  316. let btnApplySetting = document.createElement('i');
  317. btnApplySetting.setAttribute('title', '应用');
  318. btnApplySetting.classList.add('el-button');
  319. btnApplySetting.classList.add('el-icon-check');
  320. btnApplySetting.classList.add('is-circle');
  321. btnApplySetting.addEventListener('click', applySetting);
  322.  
  323. let btnCloseSetting = document.createElement('i');
  324. btnCloseSetting.setAttribute('title', '关闭');
  325. btnCloseSetting.classList.add('el-button');
  326. btnCloseSetting.classList.add('el-icon-close');
  327. btnCloseSetting.classList.add('is-circle');
  328. btnCloseSetting.addEventListener('click', closeSetting);
  329.  
  330. let divSettingButton = document.createElement('div');
  331. divSettingButton.style.display = 'inline-block';
  332. divSettingButton.style.verticalAlign = 'middle';
  333. divSettingButton.appendChild(btnApplySetting);
  334. divSettingButton.appendChild(btnCloseSetting);
  335.  
  336. let divBottomContainer = document.createElement('div');
  337. divBottomContainer.style.width = '100%';
  338. divBottomContainer.style.height = '25px';
  339. divBottomContainer.style.margin = '4px 0 8px 0';
  340. // divBottomContainer.style.position = 'absolute';
  341. // divBottomContainer.style.bottom = '0';
  342. divBottomContainer.appendChild(divCheckbox);
  343. divBottomContainer.appendChild(divApplyTip);
  344. divBottomContainer.appendChild(divSettingButton);
  345.  
  346. divSetting = document.createElement('div');
  347. divSetting.style.backgroundColor = '#d4f2e0';
  348. divSetting.style.borderRadius = '2px';
  349. divSetting.style.width = '100%';
  350. divSetting.style.height = '100%';
  351. divSetting.style.overflowY = 'auto';
  352. divSetting.style.position = 'absolute';
  353. divSetting.style.left = '0';
  354. divSetting.style.top = '0';
  355. divSetting.style.zIndex = '999';
  356. divSetting.style.display = 'none';
  357. divSetting.appendChild(divSettingTitle);
  358. divSetting.appendChild(divTip);
  359. divSetting.appendChild(divText1);
  360. divSetting.appendChild(dataText1);
  361. divSetting.appendChild(divText2);
  362. divSetting.appendChild(dataText2);
  363. divSetting.appendChild(divText3);
  364. divSetting.appendChild(dataText3);
  365. divSetting.appendChild(divText4);
  366. divSetting.appendChild(dataText4);
  367. divSetting.appendChild(divText5);
  368. divSetting.appendChild(dataText5);
  369. divSetting.appendChild(divBottomContainer);
  370.  
  371. let asideAreaVm = document.getElementById('aside-area-vm');
  372. asideAreaVm.appendChild(divSetting);
  373.  
  374. dmButtonSend = document.createElement('button');
  375. dmButtonSend.textContent = '开始';
  376. dmButtonSend.style.minWidth = '65px';
  377. dmButtonSend.style.height = '24px';
  378. dmButtonSend.style.fontSize = '12px';
  379. dmButtonSend.style.borderRadius = '4px';
  380. dmButtonSend.style.color = '#ffffff';
  381. dmButtonSend.style.background = 'rgba(217,157,27,1)';
  382. dmButtonSend.style.border = '0';
  383. dmButtonSend.style.cursor = 'pointer';
  384. //dmButtonSend.onclick = function() { alert('Hello world');}
  385. dmButtonSend.addEventListener('click', offOrOn);
  386.  
  387. let beforeSpan = document.createElement('span');
  388. beforeSpan.textContent = '每';
  389. beforeSpan.style.color = '#ffffff';
  390. beforeSpan.style.fontSize = '12px';
  391. beforeSpan.style.marginLeft = '4px';
  392. beforeSpan.style.backgroundColor = '#ec6c1b';
  393.  
  394. dmInput = document.createElement('input');
  395. dmInput.value = default_timeout;
  396. dmInput.style.width = '25px';
  397. dmInput.style.height = '15px';
  398. dmInput.style.margin = '0 3px';
  399. dmInput.style.border = '0';
  400. dmInput.style.borderRadius = '3px';
  401. dmInput.setAttribute('oninput', "this.value = this.value.replace(/[^0-9]/g, '')");
  402.  
  403. let afterSpan = document.createElement('span');
  404. afterSpan.textContent = '秒发送';
  405. afterSpan.style.color = '#ffffff';
  406. afterSpan.style.fontSize = '12px';
  407. afterSpan.style.backgroundColor = '#ec6c1b';
  408. afterSpan.style.marginRight = '4px';
  409.  
  410. let iElement = document.createElement('i');
  411. iElement.classList.add('el-icon-setting');
  412.  
  413. let btnSetting = document.createElement('button');
  414. btnSetting.title = '设置';
  415. btnSetting.classList.add('el-button');
  416. btnSetting.classList.add('el-button--mini');
  417. btnSetting.classList.add('is-circle');
  418. btnSetting.addEventListener('click', openSetting);
  419. btnSetting.appendChild(iElement);
  420.  
  421. // let btnSetting = document.createElement('ion-icon');
  422. // btnSetting.setAttribute('name', 'settings-sharp');
  423. // btnSetting.classList.add('el-button');
  424. // btnSetting.classList.add('el-button--mini');
  425. // btnSetting.classList.add('is-circle');
  426. // btnSetting.addEventListener('click', openSetting);
  427.  
  428. let div = document.createElement('div');
  429. div.style.position = 'absolute';
  430. div.appendChild(dmButtonSend);
  431. div.appendChild(beforeSpan);
  432. div.appendChild(dmInput);
  433. div.appendChild(afterSpan);
  434. div.appendChild(btnSetting);
  435. divButton.appendChild(div);
  436.  
  437. window.autoSendDanmuModuleLoaded = true;
  438. },
  439. arrayInfo = () => console.info(data),
  440. setDanmuData = (obj) => {
  441. if (!obj) return;
  442. if (source.version === obj.version)
  443. source = obj;
  444. else {
  445. source.data1.values = obj.data1;
  446. source.data2.values = obj.data2;
  447. source.data3.values = obj.data3;
  448. source.data4.values = obj.data4;
  449. source.data5.values = obj.data5;
  450. storeDataCallback(source);
  451. }
  452.  
  453. dataText1.value = source.data1.values.join('|');
  454. dataText2.value = source.data2.values.join('|');
  455. dataText3.value = source.data3.values.join('|');
  456. dataText4.value = source.data4.values.join('|');
  457. dataText5.value = source.data5.values.join('|');
  458. initData();
  459. },
  460. storeDanmuData = (cb) => storeDataCallback = cb,
  461. removeDanmuData = (cb) => removeDataCallback = cb;
  462.  
  463. initCss();
  464. window.addEventListener("load", () => {
  465. waiters[waiters.length] = setInterval(pageFullyLoaded, 1500);
  466. window.arrayInfo = arrayInfo;
  467. window.setDanmuData = setDanmuData;
  468. window.storeDanmuData = storeDanmuData;
  469. window.removeDanmuData = removeDanmuData;
  470. signIn();
  471. });
  472. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址