b-live-random-send-test

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

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

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

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