b-live-random-send-test

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

当前为 2023-03-25 提交的版本,查看 最新版本

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

  1. (function () {
  2. const blobURL = URL.createObjectURL(
  3. new Blob(
  4. [
  5. '(',
  6. function () {
  7. const ids = {};
  8. // 监听message 开始执行定时器或者销毁
  9. self.onmessage = (e) => {
  10. switch (e.data.command) {
  11. case 'interval:start': // 开启定时器
  12. const intervalId = setInterval(() => postMessage({ message: 'interval:tick', id: e.data.id }), e.data.interval);
  13. // postMessage({ message: 'interval:started', id: e.data.id });
  14. ids[e.data.id] = intervalId;
  15. break;
  16. case 'interval:clear': // 销毁
  17. clearInterval(ids[e.data.id]);
  18. postMessage({ message: 'interval:cleared', id: e.data.id });
  19. delete ids[e.data.id];
  20. break;
  21. case 'timeout:start':
  22. const timeoutId = setTimeout(() => postMessage({ message: 'timeout:tick', id: e.data.id }), e.data.timeout);
  23. // postMessage({ message: 'timeout:started', id: e.data.id });
  24. ids[e.data.id] = timeoutId;
  25. break;
  26. case 'timeout:clear':
  27. clearTimeout(ids[e.data.id]);
  28. postMessage({ message: 'timeout:cleared', id: e.data.id });
  29. delete ids[e.data.id];
  30. break;
  31. }
  32. };
  33. }.toString(),
  34. ')()',
  35. ],
  36. { type: 'application/javascript' },
  37. ),
  38. );
  39. const worker = new Worker(blobURL);
  40. URL.revokeObjectURL(blobURL); //用完释放URL对象
  41. const workerTimer = {
  42. id: 0,
  43. callbacks: {},
  44. setInterval: (cb, interval, context) => {
  45. const id = ++workerTimer.id;
  46. workerTimer.callbacks[id] = { fn: cb, context: context };
  47. worker.postMessage({ command: 'interval:start', interval: interval, id: id });
  48. return id;
  49. },
  50. setTimeout: (cb, timeout, context) => {
  51. const id = ++workerTimer.id;
  52. workerTimer.callbacks[id] = { fn: cb, context: context };
  53. worker.postMessage({ command: 'timeout:start', timeout: timeout, id: id });
  54. return id;
  55. },
  56.  
  57. // 监听worker 里面的定时器发送的message 然后执行回调函数
  58. onMessage: (e) => {
  59. switch (e.data.message) {
  60. case 'interval:tick':
  61. case 'timeout:tick':
  62. const callbackItem = workerTimer.callbacks[e.data.id];
  63. if (callbackItem && callbackItem.fn)
  64. callbackItem.fn.apply(callbackItem.context);
  65.  
  66. break;
  67. case 'interval:cleared':
  68. case 'timeout:cleared':
  69. delete workerTimer.callbacks[e.data.id];
  70. break;
  71. }
  72. },
  73.  
  74. // 往worker里面发送销毁指令
  75. clearInterval: (id) => worker.postMessage({ command: 'interval:clear', id: id }),
  76. clearTimeout: (id) => worker.postMessage({ command: 'timeout:clear', id: id }),
  77. };
  78. worker.onmessage = workerTimer.onMessage.bind(workerTimer);
  79.  
  80. let source = {
  81. version: 3,
  82. random: true,
  83. data1: { available: true, values: ['弹幕①', '弹幕②'] },
  84. data2: { available: true, values: ['弹幕③', '弹幕④'] },
  85. data3: { available: true, values: ['弹幕⑤', '弹幕⑥'] },
  86. data4: { available: true, values: ['弹幕⑦', '弹幕⑧'] },
  87. data5: { available: true, values: ['弹幕⑨', '弹幕Ⅹ'] }
  88. },
  89. rdCheckbox, group1Checkbox, group2Checkbox, group3Checkbox, group4Checkbox, group5Checkbox,
  90. dmButtonSend, dmInput, divSetting, dataText1, dataText2, dataText3, dataText4, dataText5, spanApplyTip,
  91. pdata = {}, waiters = [], data = [], sendTimer = null, removeElementsTimer = null, signInTimer = null,
  92. count = 0, waitCount = 200, arrayIndex = 0, removeElementsTimerCount = 0, default_timeout = 600,
  93. hbInterval = Math.floor(Math.random() * (35 - 50)) + 50; // 随机范围:[30 ~ 50]
  94. gmNotice = obj => { alert('请更新油猴脚本'); window.location.href = parentUrl; },
  95. getGmValue = (key, defaultValue) => { return null; },
  96. setGmValue = (key, obj) => { console.warn('===> No implementation "setGmValue" method.'); },
  97. delGmValue = key => { console.warn('===> No implementation "delGmValue" method.'); };
  98.  
  99. const minVersion = '2.4.0', noticeTimeout = 10000,
  100. icoUrl = 'https://www.bilibili.com/favicon.ico',
  101. parentUrl = 'https://greasyfork.org/scripts/446725-b%E7%AB%99%E7%9B%B4%E6%92%AD%E9%97%B4%E5%AE%9A%E6%97%B6%E5%8F%91%E9%9A%8F%E6%9C%BA%E5%BC%B9%E5%B9%95/code/B%E7%AB%99%E7%9B%B4%E6%92%AD%E9%97%B4%E5%AE%9A%E6%97%B6%E5%8F%91%E9%9A%8F%E6%9C%BA%E5%BC%B9%E5%B9%95.user.js',
  102. // roomId = window.__NEPTUNE_IS_MY_WAIFU__
  103. // ? 0 == window.__NEPTUNE_IS_MY_WAIFU__.roomInfoRes.data.room_info.short_id
  104. // ? window.__NEPTUNE_IS_MY_WAIFU__.roomInfoRes.data.room_info.room_id
  105. // : window.__NEPTUNE_IS_MY_WAIFU__.roomInfoRes.data.room_info.short_id
  106. // : window.location.pathname.replace(/^\/(\S+\/)*/g, ''),
  107. roomId = window.location.pathname.replace(/^\/(\S+\/)*/g, ''),
  108. setGmGetValue = callback => getGmValue = callback,
  109. setGmSetValue = callback => setGmValue = callback,
  110. setGmDelValue = callback => delGmValue = callback,
  111. setGmNotice = callback => gmNotice = callback,
  112. setParentData = obj => pdata = obj,
  113. arrayInfo = () => console.info(data),
  114. // setLoadedFlag = flag => window.autoSendDanmuModuleLoaded = flag,
  115. isOldVersion = () => {
  116. if (!pdata.version) {
  117. return true;
  118. }
  119. if (minVersion === pdata.version) {
  120. return false;
  121. } else {
  122. let vals = pdata.version.split('.');
  123. let mins = minVersion.split('.');
  124. if (vals.length != mins.length) {
  125. return true;
  126. } else {
  127. for (let i = 0; i < vals.length; i++) {
  128. if (mins[i] > vals[i]) {
  129. return true;
  130. }
  131. }
  132.  
  133. return false;
  134. }
  135. }
  136. },
  137. initCss = () => {
  138. let linkElement = document.createElement('link');
  139. linkElement.rel = 'stylesheet';
  140. linkElement.href = 'https://unpkg.com/element-ui@2.15.9/lib/theme-chalk/index.css';
  141. document.head.appendChild(linkElement);
  142.  
  143. // 图标库 https://ionic.io/ionicons
  144. // let scriptElement = document.createElement('script');
  145. // scriptElement.src = 'https://unpkg.com/ionicons@5.5.2/dist/ionicons.js';
  146. // document.head.appendChild(scriptElement);
  147.  
  148. let customerStyle = document.createElement('style');
  149. customerStyle.setAttribute('type', 'text/css');
  150. customerStyle.innerHTML = '.danmu-group-title{font-size:14px;padding-left:2px;color:rgb(18, 56, 141);display:inline;margin-right:60%;vertical-align:middle;}.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;}input[type="checkbox"]{display:none;}.switch-check{display:inline-block;margin:0 5px;vertical-align:middle;}.switch-check-label{display:inline-block;vertical-align:middle;border:1px solid #bdc3c7;border-radius:60px;width:40px;height:18px;position:relative;transition:all .3s;cursor:pointer;}.switch-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;}.switch-check :checked ~ label{background-color:#26b22b;border-color:#26b22b;}.switch-check :checked ~ label:before{left:22px;background-color:#fff;}.danmu-random-setting-panel{background-color:#d4f2e0;border-radius:2px;width:100%;height:100%;overflow-y:auto;position:absolute;left:0px;top:0px;z-index:999;display:none;}.danmu-random-setting-title{text-align:center;font-size:16px;font-weight:700;color:#1c5adc;line-height:30px;}.danmu-random-setting-tips{color:#0b81cc;text-align:center;font-style:italic;}.danmu-random-update-tips{color:#0b81cc;text-align:center;font-size:13px;font-weight:700;margin:10px 0px;}.danmu-random-setting-bottom{width:100%;line-height:35px;}.danmu-random-switch-button-title{font-size:16px;vertical-align:middle;margin-right:4px;color:#095ca2;cursor:help;}.danmu-random-setting-success-tips{text-align:center;display:inline-block;vertical-align:middle;width:34%;}.danmu-random-setting-success-text{font-size:16px;color:#128712;display:none;}.danmu-random-set-button-container{display:inline-block;vertical-align:middle;}';
  151. document.head.appendChild(customerStyle);
  152. },
  153. // initScript = () => {
  154. // let script = document.createElement('script');
  155. // script.type = 'text/javascript';
  156. // script.src = 'https://cdn.jsdelivr.net/npm/crypto-js@4.1.1/crypto-js.js';
  157. // document.head.appendChild(script);
  158. // },
  159. getCurrentTimestamp = () => new Date().getTime(),
  160. send = (msg, index) => {
  161. let dmTextArea = document.getElementById('aside-area-vm').getElementsByClassName('chat-input border-box')[0];
  162. if (!dmTextArea) {
  163. alert('找不到输入弹幕文本框,请尝试刷新页面');
  164. return;
  165. }
  166.  
  167. let btnSend = document.getElementsByClassName('bl-button live-skin-highlight-button-bg live-skin-button-text bl-button--primary bl-button--small')[0];
  168. if (!btnSend) {
  169. alert('找不到发送按钮,请尝试刷新页面');
  170. return;
  171. }
  172. // if (getCurrentTimestamp() - lastSent < 5000) {
  173. // setTimeout(() => send(msg, index), 5000);
  174. // console.log('===> 发送间隔少于5秒,进入递归处理');
  175. // } else {
  176. dmTextArea.value = msg;
  177. // 定义事件, 定义才可发送
  178. dmTextArea.dispatchEvent(new Event('input', { "bubbles": true, "cancelable": true }));
  179. btnSend.click();
  180. lastSent = getCurrentTimestamp();
  181. ++count;
  182. console.log('===> ' + new Date().toLocaleString() + ' 弹幕发送成功 ' + count + ' 次,第【' + index + '】条数据 === ' + msg);
  183. // }
  184. },
  185. isNull = str => {
  186. if (!str || str == "")
  187. return true;
  188.  
  189. let regu = "^[ ]+$";
  190. let re = new RegExp(regu);
  191. return re.test(str);
  192. },
  193. randomSort = arr => {
  194. for (let i = 0; i < arr.length; i++) {
  195. const rdIndex = Math.floor(Math.random() * arr.length);
  196. const temp = arr[i];
  197. arr[i] = arr[rdIndex];
  198. arr[rdIndex] = temp;
  199. }
  200.  
  201. return arr;
  202. },
  203. clearWaiters = () => {
  204. for (let i = 0; i < waiters.length; i++) {
  205. workerTimer.clearInterval(waiters[i]);
  206. waiters[i] = null;
  207. }
  208.  
  209. waiters = [];
  210. },
  211. signIn = () => {
  212. if (!signInTimer) {
  213. let timestamp = new Date(new Date(new Date().setDate(new Date().getDate() + 1)).toDateString()).getTime() - getCurrentTimestamp();
  214. console.log('===> 设置凌晨打卡定时器【' + timestamp + '】');
  215. signInTimer = workerTimer.setTimeout(() => {
  216. if (dmButtonSend.textContent === '停止')
  217. send(pdata.signText ? pdata.signText : '打卡', 0);
  218. console.log('===> 进入下一轮递归签到');
  219. workerTimer.clearTimeout(signInTimer);
  220. signInTimer = null;
  221. signIn();
  222. }, timestamp);
  223. }
  224. },
  225. // selectRandom = () => source.random = rdCheckbox.checked,
  226. setCheckboxChecked = () => {
  227. rdCheckbox.checked = source.random;
  228. group1Checkbox.checked = source.data1.available;
  229. group2Checkbox.checked = source.data2.available;
  230. group3Checkbox.checked = source.data3.available;
  231. group4Checkbox.checked = source.data4.available;
  232. group5Checkbox.checked = source.data5.available;
  233. },
  234. openSetting = () => divSetting.style.display = 'block',
  235. closeSetting = () => {
  236. setCheckboxChecked();
  237. divSetting.style.display = 'none';
  238. },
  239. initData = () => {
  240. if (source.data1.values.length <= 0
  241. && source.data2.values.length <= 0
  242. && source.data3.values.length <= 0
  243. && source.data4.values.length <= 0
  244. && source.data5.values.length <= 0) {
  245. return data ? data : [];
  246. }
  247.  
  248. let result = [];
  249. result = source.data1.available ? result.concat(source.data1.values) : result;
  250. result = source.data2.available ? result.concat(source.data2.values) : result;
  251. result = source.data3.available ? result.concat(source.data3.values) : result;
  252. result = source.data4.available ? result.concat(source.data4.values) : result;
  253. result = source.data5.available ? result.concat(source.data5.values) : result;
  254. data = result;
  255. source.random ? data = randomSort(result) : arrayIndex = 0;
  256. },
  257. applySetting = () => {
  258. source.data1.values = isNull(dataText1.value) ? [] : dataText1.value.split('|');
  259. source.data2.values = isNull(dataText2.value) ? [] : dataText2.value.split('|');
  260. source.data3.values = isNull(dataText3.value) ? [] : dataText3.value.split('|');
  261. source.data4.values = isNull(dataText4.value) ? [] : dataText4.value.split('|');
  262. source.data5.values = isNull(dataText5.value) ? [] : dataText5.value.split('|');
  263. source.random = rdCheckbox.checked;
  264. source.data1.available = group1Checkbox.checked;
  265. source.data2.available = group2Checkbox.checked;
  266. source.data3.available = group3Checkbox.checked;
  267. source.data4.available = group4Checkbox.checked;
  268. source.data5.available = group5Checkbox.checked;
  269. initData();
  270. setGmValue(roomId, source);
  271. spanApplyTip.style.display = 'block';
  272. setTimeout(() => {
  273. spanApplyTip.style.display = 'none';
  274. divSetting.style.display = 'none';
  275. }, 1500);
  276. },
  277. danmu = () => {
  278. if (data.length < 1) {
  279. gmNotice({
  280. text: '请任意在一个分组里输入一条弹幕',
  281. title: '没有弹幕数据,请先设置',
  282. image: icoUrl,
  283. highlight: true,
  284. timeout: noticeTimeout
  285. });
  286. return false;
  287. }
  288. if (source.random)
  289. arrayIndex = Math.floor((Math.random() * data.length));
  290.  
  291. send(data[arrayIndex], arrayIndex);
  292. ++arrayIndex;
  293. if (arrayIndex >= data.length)
  294. arrayIndex = 0;
  295.  
  296. return true;
  297. },
  298. offOrOn = () => {
  299. let timeout = 0;
  300. if (sendTimer) {
  301. workerTimer.clearInterval(sendTimer);
  302. sendTimer = null;
  303. dmButtonSend.style.background = 'rgba(217,157,27,1)';
  304. dmButtonSend.textContent = '开始';
  305. dmInput.removeAttribute("disabled");
  306. } else {
  307. timeout = isNull(dmInput.value) ? default_timeout * 1000 : dmInput.value * 1000;
  308. if (!danmu())
  309. return;
  310.  
  311. sendTimer = workerTimer.setInterval(danmu, timeout);
  312. dmButtonSend.style.background = '#ff0000';
  313. dmButtonSend.textContent = '停止';
  314. dmInput.setAttribute('disabled', 'disabled');
  315. }
  316. },
  317. buildPanel = divButton => {
  318. /* ----------------------------------------- head ----------------------------------------- */
  319. let divSettingTitle = document.createElement('div');
  320. divSettingTitle.textContent = '弹幕设置';
  321. divSettingTitle.classList.add('danmu-random-setting-title');
  322.  
  323. let divTip = document.createElement('div');
  324. divTip.classList.add('danmu-random-setting-tips');
  325. divTip.innerHTML = '任一分组内输入弹幕即可,多条用<span style="color:#dc6b07;margin:0 2px 0 4px;font-weight:700;font-style:normal;">竖线</span>分隔';
  326.  
  327. let divUpdateTip = document.createElement('div');
  328. divUpdateTip.classList.add('danmu-random-update-tips');
  329. divUpdateTip.innerHTML = '<span style="color:#f00">更新提示:</span>近期油猴网络不稳影响脚本加载';
  330. /* ----------------------------------------- head ----------------------------------------- */
  331.  
  332. /* ----------------------------------------- textarea 1 ----------------------------------------- */
  333. let divText1 = document.createElement('div');
  334. divText1.textContent = '分组 1 :';
  335. divText1.classList.add('danmu-group-title');
  336.  
  337. group1Checkbox = document.createElement('input');
  338. group1Checkbox.type = 'checkbox';
  339. group1Checkbox.id = 'group1Checkbox';
  340. group1Checkbox.checked = true;
  341.  
  342. let lblGroup1Checkbox = document.createElement('label');
  343. lblGroup1Checkbox.setAttribute('for', 'group1Checkbox');
  344. lblGroup1Checkbox.classList.add('switch-check-label');
  345.  
  346. let divGroup1Checkbox = document.createElement('div');
  347. divGroup1Checkbox.classList.add('switch-check');
  348. divGroup1Checkbox.appendChild(group1Checkbox);
  349. divGroup1Checkbox.appendChild(lblGroup1Checkbox);
  350.  
  351. dataText1 = document.createElement('textarea');
  352. dataText1.classList.add('danmu-group-textarea');
  353. dataText1.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  354. /* ----------------------------------------- textarea 1 ----------------------------------------- */
  355.  
  356. /* ----------------------------------------- textarea 2 ----------------------------------------- */
  357. let divText2 = document.createElement('div');
  358. divText2.textContent = '分组 2 :';
  359. divText2.classList.add('danmu-group-title');
  360.  
  361. group2Checkbox = document.createElement('input');
  362. group2Checkbox.type = 'checkbox';
  363. group2Checkbox.id = 'group2Checkbox';
  364. group2Checkbox.checked = true;
  365.  
  366. let lblGroup2Checkbox = document.createElement('label');
  367. lblGroup2Checkbox.setAttribute('for', 'group2Checkbox');
  368. lblGroup2Checkbox.classList.add('switch-check-label');
  369.  
  370. let divGroup2Checkbox = document.createElement('div');
  371. divGroup2Checkbox.classList.add('switch-check');
  372. divGroup2Checkbox.appendChild(group2Checkbox);
  373. divGroup2Checkbox.appendChild(lblGroup2Checkbox);
  374.  
  375. dataText2 = document.createElement('textarea');
  376. dataText2.classList.add('danmu-group-textarea');
  377. dataText2.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  378. /* ----------------------------------------- textarea 2 ----------------------------------------- */
  379.  
  380. /* ----------------------------------------- textarea 3 ----------------------------------------- */
  381. let divText3 = document.createElement('div');
  382. divText3.textContent = '分组 3 :';
  383. divText3.classList.add('danmu-group-title');
  384.  
  385. group3Checkbox = document.createElement('input');
  386. group3Checkbox.type = 'checkbox';
  387. group3Checkbox.id = 'group3Checkbox';
  388. group3Checkbox.checked = true;
  389.  
  390. let lblGroup3Checkbox = document.createElement('label');
  391. lblGroup3Checkbox.setAttribute('for', 'group3Checkbox');
  392. lblGroup3Checkbox.classList.add('switch-check-label');
  393.  
  394. let divGroup3Checkbox = document.createElement('div');
  395. divGroup3Checkbox.classList.add('switch-check');
  396. divGroup3Checkbox.appendChild(group3Checkbox);
  397. divGroup3Checkbox.appendChild(lblGroup3Checkbox);
  398.  
  399. dataText3 = document.createElement('textarea');
  400. dataText3.classList.add('danmu-group-textarea');
  401. dataText3.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  402. /* ----------------------------------------- textarea 3 ----------------------------------------- */
  403.  
  404. /* ----------------------------------------- textarea 4 ----------------------------------------- */
  405. let divText4 = document.createElement('div');
  406. divText4.textContent = '分组 4 :';
  407. divText4.classList.add('danmu-group-title');
  408.  
  409. group4Checkbox = document.createElement('input');
  410. group4Checkbox.type = 'checkbox';
  411. group4Checkbox.id = 'group4Checkbox';
  412. group4Checkbox.checked = true;
  413.  
  414. let lblGroup4Checkbox = document.createElement('label');
  415. lblGroup4Checkbox.setAttribute('for', 'group4Checkbox');
  416. lblGroup4Checkbox.classList.add('switch-check-label');
  417.  
  418. let divGroup4Checkbox = document.createElement('div');
  419. divGroup4Checkbox.classList.add('switch-check');
  420. divGroup4Checkbox.appendChild(group4Checkbox);
  421. divGroup4Checkbox.appendChild(lblGroup4Checkbox);
  422.  
  423. dataText4 = document.createElement('textarea');
  424. dataText4.classList.add('danmu-group-textarea');
  425. dataText4.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  426. /* ----------------------------------------- textarea 4 ----------------------------------------- */
  427.  
  428. /* ----------------------------------------- textarea 5 ----------------------------------------- */
  429. let divText5 = document.createElement('div');
  430. divText5.textContent = '分组 5 :';
  431. divText5.classList.add('danmu-group-title');
  432.  
  433. group5Checkbox = document.createElement('input');
  434. group5Checkbox.type = 'checkbox';
  435. group5Checkbox.id = 'group5Checkbox';
  436. group5Checkbox.checked = true;
  437.  
  438. let lblGroup5Checkbox = document.createElement('label');
  439. lblGroup5Checkbox.setAttribute('for', 'group5Checkbox');
  440. lblGroup5Checkbox.classList.add('switch-check-label');
  441.  
  442. let divGroup5Checkbox = document.createElement('div');
  443. divGroup5Checkbox.classList.add('switch-check');
  444. divGroup5Checkbox.appendChild(group5Checkbox);
  445. divGroup5Checkbox.appendChild(lblGroup5Checkbox);
  446.  
  447. dataText5 = document.createElement('textarea');
  448. dataText5.classList.add('danmu-group-textarea');
  449. dataText5.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  450. /* ----------------------------------------- textarea 5 ----------------------------------------- */
  451.  
  452. /* ----------------------------------------- random chackbox ----------------------------------------- */
  453. let descCheckbox = document.createElement('span');
  454. descCheckbox.textContent = '随机';
  455. descCheckbox.title = '将合并所有分组数据,从中随机选出一条发送';
  456. descCheckbox.classList.add('danmu-random-switch-button-title');
  457.  
  458. rdCheckbox = document.createElement('input');
  459. rdCheckbox.type = 'checkbox';
  460. rdCheckbox.id = 'rdmCheckbox';
  461. rdCheckbox.checked = true;
  462. // rdCheckbox.addEventListener('click', selectRandom);
  463.  
  464. let lblCheckbox = document.createElement('label');
  465. lblCheckbox.setAttribute('for', 'rdmCheckbox');
  466. lblCheckbox.classList.add('switch-check-label');
  467.  
  468. let divCheckbox = document.createElement('div');
  469. divCheckbox.classList.add('switch-check');
  470. divCheckbox.style.marginLeft = '10px';
  471. divCheckbox.appendChild(descCheckbox);
  472. divCheckbox.appendChild(rdCheckbox);
  473. divCheckbox.appendChild(lblCheckbox);
  474. /* ----------------------------------------- random chackbox ----------------------------------------- */
  475.  
  476. /* ----------------------------------------- div tip ----------------------------------------- */
  477. spanApplyTip = document.createElement('span');
  478. spanApplyTip.textContent = '设置成功';
  479. spanApplyTip.classList.add('danmu-random-setting-success-text');
  480.  
  481. let divApplyTip = document.createElement('div');
  482. divApplyTip.classList.add('danmu-random-setting-success-tips');
  483. divApplyTip.appendChild(spanApplyTip);
  484. /* ----------------------------------------- div tip ----------------------------------------- */
  485.  
  486. /* ----------------------------------------- appley and close button ----------------------------------------- */
  487. let btnApplySetting = document.createElement('i');
  488. btnApplySetting.setAttribute('title', '应用');
  489. btnApplySetting.classList.add('el-button');
  490. btnApplySetting.classList.add('el-icon-check');
  491. btnApplySetting.classList.add('is-circle');
  492. btnApplySetting.addEventListener('click', applySetting);
  493.  
  494. let btnCloseSetting = document.createElement('i');
  495. btnCloseSetting.setAttribute('title', '关闭');
  496. btnCloseSetting.classList.add('el-button');
  497. btnCloseSetting.classList.add('el-icon-close');
  498. btnCloseSetting.classList.add('is-circle');
  499. btnCloseSetting.addEventListener('click', closeSetting);
  500.  
  501. let divSettingButton = document.createElement('div');
  502. divSettingButton.classList.add('danmu-random-set-button-container');
  503. divSettingButton.appendChild(btnApplySetting);
  504. divSettingButton.appendChild(btnCloseSetting);
  505. /* ----------------------------------------- appley and close button ----------------------------------------- */
  506.  
  507. /* ----------------------------------------- container ----------------------------------------- */
  508. let divBottomContainer = document.createElement('div');
  509. divBottomContainer.classList.add('danmu-random-setting-bottom');
  510. divBottomContainer.appendChild(divCheckbox);
  511. divBottomContainer.appendChild(divApplyTip);
  512. divBottomContainer.appendChild(divSettingButton);
  513.  
  514. let divContainer = document.createElement('div');
  515. divContainer.style.height = 'calc(98% - 30px - 25px)';
  516. divContainer.appendChild(divText1);
  517. divContainer.appendChild(divGroup1Checkbox);
  518. divContainer.appendChild(dataText1);
  519. divContainer.appendChild(divText2);
  520. divContainer.appendChild(divGroup2Checkbox);
  521. divContainer.appendChild(dataText2);
  522. divContainer.appendChild(divText3);
  523. divContainer.appendChild(divGroup3Checkbox);
  524. divContainer.appendChild(dataText3);
  525. divContainer.appendChild(divText4);
  526. divContainer.appendChild(divGroup4Checkbox);
  527. divContainer.appendChild(dataText4);
  528. divContainer.appendChild(divText5);
  529. divContainer.appendChild(divGroup5Checkbox);
  530. divContainer.appendChild(dataText5);
  531. divContainer.appendChild(divBottomContainer);
  532. /* ----------------------------------------- container ----------------------------------------- */
  533.  
  534. divSetting = document.createElement('div');
  535. divSetting.id = 'danmu-setting-panel';
  536. divSetting.classList.add('danmu-random-setting-panel');
  537. divSetting.appendChild(divSettingTitle);
  538. divSetting.appendChild(divTip);
  539. divSetting.appendChild(divUpdateTip);
  540. divSetting.appendChild(divContainer);
  541.  
  542. let asideAreaVm = document.getElementById('aside-area-vm');
  543. asideAreaVm.appendChild(divSetting);
  544.  
  545. /* ----------------------------------------- function ----------------------------------------- */
  546. dmButtonSend = document.createElement('button');
  547. dmButtonSend.textContent = '开始';
  548. dmButtonSend.style.minWidth = '65px';
  549. dmButtonSend.style.height = '24px';
  550. dmButtonSend.style.fontSize = '12px';
  551. dmButtonSend.style.borderRadius = '4px';
  552. dmButtonSend.style.color = '#ffffff';
  553. dmButtonSend.style.background = 'rgba(217,157,27,1)';
  554. dmButtonSend.style.border = '0';
  555. dmButtonSend.style.cursor = 'pointer';
  556. //dmButtonSend.onclick = function() { alert('Hello world');}
  557. dmButtonSend.addEventListener('click', offOrOn);
  558.  
  559. let beforeSpan = document.createElement('span');
  560. beforeSpan.textContent = '每';
  561. beforeSpan.style.color = '#ffffff';
  562. beforeSpan.style.fontSize = '12px';
  563. beforeSpan.style.marginLeft = '4px';
  564. beforeSpan.style.backgroundColor = '#ec6c1b';
  565.  
  566. dmInput = document.createElement('input');
  567. dmInput.value = default_timeout;
  568. dmInput.style.width = '25px';
  569. dmInput.style.height = '15px';
  570. dmInput.style.margin = '0 3px';
  571. dmInput.style.border = '0';
  572. dmInput.style.borderRadius = '3px';
  573. dmInput.setAttribute('oninput', "this.value = this.value.replace(/[^0-9]/g, '')");
  574.  
  575. let afterSpan = document.createElement('span');
  576. afterSpan.textContent = '秒发送';
  577. afterSpan.style.color = '#ffffff';
  578. afterSpan.style.fontSize = '12px';
  579. afterSpan.style.backgroundColor = '#ec6c1b';
  580. afterSpan.style.marginRight = '4px';
  581.  
  582. let iElement = document.createElement('i');
  583. iElement.classList.add('el-icon-setting');
  584.  
  585. let btnSetting = document.createElement('button');
  586. btnSetting.title = '设置';
  587. btnSetting.classList.add('el-button');
  588. btnSetting.classList.add('el-button--mini');
  589. btnSetting.classList.add('is-circle');
  590. btnSetting.addEventListener('click', openSetting);
  591. btnSetting.appendChild(iElement);
  592.  
  593. // let btnSetting = document.createElement('ion-icon');
  594. // btnSetting.setAttribute('name', 'settings-sharp');
  595. // btnSetting.classList.add('el-button');
  596. // btnSetting.classList.add('el-button--mini');
  597. // btnSetting.classList.add('is-circle');
  598. // btnSetting.addEventListener('click', openSetting);
  599.  
  600. let div = document.createElement('div');
  601. div.style.position = 'absolute';
  602. div.appendChild(dmButtonSend);
  603. div.appendChild(beforeSpan);
  604. div.appendChild(dmInput);
  605. div.appendChild(afterSpan);
  606. div.appendChild(btnSetting);
  607. divButton.appendChild(div);
  608. /* ----------------------------------------- function ----------------------------------------- */
  609. },
  610. removeElements = () => {
  611. let switchLoginGuideVm = document.getElementById('switch-login-guide-vm');
  612. if (switchLoginGuideVm)
  613. switchLoginGuideVm.style.setProperty('display', 'none', 'important');
  614.  
  615. let myDearHarunaVm = document.getElementById('my-dear-haruna-vm');
  616. if (myDearHarunaVm)
  617. myDearHarunaVm.style.setProperty('display', 'none', 'important');
  618.  
  619. let shopPopoverVm = document.getElementById('shop-popover-vm');
  620. if (shopPopoverVm)
  621. shopPopoverVm.style.setProperty('display', 'none', 'important');
  622.  
  623. if (removeElementsTimer) {
  624. workerTimer.clearInterval(removeElementsTimer);
  625. removeElementsTimer = null;
  626. }
  627. removeElementsTimer = workerTimer.setInterval(() => {
  628. ++removeElementsTimerCount;
  629. let ltRow = document.getElementsByClassName('lt-row')[0];
  630. if (ltRow) {
  631. ltRow.style.setProperty('display', 'none', 'important');
  632. if (removeElementsTimer) {
  633. workerTimer.clearInterval(removeElementsTimer);
  634. removeElementsTimer = null;
  635. }
  636. }
  637. if (30 <= removeElementsTimerCount && removeElementsTimer) {
  638. workerTimer.clearInterval(removeElementsTimer);
  639. removeElementsTimer = null;
  640. }
  641. }, 10000);
  642. },
  643. loadData = () => {
  644. let obj = getGmValue(roomId, null);
  645. if (obj) {
  646. if (source.version === obj.version) {
  647. source = obj;
  648. }
  649. else if (obj.version === 2) {
  650. source.data1 = obj.data1;
  651. source.data2 = obj.data2;
  652. source.data3 = obj.data3;
  653. source.data4 = obj.data4;
  654. source.data5 = obj.data5;
  655. setGmValue(roomId, source);
  656. } else {
  657. source.data1.values = obj.data1 ? obj.data1 : source.data1.values;
  658. source.data2.values = obj.data2 ? obj.data2 : source.data2.values;
  659. source.data3.values = obj.data3 ? obj.data3 : source.data3.values;
  660. source.data4.values = obj.data4 ? obj.data4 : source.data4.values;
  661. source.data5.values = obj.data5 ? obj.data5 : source.data5.values;
  662. setGmValue(roomId, source);
  663. }
  664. }
  665.  
  666. setCheckboxChecked();
  667. dataText1.value = source.data1.values.join('|');
  668. dataText2.value = source.data2.values.join('|');
  669. dataText3.value = source.data3.values.join('|');
  670. dataText4.value = source.data4.values.join('|');
  671. dataText5.value = source.data5.values.join('|');
  672. initData();
  673. },
  674. initSettingPanel = () => {
  675. let settingPanel = document.getElementById('danmu-setting-panel');
  676. if (!settingPanel) {
  677. console.log('===> 进行弹幕设置面板初始化');
  678. let divButton = document.getElementsByClassName('bottom-actions p-relative')[0];
  679. if (divButton) {
  680. buildPanel(divButton);
  681. loadData();
  682. console.log('===> 弹幕设置面板初始化完成');
  683. } else {
  684. console.warn('===> bottom-actions节点丢失');
  685. return false;
  686. }
  687. }
  688.  
  689. return true;
  690. },
  691. heartBeat = async () => {
  692. console.log('===> 开始直播间心跳');
  693. const arg = `${hbInterval}|${roomId}|1|0`;
  694. const argBase64 = CryptoJS ? CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(arg)) : window.btoa(unescape(encodeURIComponent(arg)));
  695. try {
  696. const result = await fetch(`//live-trace.bilibili.com/xlive/rdata-interface/v1/heartbeat/webHeartBeat?hb=${encodeURIComponent(argBase64)}&pf=web`, {
  697. mode: 'cors',
  698. credentials: 'include',
  699. }).then(res => res.json());
  700. if (0 === result.code) {
  701. hbInterval = result.data.next_interval;
  702. console.log('===> 直播间心跳成功');
  703. } else {
  704. console.error(`===> ${roomId}直播间心跳失败`);
  705. }
  706. } catch (error) {
  707. console.error(`===> ${roomId}直播间心跳网络请求失败`, error);
  708. }
  709.  
  710. let hbTimer = workerTimer.setTimeout(() => {
  711. workerTimer.clearTimeout(hbTimer);
  712. heartBeat();
  713. }, hbInterval * 1000);
  714. },
  715. runStart = () => {
  716. if (isOldVersion()) {
  717. // alert('发随机弹幕脚本有新版本,请及时更新');
  718. window.location.href = parentUrl;
  719. // gmNotice({
  720. // text: '当前脚本不是最新版,请更新再使用功能',
  721. // title: '油猴脚本有更新,点击进行安装',
  722. // image: icoUrl,
  723. // highlight: true,
  724. // timeout: 120000,
  725. // onclick: () => window.open(parentUrl, '_blank')
  726. // });
  727. return;
  728. }
  729.  
  730. waiters[waiters.length] = workerTimer.setInterval(() => {
  731. if (initSettingPanel()) {
  732. clearWaiters();
  733. removeElements();
  734. signIn();
  735. heartBeat();
  736. console.log('===> 运行成功');
  737. } else {
  738. --waitCount;
  739. if (0 >= waitCount) {
  740. clearWaiters();
  741. console.log('===> 创建弹幕设置面板失败,停止初始化');
  742. }
  743. }
  744. }, 1500);
  745. };
  746.  
  747. initCss();
  748. // initScript();
  749. window.runStart = runStart;
  750. window.arrayInfo = arrayInfo;
  751. window.setGmNotice = setGmNotice;
  752. window.setGmGetValue = setGmGetValue;
  753. window.setGmSetValue = setGmSetValue;
  754. window.setGmDelValue = setGmDelValue;
  755. window.setParentData = setParentData;
  756. window.autoSendDanmuModuleLoaded = true;
  757. })();