b-live-random-send-test

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

目前为 2024-10-03 提交的版本。查看 最新版本

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

  1. // ==UserScript==
  2. // @version 1.7.2
  3. // ==/UserScript==
  4.  
  5.  
  6.  
  7. (function () {
  8. const blobURL = URL.createObjectURL(
  9. new Blob(
  10. [
  11. '(',
  12. function () {
  13. const ids = {};
  14. // 监听message 开始执行定时器或者销毁
  15. self.onmessage = (e) => {
  16. switch (e.data.command) {
  17. case 'interval:start': // 开启定时器
  18. const intervalId = setInterval(() => postMessage({ message: 'interval:tick', id: e.data.id }), e.data.interval);
  19. // postMessage({ message: 'interval:started', id: e.data.id });
  20. ids[e.data.id] = intervalId;
  21. break;
  22. case 'interval:clear': // 销毁
  23. clearInterval(ids[e.data.id]);
  24. postMessage({ message: 'interval:cleared', id: e.data.id });
  25. delete ids[e.data.id];
  26. break;
  27. case 'timeout:start':
  28. const timeoutId = setTimeout(() => postMessage({ message: 'timeout:tick', id: e.data.id }), e.data.timeout);
  29. // postMessage({ message: 'timeout:started', id: e.data.id });
  30. ids[e.data.id] = timeoutId;
  31. break;
  32. case 'timeout:clear':
  33. clearTimeout(ids[e.data.id]);
  34. postMessage({ message: 'timeout:cleared', id: e.data.id });
  35. delete ids[e.data.id];
  36. break;
  37. }
  38. };
  39. }.toString(),
  40. ')()',
  41. ],
  42. { type: 'application/javascript' },
  43. ),
  44. );
  45. const worker = new Worker(blobURL);
  46. URL.revokeObjectURL(blobURL); //用完释放URL对象
  47. const workerTimer = {
  48. id: 0,
  49. callbacks: {},
  50. setInterval: (cb, interval, context) => {
  51. const id = ++workerTimer.id;
  52. workerTimer.callbacks[id] = { fn: cb, context: context };
  53. worker.postMessage({ command: 'interval:start', interval: interval, id: id });
  54. return id;
  55. },
  56. setTimeout: (cb, timeout, context) => {
  57. const id = ++workerTimer.id;
  58. workerTimer.callbacks[id] = { fn: cb, context: context };
  59. worker.postMessage({ command: 'timeout:start', timeout: timeout, id: id });
  60. return id;
  61. },
  62.  
  63. // 监听worker 里面的定时器发送的message 然后执行回调函数
  64. onMessage: (e) => {
  65. switch (e.data.message) {
  66. case 'interval:tick':
  67. case 'timeout:tick':
  68. const callbackItem = workerTimer.callbacks[e.data.id];
  69. if (callbackItem && callbackItem.fn) {
  70. callbackItem.fn.apply(callbackItem.context);
  71. }
  72.  
  73. break;
  74. case 'interval:cleared':
  75. case 'timeout:cleared':
  76. delete workerTimer.callbacks[e.data.id];
  77. break;
  78. }
  79. },
  80.  
  81. // 往worker里面发送销毁指令
  82. clearInterval: (id) => worker.postMessage({ command: 'interval:clear', id: id }),
  83. clearTimeout: (id) => worker.postMessage({ command: 'timeout:clear', id: id }),
  84. };
  85. worker.onmessage = workerTimer.onMessage.bind(workerTimer);
  86.  
  87. let source = {
  88. version: 4,
  89. like: false,
  90. random: true,
  91. usePublic: false,
  92. data1: { available: true, values: ['弹幕①', '弹幕②'] },
  93. data2: { available: true, values: [] },
  94. data3: { available: true, values: [] },
  95. data4: { available: true, values: ['弹幕⑦'] },
  96. data5: { available: true, values: [] }
  97. },
  98. signInCheckbox, hideLoginGuideCheckbox, hideHarunaCheckbox, hideShopCheckbox, noSleepCheckbox, hideGiftControlCheckbox,
  99. hideRoomFeedCheckbox, hideRoomInfoCheckbox, hideNoticeCheckbox, hideFooterCheckbox, lotteryCheckbox, closeLotteryCheckbox,
  100. hesitationCheckbox, hidePrivacyCheckbox, hideRoomStatusCheckbox, setPublicCheckbox, hideBusinessCheckbox,
  101. hideRankListCheckbox, showLiveAreaCheckbox,
  102. group1Checkbox, group2Checkbox, group3Checkbox, group4Checkbox, group5Checkbox,
  103. rdCheckbox, usePublicCheckbox, autoLikeCheckbox,
  104. dmInput, divSetting, dataText1, dataText2, dataText3, dataText4, dataText5, signInput, hesitateInput,
  105. dmButtonSend, beforeSpan, afterSpan, spanApplyMsg, divUpdateInfo, btnStartText,
  106. baseInfo = {}, config = {}, waiters = [], data = [], ws = null, wsHostList = [], wsCmds = [],
  107. sendTimer = null, signInTimer = null, miniCloseTimer, noSleepTimer, noSleepTimeouter, btnLotteryTimer,
  108. divSendBtnTimer, autoLikeTimer, clickLikeBtnTimer, wsHeartBeatTimer, wsReconnectWsTimer,
  109. count = 0, waitCount = 200, arrayIndex = 0, default_timeout = 600, wsSequence = 0, wsHostIndex = 0, wsConnectCount = 30,
  110. lotteryChecked = 'lottery_checked', closeLotteryChecked = 'close_lottery_checked', hesitationChecked = 'hesitation_checked',
  111. hesitationExpiry = 'hesitation_expiry', wsToken = '',
  112. gmNotice = obj => { alert(''); },
  113. getGmValue = (key, defaultValue) => { return null; },
  114. setGmValue = (key, obj) => { console.warn('===> No implementation "setGmValue" method.'); },
  115. delGmValue = key => { console.warn('===> No implementation "delGmValue" method.'); };
  116.  
  117. const version = '1.7.2', upodateInfo = 'WebScoket', noticeTimeout = 10e3,
  118. icoUrl = 'https://www.bilibili.com/favicon.ico',
  119. roomId = window.location.pathname.replace(/^\/(\S+\/)*/g, ''),
  120. ruid = window.__STORE__?.baseInfoAnchor?.uid || 0,
  121. uid = window.__STORE__?.baseInfoUser?.uid || 0,
  122. textEncoder = new TextEncoder('utf-8'),
  123. textDecoder = new TextDecoder('utf-8'),
  124. setGmGetValue = callback => getGmValue = callback,
  125. setGmSetValue = callback => setGmValue = callback,
  126. setGmDelValue = callback => delGmValue = callback,
  127. setGmNotice = callback => gmNotice = callback,
  128. setBaseInfo = obj => baseInfo = obj,
  129. arrayInfo = () => console.info(data),
  130. isNull = str => {
  131. if (!str || str == "") {
  132. return true;
  133. }
  134.  
  135. let regu = "^[ ]+$";
  136. let re = new RegExp(regu);
  137. return re.test(str);
  138. },
  139. compareVersion = (ver1, ver2) => {
  140. if (isNull(ver1)) return -1;
  141. if (isNull(ver2)) return 1;
  142.  
  143. const arr1 = ver1.split('.').map(x => x * 1),
  144. arr2 = ver2.split('.').map(x => x * 1),
  145. len = Math.max(arr1.length, arr2.length);
  146. for (let i = 0; i < len; i++) {
  147. if ((arr1[i] || 0) > (arr2[i] || 0)) return 1;
  148. if ((arr1[i] || 0) < (arr2[i] || 0)) return -1;
  149. }
  150.  
  151. return 0;
  152. },
  153. getCookie = name => {
  154. let key = name + '=';
  155. const cookies = document.cookie.split(';');
  156. cookies.forEach(c => {
  157. let item = c.trim();
  158. if (0 == item.indexOf(key)) {
  159. return item.substring(key.length, item.length);
  160. }
  161. });
  162. return '';
  163. },
  164. readInt = (buffer, start, len) => {
  165. let result = 0;
  166. for (let i = len - 1; i >= 0; i--) {
  167. result += Math.pow(256, len - i - 1) * buffer[start + i];
  168. }
  169. return result;
  170. },
  171. writeInt = (buffer, start, len, value) => {
  172. let i = 0;
  173. while (i < len) {
  174. buffer[start + i] = value / Math.pow(256, len - i - 1);
  175. i++;
  176. }
  177. },
  178. encode = (str, op) => {
  179. ++wsSequence;
  180. let data = textEncoder.encode(str);
  181. let packetLen = 16 + data.byteLength;
  182. let header = [0, 0, 0, 0, 0, 16, 0, 1, 0, 0, 0, op, 0, 0, 0, 0];
  183. writeInt(header, 0, 4, packetLen);
  184. writeInt(header, 12, 4, wsSequence);
  185. console.log(`===> header: ${header}`);
  186. return (new Uint8Array(header.concat(...data))).buffer;
  187. },
  188. decode = blob => {
  189. return new Promise((resolve, reject) => {
  190. let reader = new FileReader();
  191. reader.onload = e => {
  192. let buffer = new Uint8Array(e.target.result), result = {};
  193. result.packetLen = readInt(buffer, 0, 4);
  194. result.headerLen = readInt(buffer, 4, 2);
  195. result.ver = readInt(buffer, 6, 2);
  196. result.op = readInt(buffer, 8, 4);
  197. result.seq = readInt(buffer, 12, 4);
  198. if (result.op === 5) {
  199. result.body = [];
  200. let offset = 0;
  201. while (offset < buffer.length) {
  202. let packetLen = readInt(buffer, offset + 0, 4);
  203. let headerLen = 16; // readInt(buffer,offset + 4, 2);
  204. let data = buffer.slice(offset + headerLen, offset + packetLen);
  205. let body = '';
  206. try {
  207. body = textDecoder.decode(pako.inflate(data));
  208. }
  209. catch (e) {
  210. console.log(`===> pako.inflate()解压失败,直接进行解码`);
  211. body = textDecoder.decode(data);
  212. }
  213.  
  214. if (body) {
  215. console.log(`===> 解码后的body数据【${body}】`);
  216. const group = body.split(/[\x00-\x1f]+/);
  217. console.log(`===> 分割后的group数据【${group}】`);
  218. group.forEach(item => {
  219. try {
  220. const parsedItem = JSON.parse(item);
  221. if (typeof parsedItem === 'object') {
  222. result.body.push(parsedItem);
  223. } else {
  224. console.log(`===> JSON.parse(item)反序列化出来的不是object类型【${item}】`)
  225. }
  226. }
  227. catch (e) {
  228. console.warn(`===> 反序列化失败【${e}】,原数据【${item}】`);
  229. }
  230. });
  231. }
  232.  
  233. offset += packetLen;
  234. }
  235. } else if (result.op === 3) {
  236. result.body = {count: readInt(buffer, 16, 4)};
  237. }
  238.  
  239. resolve(result);
  240. };
  241. reader.readAsArrayBuffer(blob);
  242. });
  243. },
  244. initCss = () => {
  245. let linkElement = document.createElement('link');
  246. linkElement.rel = 'stylesheet';
  247. linkElement.href = 'https://unpkg.zhimg.com/element-ui/lib/theme-chalk/index.css';
  248. document.head.appendChild(linkElement);
  249.  
  250. let customerStyle = document.createElement('style');
  251. customerStyle.setAttribute('type', 'text/css');
  252. 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;}.switch-check-group{margin-top:5px;width:95%;}.danmu-random-setting-panel{background-color:#e7f1fb;border-radius:10px;width:100%;height:100%;overflow-y:auto;position:absolute;left:0px;top:0px;z-index:999;display:none;}.danmu-random-setting-panel::-webkit-scrollbar{width:4px;height:4px;}.danmu-random-setting-panel::-webkit-scrollbar-thumb{border-radius:5px;-webkit-box-shadow:inset 0 0 5px rgba(0,0,0,0.2);background:rgba(0,0,0,0.2);}.danmu-random-setting-panel::-webkit-scrollbar-track{-webkit-box-shadow:inset 0 0 5px rgba(0,0,0,0.2);border-radius:0;background:rgba(0,0,0,0.1);}.danmu-random-setting-title{text-align:center;font-size:16px;font-weight:700;color:#1c5adc;line-height:30px;}.danmu-random-setting-title-sub{display:inline-block;color:#ee8b8b;height:24px;vertical-align:sub;-webkit-transform:scale(0.7);}.danmu-random-setting-tips{color:#0b81cc;text-align:center;font-style:italic;}.module-update-info{color:#0b81cc;text-indent:2em;font-size:13px;font-weight:700;margin:8px 0;padding:2px 0;background-color:#ffffff;}.update-info-text{color:#f00;vertical-align:-webkit-baseline-middle;}.danmu-random-setting-bottom{width:100%;line-height:35px;}.danmu-random-switch-button-title{font-size:14px;vertical-align:middle;margin-left:5px;color:#095ca2;cursor:help;}.danmu-random-setting-success-tips{text-align:center;display:inline-block;vertical-align:middle;width:40%;}.danmu-random-setting-success-text{font-size:16px;color:#128712;display:none;}.danmu-random-set-button-container{display:inline-block;vertical-align:middle;}.global-setting-tip{padding-left:10px;color:red;font-size:14px;font-weight:700;cursor:help;}.disabled{color:#ababab;cursor:not-allowed;}.clean-cache-btn{min-width:70px;font-size:14px;border-radius:4px;color:#fff;background:#d99d1b;border:0px;cursor:pointer;vertical-align:middle;line-height:30px;}.clean-cache-btn:hover{background:rgba(217,157,27,0.8);color:#000}.danmu-btn{min-width:65px;height:24px;font-size:12px;border-radius:4px;color:rgb(255,255,255);background:rgb(217,157,27);border:0px;cursor:pointer;line-height:1;display:inline-flex;justify-content:center;align-items:center;}.danmu-btn:hover{background:var(--color)!important;}.danmu-text-span{color:#095ca2;font-size:20px;vertical-align:middle;font-weight:700}.danmu-second-input{width:70px;height:20px;margin:0px 3px;border:0px;border-radius:3px;font-size:18px}.not-display{display:none !important;}';
  253. document.head.appendChild(customerStyle);
  254. },
  255. initScript = () => {
  256. let script = document.createElement('script');
  257. script.type = 'text/javascript';
  258. script.src = 'https://cdnjs.cloudflare.com/ajax/libs/pako/2.1.0/pako_inflate.min.js';
  259. document.head.appendChild(script);
  260. },
  261. getCurrentTimestamp = () => new Date().getTime(),
  262. send = (msg, index) => {
  263. let dmTextArea = document.getElementById('chat-control-panel-vm').querySelector('textarea');
  264. if (!dmTextArea) {
  265. alert('找不到输入弹幕文本框,请尝试刷新页面');
  266. return;
  267. }
  268. if (!isNull(dmTextArea.value)) {
  269. console.log(`===> 有内容正在编辑,跳过该次发送任务`);
  270. return;
  271. }
  272.  
  273. let btnSend = document.getElementsByClassName('bl-button bl-button--primary')[0];
  274. if (!btnSend) {
  275. alert('找不到发送按钮,请尝试刷新页面');
  276. return;
  277. }
  278. dmTextArea.value = msg;
  279. dmTextArea.dispatchEvent(new Event('input', { "bubbles": true, "cancelable": true }));
  280. btnSend.click();
  281. // ++count;
  282. // console.log('===> ' + new Date().toLocaleString() + ' 弹幕发送成功 ' + count + ' 次,第【' + index + '】条数据 === ' + msg);
  283. },
  284. randomSort = arr => {
  285. for (let i = 0; i < arr.length; i++) {
  286. const rdIndex = Math.floor(Math.random() * arr.length);
  287. const temp = arr[i];
  288. arr[i] = arr[rdIndex];
  289. arr[rdIndex] = temp;
  290. }
  291.  
  292. return arr;
  293. },
  294. clearWaiters = () => {
  295. for (let i = 0; i < waiters.length; i++) {
  296. workerTimer.clearInterval(waiters[i]);
  297. waiters[i] = null;
  298. }
  299.  
  300. waiters = [];
  301. },
  302. signInFunc = () => {
  303. if (signInCheckbox.checked) {
  304. if (!signInTimer) {
  305. let defaultText = isNull(baseInfo.text) ? baseInfo.text : '打卡';
  306. let timestamp = new Date(new Date(new Date().setDate(new Date().getDate() + 1)).toDateString()).getTime() - getCurrentTimestamp();
  307. console.log('===> 设置凌晨打卡定时器【' + timestamp + '】');
  308. signInput.value = isNull(config.signInText) ? defaultText : config.signInText;
  309. signInTimer = workerTimer.setTimeout(() => {
  310. send(isNull(signInput.value) ? defaultText : signInput.value, 0);
  311. console.log('===> 设置下一次打卡');
  312. workerTimer.clearTimeout(signInTimer);
  313. signInTimer = null;
  314. signInFunc();
  315. }, timestamp);
  316. }
  317. } else if (signInTimer) {
  318. console.log('===> 关闭自动打卡功能');
  319. workerTimer.clearTimeout(signInTimer);
  320. signInTimer = null;
  321. }
  322. },
  323. setSource = obj => {
  324. dataText1.value = obj.data1.values.length ? obj.data1.values.join('|') : [];
  325. dataText2.value = obj.data2.values.length ? obj.data2.values.join('|') : [];
  326. dataText3.value = obj.data3.values.length ? obj.data3.values.join('|') : [];
  327. dataText4.value = obj.data4.values.length ? obj.data4.values.join('|') : [];
  328. dataText5.value = obj.data5.values.length ? obj.data5.values.join('|') : [];
  329. },
  330. runCheckbox = () => {
  331. usePublicFunc();
  332. hideFooterFunc();
  333. hideGiftControlFunc();
  334. hideLoginGuideFunc();
  335. hideHarunaFunc();
  336. hideShopFunc();
  337. hidePrivacyFunc();
  338. hideRoomStatusFunc();
  339. hideBusinessFunc();
  340. hideRankListFunc();
  341. noSleepFunc();
  342. signInFunc();
  343. lotteryFunc();
  344. hesitationFunc();
  345. closeLotteryFunc();
  346. autoLikeFunc();
  347. // autoPlay();
  348. hideRoomFeedFunc();
  349. hideRoomInfoFunc();
  350. hideNoticeFunc();
  351. showLiveAreaFunc();
  352. let hideTimeout = workerTimer.setTimeout(() => {
  353. workerTimer.clearTimeout(hideTimeout);
  354. hideRoomFeedFunc();
  355. hideRoomInfoFunc();
  356. hideNoticeFunc();
  357. showLiveAreaFunc();
  358. }, 1e3);
  359. },
  360. setOperationSetting = () => {
  361. setPublicCheckbox.checked = false;
  362. rdCheckbox.checked = source.random;
  363. autoLikeCheckbox.checked = source.like;
  364. usePublicCheckbox.checked = source.usePublic;
  365. dmInput.value = source.interval || default_timeout;
  366. group1Checkbox.checked = source.data1.available;
  367. group2Checkbox.checked = source.data2.available;
  368. group3Checkbox.checked = source.data3.available;
  369. group4Checkbox.checked = source.data4.available;
  370. group5Checkbox.checked = source.data5.available;
  371.  
  372. signInCheckbox.checked = config.autoSignIn;
  373. noSleepCheckbox.checked = config.noSleep;
  374. hideLoginGuideCheckbox.checked = config.hideLoginGuide;
  375. hideHarunaCheckbox.checked = config.hideHaruna;
  376. hideShopCheckbox.checked = config.hideShop;
  377. hideGiftControlCheckbox.checked = config.hideGift;
  378. hideRoomFeedCheckbox.checked = config.hideRoomFeed;
  379. hideRoomInfoCheckbox.checked = config.hideRoomInfo;
  380. hideNoticeCheckbox.checked = config.hideNotice;
  381. hideFooterCheckbox.checked = config.hideFooter;
  382. lotteryCheckbox.checked = config.lottery;
  383. hesitationCheckbox.checked = config.hesitation;
  384. closeLotteryCheckbox.checked = config.closeLottery;
  385. hidePrivacyCheckbox.checked = config.noPrivacy;
  386. hideRoomStatusCheckbox.checked = config.hideWatermark;
  387. hideBusinessCheckbox.checked = config.hideBusiness;
  388. hideRankListCheckbox.checked = config.hideRankList;
  389. showLiveAreaCheckbox.checked = config.showLiveArea;
  390. runCheckbox();
  391. },
  392. openSetting = () => divSetting.style.display = 'block',
  393. closeSetting = () => {
  394. setOperationSetting();
  395. divSetting.style.display = 'none';
  396. },
  397. initData = () => {
  398. if (source.data1.values.length <= 0
  399. && source.data2.values.length <= 0
  400. && source.data3.values.length <= 0
  401. && source.data4.values.length <= 0
  402. && source.data5.values.length <= 0) {
  403. return data ? data : [];
  404. }
  405.  
  406. let result = [];
  407. result = source.data1.available ? result.concat(source.data1.values) : result;
  408. result = source.data2.available ? result.concat(source.data2.values) : result;
  409. result = source.data3.available ? result.concat(source.data3.values) : result;
  410. result = source.data4.available ? result.concat(source.data4.values) : result;
  411. result = source.data5.available ? result.concat(source.data5.values) : result;
  412. data = result;
  413. rdCheckbox.checked ? data = randomSort(result) : arrayIndex = 0;
  414. },
  415. flashMsg = (txt, back, color) => {
  416. spanApplyMsg.textContent = txt;
  417. spanApplyMsg.style.display = 'block';
  418. if (color) {
  419. spanApplyMsg.style.color = color;
  420. }
  421. else {
  422. spanApplyMsg.style.color = '#128712';
  423. }
  424.  
  425. let hideTipTimer = workerTimer.setTimeout(() => {
  426. workerTimer.clearTimeout(hideTipTimer);
  427. spanApplyMsg.style.display = 'none';
  428. spanApplyMsg.textContent = '';
  429. if (back) {
  430. divSetting.style.display = 'none';
  431. }
  432. }, 1.5e3);
  433. },
  434. save = () => {
  435. if (!baseInfo || isNull(baseInfo.default) || isNull(baseInfo.config)) {
  436. flashMsg('保存失败', false, 'red');
  437. return;
  438. }
  439. if (baseInfo.config) {
  440. config.autoSignIn = signInCheckbox.checked;
  441. config.signInText = signInput.value;
  442. config.noSleep = noSleepCheckbox.checked;
  443. config.hideLoginGuide = hideLoginGuideCheckbox.checked;
  444. config.hideHaruna = hideHarunaCheckbox.checked;
  445. config.hideShop = hideShopCheckbox.checked;
  446. config.hideGift = hideGiftControlCheckbox.checked;
  447. config.hideRoomFeed = hideRoomFeedCheckbox.checked;
  448. config.hideRoomInfo = hideRoomInfoCheckbox.checked;
  449. config.hideNotice = hideNoticeCheckbox.checked;
  450. config.hideFooter = hideFooterCheckbox.checked;
  451. config.lottery = lotteryCheckbox.checked;
  452. window.localStorage.setItem(lotteryChecked, lotteryCheckbox.checked);
  453. config.hesitation = hesitationCheckbox.checked;
  454. window.localStorage.setItem(hesitationChecked, hesitationCheckbox.checked);
  455. config.hesitationExpiry = hesitateInput.value;
  456. window.localStorage.setItem(hesitationExpiry, hesitateInput.value);
  457. config.closeLottery = closeLotteryCheckbox.checked;
  458. window.localStorage.setItem(closeLotteryChecked, config.closeLottery);
  459. config.noPrivacy = hidePrivacyCheckbox.checked;
  460. config.hideWatermark = hideRoomStatusCheckbox.checked;
  461. config.hideBusiness = hideBusinessCheckbox.checked;
  462. config.hideRankList = hideRankListCheckbox.checked;
  463. config.showLiveArea = showLiveAreaCheckbox.checked;
  464. setGmValue(baseInfo.config, config);
  465. }
  466.  
  467. let v1 = isNull(dataText1.value) ? [] : dataText1.value.split('|'),
  468. v2 = isNull(dataText2.value) ? [] : dataText2.value.split('|'),
  469. v3 = isNull(dataText3.value) ? [] : dataText3.value.split('|'),
  470. v4 = isNull(dataText4.value) ? [] : dataText4.value.split('|'),
  471. v5 = isNull(dataText5.value) ? [] : dataText5.value.split('|');
  472. source.interval = dmInput.value;
  473. source.random = rdCheckbox.checked;
  474. source.like = autoLikeCheckbox.checked;
  475. source.usePublic = usePublicCheckbox.checked;
  476. source.data1.available = group1Checkbox.checked;
  477. source.data2.available = group2Checkbox.checked;
  478. source.data3.available = group3Checkbox.checked;
  479. source.data4.available = group4Checkbox.checked;
  480. source.data5.available = group5Checkbox.checked;
  481. if (!usePublicCheckbox.checked) {
  482. source.data1.values = v1;
  483. source.data2.values = v2;
  484. source.data3.values = v3;
  485. source.data4.values = v4;
  486. source.data5.values = v5;
  487. }
  488. setGmValue(roomId, source);
  489. if (setPublicCheckbox.checked) {
  490. let ps = {data1:{},data2:{},data3:{},data4:{},data5:{}};
  491. ps.data1.values = v1;
  492. ps.data2.values = v2;
  493. ps.data3.values = v3;
  494. ps.data4.values = v4;
  495. ps.data5.values = v5;
  496. setGmValue(baseInfo.default, ps);
  497. }
  498. if (usePublicCheckbox.checked) {
  499. source.data1.values = v1;
  500. source.data2.values = v2;
  501. source.data3.values = v3;
  502. source.data4.values = v4;
  503. source.data5.values = v5;
  504. }
  505. initData();
  506. flashMsg('设置成功', true);
  507. },
  508. cleanCache = () => {
  509. if (baseInfo.config && config) {
  510. config.script = '';
  511. config.moduleVersion = '0.0.0';
  512. config.lastUpdate = '清除脚本缓存';
  513. setGmValue(baseInfo.config, config);
  514. flashMsg('清除成功');
  515. } else {
  516. console.warn('元数据丢失');
  517. flashMsg('操作失败', false, 'red');
  518. }
  519. },
  520. danmu = () => {
  521. if (data.length < 1) {
  522. // gmNotice({
  523. // text: '请任意在一个分组里输入一条弹幕',
  524. // title: '没有弹幕数据,请先设置',
  525. // image: icoUrl,
  526. // highlight: true,
  527. // timeout: noticeTimeout
  528. // });
  529. alert('请先设置弹幕');
  530. return false;
  531. }
  532. if (rdCheckbox.checked) {
  533. arrayIndex = Math.floor((Math.random() * data.length));
  534. }
  535.  
  536. send(data[arrayIndex], arrayIndex);
  537. ++arrayIndex;
  538. if (arrayIndex >= data.length) {
  539. arrayIndex = 0;
  540. }
  541.  
  542. return true;
  543. },
  544. offOrOn = () => {
  545. let timeout = 0;
  546. if (sendTimer) {
  547. workerTimer.clearInterval(sendTimer);
  548. sendTimer = null;
  549. dmButtonSend.style.removeProperty('background');
  550. dmButtonSend.style.removeProperty('--color');
  551. dmInput.removeAttribute("disabled");
  552. btnStartText.textContent = '开始';
  553. } else {
  554. timeout = (dmInput.value || default_timeout) * 1e3;
  555. if (!danmu()) {
  556. return;
  557. }
  558.  
  559. sendTimer = workerTimer.setInterval(danmu, timeout);
  560. dmButtonSend.style.background = 'rgba(255,0,0,1)';
  561. dmButtonSend.style.setProperty('--color', 'rgba(255,0,0,0.8)');
  562. dmInput.setAttribute('disabled', 'disabled');
  563. btnStartText.textContent = '停止';
  564. }
  565. },
  566. createSwitch = (id, txt, title, func, container, indent, width, hidden, rear, disabled) => {
  567. let checkbox = document.createElement('input');
  568. checkbox.type = 'checkbox';
  569. checkbox.id = id;
  570. checkbox.checked = false;
  571. if (func && !disabled) {
  572. checkbox.addEventListener('click', func);
  573. }
  574.  
  575. let lblCheckbox = document.createElement('label');
  576. lblCheckbox.setAttribute('for', id);
  577. lblCheckbox.classList.add('switch-check-label');
  578.  
  579. let descSpan = document.createElement('span');
  580. descSpan.textContent = txt;
  581. descSpan.title = title;
  582. descSpan.classList.add('danmu-random-switch-button-title');
  583. if (disabled) {
  584. checkbox.disabled = true;
  585. checkbox.classList.add('disabled');
  586. lblCheckbox.classList.add('disabled');
  587. descSpan.classList.add('disabled');
  588. }
  589.  
  590. let divCheckbox = document.createElement('div');
  591. divCheckbox.id = id + 'Div';
  592. divCheckbox.classList.add('switch-check');
  593. divCheckbox.classList.add('switch-check-group');
  594. divCheckbox.appendChild(checkbox);
  595. divCheckbox.appendChild(lblCheckbox);
  596. divCheckbox.appendChild(descSpan);
  597. if (!isNull(indent)) {
  598. divCheckbox.style.marginLeft = indent;
  599. }
  600. if (!isNull(width)) {
  601. divCheckbox.style.width = width;
  602. }
  603. if (hidden) {
  604. divCheckbox.style.setProperty('display', 'none');
  605. }
  606. if (rear) {
  607. divCheckbox.appendChild(rear);
  608. }
  609.  
  610. container.appendChild(divCheckbox);
  611. return checkbox;
  612. },
  613. buildPanel = () => {
  614. /* ----------------------------------------- head ----------------------------------------- */
  615. let divSettingTitle = document.createElement('div');
  616. divSettingTitle.textContent = '弹幕设置';
  617. divSettingTitle.classList.add('danmu-random-setting-title');
  618.  
  619. let divSub = document.createElement('div');
  620. divSub.textContent = version;
  621. divSub.classList.add('danmu-random-setting-title-sub');
  622. divSettingTitle.appendChild(divSub);
  623.  
  624. let divTip = document.createElement('div');
  625. divTip.classList.add('danmu-random-setting-tips');
  626. divTip.innerHTML = '任一分组内输入弹幕即可,多条用<span style="color:#dc6b07;margin:0 2px 0 4px;font-weight:700;font-style:normal;">竖线</span>分隔';
  627.  
  628. divUpdateInfo = document.createElement('div');
  629. divUpdateInfo.classList.add('module-update-info');
  630. divUpdateInfo.innerHTML = `<span class="update-info-text">消息提示:</span><span class="update-info-text" style="color:#0f6ba6;">${baseInfo.msg || upodateInfo}</span>`;
  631. /* ----------------------------------------- head ----------------------------------------- */
  632.  
  633. /* ----------------------------------------- textarea 1 ----------------------------------------- */
  634. let divText1 = document.createElement('div');
  635. divText1.textContent = '分组 1 :';
  636. divText1.classList.add('danmu-group-title');
  637.  
  638. group1Checkbox = document.createElement('input');
  639. group1Checkbox.type = 'checkbox';
  640. group1Checkbox.id = 'group1Checkbox';
  641. group1Checkbox.checked = true;
  642.  
  643. let lblGroup1Checkbox = document.createElement('label');
  644. lblGroup1Checkbox.setAttribute('for', 'group1Checkbox');
  645. lblGroup1Checkbox.classList.add('switch-check-label');
  646.  
  647. let divGroup1Checkbox = document.createElement('div');
  648. divGroup1Checkbox.classList.add('switch-check');
  649. divGroup1Checkbox.appendChild(group1Checkbox);
  650. divGroup1Checkbox.appendChild(lblGroup1Checkbox);
  651.  
  652. dataText1 = document.createElement('textarea');
  653. dataText1.classList.add('danmu-group-textarea');
  654. dataText1.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  655. /* ----------------------------------------- textarea 1 ----------------------------------------- */
  656.  
  657. /* ----------------------------------------- textarea 2 ----------------------------------------- */
  658. let divText2 = document.createElement('div');
  659. divText2.textContent = '分组 2 :';
  660. divText2.classList.add('danmu-group-title');
  661.  
  662. group2Checkbox = document.createElement('input');
  663. group2Checkbox.type = 'checkbox';
  664. group2Checkbox.id = 'group2Checkbox';
  665. group2Checkbox.checked = true;
  666.  
  667. let lblGroup2Checkbox = document.createElement('label');
  668. lblGroup2Checkbox.setAttribute('for', 'group2Checkbox');
  669. lblGroup2Checkbox.classList.add('switch-check-label');
  670.  
  671. let divGroup2Checkbox = document.createElement('div');
  672. divGroup2Checkbox.classList.add('switch-check');
  673. divGroup2Checkbox.appendChild(group2Checkbox);
  674. divGroup2Checkbox.appendChild(lblGroup2Checkbox);
  675.  
  676. dataText2 = document.createElement('textarea');
  677. dataText2.classList.add('danmu-group-textarea');
  678. dataText2.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  679. /* ----------------------------------------- textarea 2 ----------------------------------------- */
  680.  
  681. /* ----------------------------------------- textarea 3 ----------------------------------------- */
  682. let divText3 = document.createElement('div');
  683. divText3.textContent = '分组 3 :';
  684. divText3.classList.add('danmu-group-title');
  685.  
  686. group3Checkbox = document.createElement('input');
  687. group3Checkbox.type = 'checkbox';
  688. group3Checkbox.id = 'group3Checkbox';
  689. group3Checkbox.checked = true;
  690.  
  691. let lblGroup3Checkbox = document.createElement('label');
  692. lblGroup3Checkbox.setAttribute('for', 'group3Checkbox');
  693. lblGroup3Checkbox.classList.add('switch-check-label');
  694.  
  695. let divGroup3Checkbox = document.createElement('div');
  696. divGroup3Checkbox.classList.add('switch-check');
  697. divGroup3Checkbox.appendChild(group3Checkbox);
  698. divGroup3Checkbox.appendChild(lblGroup3Checkbox);
  699.  
  700. dataText3 = document.createElement('textarea');
  701. dataText3.classList.add('danmu-group-textarea');
  702. dataText3.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  703. /* ----------------------------------------- textarea 3 ----------------------------------------- */
  704.  
  705. /* ----------------------------------------- textarea 4 ----------------------------------------- */
  706. let divText4 = document.createElement('div');
  707. divText4.textContent = '分组 4 :';
  708. divText4.classList.add('danmu-group-title');
  709.  
  710. group4Checkbox = document.createElement('input');
  711. group4Checkbox.type = 'checkbox';
  712. group4Checkbox.id = 'group4Checkbox';
  713. group4Checkbox.checked = true;
  714.  
  715. let lblGroup4Checkbox = document.createElement('label');
  716. lblGroup4Checkbox.setAttribute('for', 'group4Checkbox');
  717. lblGroup4Checkbox.classList.add('switch-check-label');
  718.  
  719. let divGroup4Checkbox = document.createElement('div');
  720. divGroup4Checkbox.classList.add('switch-check');
  721. divGroup4Checkbox.appendChild(group4Checkbox);
  722. divGroup4Checkbox.appendChild(lblGroup4Checkbox);
  723.  
  724. dataText4 = document.createElement('textarea');
  725. dataText4.classList.add('danmu-group-textarea');
  726. dataText4.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  727. /* ----------------------------------------- textarea 4 ----------------------------------------- */
  728.  
  729. /* ----------------------------------------- textarea 5 ----------------------------------------- */
  730. let divText5 = document.createElement('div');
  731. divText5.textContent = '分组 5 :';
  732. divText5.classList.add('danmu-group-title');
  733.  
  734. group5Checkbox = document.createElement('input');
  735. group5Checkbox.type = 'checkbox';
  736. group5Checkbox.id = 'group5Checkbox';
  737. group5Checkbox.checked = true;
  738.  
  739. let lblGroup5Checkbox = document.createElement('label');
  740. lblGroup5Checkbox.setAttribute('for', 'group5Checkbox');
  741. lblGroup5Checkbox.classList.add('switch-check-label');
  742.  
  743. let divGroup5Checkbox = document.createElement('div');
  744. divGroup5Checkbox.classList.add('switch-check');
  745. divGroup5Checkbox.appendChild(group5Checkbox);
  746. divGroup5Checkbox.appendChild(lblGroup5Checkbox);
  747.  
  748. dataText5 = document.createElement('textarea');
  749. dataText5.classList.add('danmu-group-textarea');
  750. dataText5.setAttribute('placeholder', '请输入弹幕,多条弹幕请用“|”分隔');
  751. /* ----------------------------------------- textarea 5 ----------------------------------------- */
  752.  
  753.  
  754. /* ----------------------------------------- send interval ----------------------------------------- */
  755. beforeSpan = document.createElement('span');
  756. beforeSpan.textContent = '设置弹幕每';
  757. beforeSpan.classList.add('danmu-text-span');
  758. beforeSpan.style.marginLeft = '4px';
  759.  
  760. dmInput = document.createElement('input');
  761. dmInput.value = default_timeout;
  762. dmInput.classList.add('danmu-second-input');
  763. dmInput.setAttribute('oninput', "this.value = this.value.replace(/[^0-9]/g, '')");
  764.  
  765. afterSpan = document.createElement('span');
  766. afterSpan.textContent = '秒发送一次';
  767. afterSpan.classList.add('danmu-text-span');
  768. afterSpan.style.marginRight = '4px';
  769.  
  770. let divSendInterval = document.createElement('div');
  771. divSendInterval.appendChild(beforeSpan);
  772. divSendInterval.appendChild(dmInput);
  773. divSendInterval.appendChild(afterSpan);
  774. /* ----------------------------------------- send interval ----------------------------------------- */
  775.  
  776.  
  777. /* ----------------------------------------- different room setting ----------------------------------------- */
  778. let divRoomSetting = document.createElement('div');
  779. divRoomSetting.appendChild(divText1);
  780. divRoomSetting.appendChild(divGroup1Checkbox);
  781. divRoomSetting.appendChild(dataText1);
  782. divRoomSetting.appendChild(divText2);
  783. divRoomSetting.appendChild(divGroup2Checkbox);
  784. divRoomSetting.appendChild(dataText2);
  785. divRoomSetting.appendChild(divText3);
  786. divRoomSetting.appendChild(divGroup3Checkbox);
  787. divRoomSetting.appendChild(dataText3);
  788. divRoomSetting.appendChild(divText4);
  789. divRoomSetting.appendChild(divGroup4Checkbox);
  790. divRoomSetting.appendChild(dataText4);
  791. divRoomSetting.appendChild(divText5);
  792. divRoomSetting.appendChild(divGroup5Checkbox);
  793. divRoomSetting.appendChild(dataText5);
  794. divRoomSetting.appendChild(divSendInterval);
  795. rdCheckbox = createSwitch('rdCheckbox', '随机从上面的弹幕中选出一条发送', '将合并所有分组数据,从中随机选出一条发送', null, divRoomSetting);
  796. usePublicCheckbox = createSwitch('usePublicCheckbox', '使用共用弹幕源', '使用设置为共用弹幕作为弹幕源', usePublicFunc, divRoomSetting);
  797. autoLikeCheckbox = createSwitch('autoLikeCheckbox', '自动点赞该直播间', '每15秒内随机一个时间点点赞一次直播间(点赞1000次大概需要4小时)', autoLikeFunc, divRoomSetting, null, null, null, null);
  798.  
  799. let operationDescription = document.createElement('div');
  800. operationDescription.textContent = '以上设置对应各个直播间独立保存,无需刷新';
  801. operationDescription.title = '请点击设置面板底下的“✓”进行保存';
  802. operationDescription.classList.add('global-setting-tip');
  803. operationDescription.classList.add('switch-check-group');
  804. divRoomSetting.appendChild(operationDescription);
  805. /* ----------------------------------------- different room setting ----------------------------------------- */
  806.  
  807. /* ----------------------------------------- global setting ----------------------------------------- */
  808. let divGlobalSetting = document.createElement('div');
  809. divGlobalSetting.style.margin = '20px 0 10px';
  810.  
  811. operationDescription = document.createElement('div');
  812. operationDescription.textContent = '以下设置,需刷新其它直播间才能适用';
  813. operationDescription.title = '请点击设置面板底下的“✓”进行保存';
  814. operationDescription.classList.add('global-setting-tip');
  815. operationDescription.classList.add('switch-check-group');
  816. divGlobalSetting.appendChild(operationDescription);
  817.  
  818. signInput = document.createElement('input');
  819. signInput.id = 'signInputText';
  820. signInput.style.border = '0';
  821. signInput.style.width = '90px';
  822. signInput.setAttribute('placeholder', '输入打卡的文字');
  823.  
  824. hesitateInput = document.createElement('input');
  825. hesitateInput.style.border = '0';
  826. hesitateInput.style.width = '55px';
  827. hesitateInput.setAttribute('placeholder', '单位:秒');
  828. hesitateInput.setAttribute('oninput', "this.value = this.value.replace(/[^0-9]/g, '')");
  829.  
  830. setPublicCheckbox = createSwitch('setPublicCheckbox', '设为共用弹幕源', '把这个直播间的弹幕共享给其它直播间使用,先后设置时,后面的会覆盖前面的设置', setPublicFunc, divGlobalSetting);
  831. signInCheckbox = createSwitch('signInCheckbox', '打卡弹幕(需登录):', '每日零点发送一条打卡弹幕', signInFunc, divGlobalSetting, null, null, false, signInput);
  832. lotteryCheckbox = createSwitch('lotteryCheckbox', '自动参与天选时刻抽奖(需登录)', '自动点击参与按钮,请确保已经登录了阿B账号', lotteryFunc, divGlobalSetting);
  833. hesitationCheckbox = createSwitch('hesitationCheckbox', '随机犹豫期:', '(尝试应对人机校验,不一定有效,随机的范围:(0, 填的数字])不立刻参与天选时刻,这期间可以手动处理,免得自动参加后后悔(时间单位:秒)', hesitationFunc, divGlobalSetting, '23px', '90%', true, hesitateInput);
  834. closeLotteryCheckbox = createSwitch('closeLotteryCheckbox', '关闭天选时刻', '关闭天选时刻弹窗', closeLotteryFunc, divGlobalSetting);
  835. hideGiftControlCheckbox = createSwitch('hideGiftControlCheckbox', '隐藏礼物栏', '隐藏播放器底部的礼物栏', hideGiftControlFunc, divGlobalSetting);
  836. noSleepCheckbox = createSwitch('noSleepCheckbox', '防止直播间休眠', '防止直播间页面一段时间没操作之后进入休眠', noSleepFunc, divGlobalSetting, null, null, false);
  837. hideLoginGuideCheckbox = createSwitch('hideLoginGuideCheckbox', '隐藏播放器底部登录提示', '隐藏未登录时播放器底部显示的登录提示', hideLoginGuideFunc, divGlobalSetting);
  838. hideHarunaCheckbox = createSwitch('hideHarunaCheckbox', '隐藏看板娘立绘', '隐藏直播间Haruna立绘', hideHarunaFunc, divGlobalSetting);
  839. hideShopCheckbox = createSwitch('hideShopCheckbox', '隐藏购物提示', '隐藏播放器左上角的商店购物提示', hideShopFunc, divGlobalSetting);
  840. hideRoomFeedCheckbox = createSwitch('hideRoomFeedCheckbox', '隐藏主播动态', '隐藏播放器底下主播的动态栏', hideRoomFeedFunc, divGlobalSetting);
  841. hideRoomInfoCheckbox = createSwitch('hideRoomInfoCheckbox', '隐藏简介、荣誉,或直播间推荐列表', '隐藏播放器底下直播间推荐列表,不登录账号显示为主播的荣耀和简介', hideRoomInfoFunc, divGlobalSetting);
  842. hideNoticeCheckbox = createSwitch('hideNoticeCheckbox', '隐藏主播公告', '隐藏弹幕列表底下主播的公告', hideNoticeFunc, divGlobalSetting);
  843. hideFooterCheckbox = createSwitch('hideFooterCheckbox', '隐藏直播间页脚', '隐藏直播间底部的网页页脚', hideFooterFunc, divGlobalSetting);
  844. hidePrivacyCheckbox = createSwitch('hidePrivacyCheckbox', '隐藏隐私提示对话框', '隐藏隐私提示登录的对话框,被打码的昵称不保证变回正常', hidePrivacyFunc, divGlobalSetting);
  845. hideRoomStatusCheckbox = createSwitch('hideRoomStatusCheckbox', '隐藏直播水印', '隐藏播放器左上角的直播水印', hideRoomStatusFunc, divGlobalSetting);
  846. hideBusinessCheckbox = createSwitch('hideBusinessCheckbox', '隐藏商业性互动', '隐藏全站广播、PK、连MIC、连视频等', hideBusinessFunc, divGlobalSetting);
  847. hideRankListCheckbox = createSwitch('hideRankListCheckbox', '隐藏滚动排行榜', '隐藏顶部的人气榜、航海榜、礼物星球等', hideRankListFunc, divGlobalSetting);
  848. showLiveAreaCheckbox = createSwitch('showLiveArea', '显示直播分区', '显示直播间所属的直播分区', showLiveAreaFunc, divGlobalSetting);
  849. /* ----------------------------------------- global setting ----------------------------------------- */
  850.  
  851. /* ----------------------------------------- operation msg ----------------------------------------- */
  852. spanApplyMsg = document.createElement('span');
  853. spanApplyMsg.classList.add('danmu-random-setting-success-text');
  854.  
  855. let divApplyMsg = document.createElement('div');
  856. divApplyMsg.classList.add('danmu-random-setting-success-tips');
  857. divApplyMsg.appendChild(spanApplyMsg);
  858. /* ----------------------------------------- operation msg ----------------------------------------- */
  859. /* ----------------------------------------- clean cache ----------------------------------------- */
  860. let cleanCacheBtn = document.createElement('button');
  861. cleanCacheBtn.style.setProperty('display', 'none');
  862. cleanCacheBtn.textContent = '清除缓存';
  863. cleanCacheBtn.classList.add('clean-cache-btn');
  864. cleanCacheBtn.addEventListener('click', cleanCache);
  865. /* ----------------------------------------- clean cache ----------------------------------------- */
  866.  
  867. /* ----------------------------------------- save and close button ----------------------------------------- */
  868. let btnSave = document.createElement('i');
  869. btnSave.setAttribute('title', '保存');
  870. btnSave.style.padding = '5px';
  871. btnSave.classList.add('el-button');
  872. btnSave.classList.add('el-icon-check');
  873. btnSave.classList.add('is-circle');
  874. btnSave.addEventListener('click', save);
  875.  
  876. let btnClose = document.createElement('i');
  877. btnClose.setAttribute('title', '关闭');
  878. btnClose.style.padding = '5px';
  879. btnClose.classList.add('el-button');
  880. btnClose.classList.add('el-icon-close');
  881. btnClose.classList.add('is-circle');
  882. btnClose.addEventListener('click', closeSetting);
  883.  
  884. let divBtnSetting = document.createElement('div');
  885. divBtnSetting.classList.add('danmu-random-set-button-container');
  886. divBtnSetting.appendChild(cleanCacheBtn);
  887. divBtnSetting.appendChild(btnSave);
  888. divBtnSetting.appendChild(btnClose);
  889. /* ----------------------------------------- save and close button ----------------------------------------- */
  890.  
  891. /* ----------------------------------------- container ----------------------------------------- */
  892. let divBottomContainer = document.createElement('div');
  893. divBottomContainer.classList.add('danmu-random-setting-bottom');
  894. divBottomContainer.appendChild(divApplyMsg);
  895. divBottomContainer.appendChild(divBtnSetting);
  896.  
  897. let divOtherContainer = document.createElement('div');
  898. divOtherContainer.id = 'otherContainer';
  899.  
  900. let divContainer = document.createElement('div');
  901. divContainer.style.height = 'calc(98% - 30px - 25px)';
  902. divContainer.appendChild(divRoomSetting);
  903. divContainer.appendChild(divGlobalSetting);
  904. divContainer.appendChild(divOtherContainer);
  905. divContainer.appendChild(divBottomContainer);
  906. /* ----------------------------------------- container ----------------------------------------- */
  907.  
  908. divSetting = document.createElement('div');
  909. divSetting.id = 'danmu-setting-panel';
  910. divSetting.classList.add('danmu-random-setting-panel');
  911. divSetting.appendChild(divSettingTitle);
  912. divSetting.appendChild(divUpdateInfo);
  913. divSetting.appendChild(divTip);
  914. divSetting.appendChild(divContainer);
  915.  
  916. let asideAreaVm = document.getElementById('aside-area-vm');
  917. asideAreaVm.appendChild(divSetting);
  918.  
  919. },
  920. btnStart = btn => {
  921. const title = '开始定时发送';
  922. btnStartText = document.createElement('span');
  923. btnStartText.textContent = '开始';
  924. btnStartText.title = title;
  925. btnStartText.classList.add('txt');
  926.  
  927. dmButtonSend = document.createElement('button');
  928. dmButtonSend.title = title;
  929. //dmButtonSend.onclick = function() { alert('Hello world');}
  930. dmButtonSend.addEventListener('click', offOrOn);
  931. dmButtonSend.appendChild(btnStartText);
  932. if (btn) {
  933. copyAttributes(btn, dmButtonSend);
  934. const span = btn.querySelector('span');
  935. if (span) {
  936. copyAttributes(span, btnStartText);
  937. }
  938. }
  939. else {
  940. dmButtonSend.classList.add('danmu-btn');
  941. dmButtonSend.style.setProperty('--color', 'rgba(217,157,27,0.8)');
  942. }
  943. },
  944. btnSetting = () => {
  945. const title = '定时设置', divEmo = document.getElementsByClassName('emoticons-panel border-box')[0];
  946. let iElement = document.createElement('i');
  947. iElement.classList.add('el-icon-setting');
  948.  
  949. let btn = document.createElement('button');
  950. btn.title = title;
  951. btn.classList.add('el-button');
  952. btn.classList.add('el-button--mini');
  953. btn.classList.add('is-circle');
  954. btn.addEventListener('click', openSetting);
  955. btn.appendChild(iElement);
  956. if (divEmo) {
  957. let div = document.createElement('div');
  958. copyAttributes(divEmo, div);
  959. div.title = title;
  960. div.style.backgroundImage = 'none';
  961. div.style.position = 'absolute';
  962. div.style.right = '60px';
  963. div.style.bottom = '2px';
  964. div.appendChild(btn);
  965.  
  966. divEmo.style.position = 'absolute';
  967. divEmo.style.right = '60px';
  968. divEmo.style.top = '4px';
  969. return div;
  970. }
  971. else {
  972. btn.style.margin = '0 5px';
  973. return btn;
  974. }
  975. },
  976. copyAttributes = (oldNode, newNode) => {
  977. Array.prototype.forEach.call(oldNode.attributes, (item, index) => newNode.setAttribute(item.name, item.value));
  978. },
  979. setDisplay = (dom, property, important) => {
  980. if (important) {
  981. dom.style.setProperty('display', property, 'important');
  982. }
  983. else {
  984. dom.style.setProperty('display', property);
  985. }
  986. },
  987. blockDisplay = (dom, shwon) => {
  988. if (shwon) {
  989. setDisplay(dom, 'block', true);
  990. }
  991. else {
  992. dom.style.removeProperty('display');
  993. }
  994. },
  995. noneDisplay = (dom, hidden) => {
  996. if (hidden) {
  997. setDisplay(dom, 'none', true);
  998. }
  999. else {
  1000. dom.style.removeProperty('display');
  1001. }
  1002. },
  1003. removeAttributeFromChildren = (dom, attr) => {
  1004. dom.removeAttribute(attr);
  1005. if (0 < dom.children.length) {
  1006. for (let i = 0; i < dom.children.length; i++) {
  1007. removeAttributeFromChildren(dom.children[i], attr);
  1008. }
  1009. }
  1010. },
  1011. setAttributeToChildren = (dom, attr, val) => {
  1012. dom.setAttribute(attr, val);
  1013. if (0 < dom.children.length) {
  1014. for (let i = 0; i < dom.children.length; i++) {
  1015. setAttributeToChildren(dom.children[i], attr, val);
  1016. }
  1017. }
  1018. },
  1019. hideLoginGuideFunc = () => {
  1020. let dom = document.getElementById('switch-login-guide-vm');
  1021. if (dom) {
  1022. noneDisplay(dom, hideLoginGuideCheckbox.checked);
  1023. }
  1024. },
  1025. hideHarunaFunc = () => {
  1026. let dom = document.getElementById('my-dear-haruna-vm');
  1027. if (dom) {
  1028. noneDisplay(dom, hideHarunaCheckbox.checked);
  1029. }
  1030. },
  1031. hideShopFunc = () => {
  1032. let dom = document.getElementById('shop-popover-vm');
  1033. if (dom) {
  1034. noneDisplay(dom, hideShopCheckbox.checked);
  1035. }
  1036. },
  1037. hideGiftControlFunc = () => {
  1038. let dom = document.getElementsByClassName('gift-control-section')[0];
  1039. if (dom) {
  1040. noneDisplay(dom, hideGiftControlCheckbox.checked);
  1041. }
  1042.  
  1043. dom = document.getElementById('web-player__bottom-bar__container');
  1044. if (dom) {
  1045. noneDisplay(dom, hideGiftControlCheckbox.checked);
  1046. }
  1047.  
  1048. dom = document.getElementsByTagName('video');
  1049. for (let i = 0; i < dom.length; i++) {
  1050. if (!dom[i]) {
  1051. return;
  1052. }
  1053. if (hideGiftControlCheckbox.checked) {
  1054. dom[i].style.setProperty('height', '100%');
  1055. }
  1056. else if (document.body.classList.contains('player-full-win') || document.body.classList.contains('fullscreen-fix')) {
  1057. dom[i].style.setProperty('height', 'calc(100% - 114px)');
  1058. }
  1059. }
  1060. },
  1061. hideRoomFeedFunc = () => {
  1062. let dom = document.getElementsByClassName('room-feed')[0];
  1063. if (dom) {
  1064. noneDisplay(dom, hideRoomFeedCheckbox.checked);
  1065. }
  1066. dom = document.getElementsByClassName('flip-view p-relative')[0];
  1067. if (dom) {
  1068. noneDisplay(dom, hideRoomFeedCheckbox.checked);
  1069. }
  1070. },
  1071. hideRoomInfoFunc = () => {
  1072. let dom = document.getElementsByClassName('room-info-ctnr')[0];
  1073. if (dom) {
  1074. noneDisplay(dom, hideRoomInfoCheckbox.checked);
  1075. }
  1076. },
  1077. hideNoticeFunc = () => {
  1078. let dom = document.getElementsByClassName('right-container')[0];
  1079. if (dom) {
  1080. dom.style.setProperty('min-height', 'auto');
  1081. noneDisplay(dom, hideNoticeCheckbox.checked);
  1082. }
  1083. },
  1084. hideFooterFunc = () => {
  1085. let dom = document.getElementById('link-footer-vm');
  1086. if (dom) {
  1087. noneDisplay(dom, hideFooterCheckbox.checked);
  1088. }
  1089. },
  1090. hidePrivacyFunc = () => {
  1091. if (hidePrivacyCheckbox.checked) {
  1092. let dom = document.createElement('style');
  1093. dom.id = 'hidePrivacyDialog';
  1094. dom.setAttribute('type', 'text/css');
  1095. dom.innerHTML = '.privacy-dialog{display:none !important;}';
  1096. document.head.appendChild(dom);
  1097. }
  1098. else {
  1099. let dom = document.getElementById('hidePrivacyDialog');
  1100. if (dom) {
  1101. dom.remove();
  1102. }
  1103. }
  1104. },
  1105. hideRoomStatusFunc = () => {
  1106. let dom = document.getElementsByClassName('web-player-icon-roomStatus')[0];
  1107. if (dom) {
  1108. noneDisplay(dom, hideRoomStatusCheckbox.checked);
  1109. }
  1110. },
  1111. hideBusinessFunc = () => {
  1112. let dom = document.getElementById('businessContainerElement');
  1113. if (dom) {
  1114. noneDisplay(dom, hideBusinessCheckbox.checked);
  1115. }
  1116. },
  1117. // 适配Bilibili直播自动追帧样式
  1118. adaptBililiveSeeker = () => {
  1119. let dom = document.getElementById('playback-rate-title');
  1120. if (dom) {
  1121. noneDisplay(dom, true);
  1122. dom.parentElement.style.removeProperty('padding-bottom');
  1123. }
  1124.  
  1125. dom = document.getElementById('playback-rate-username');
  1126. if (dom) {
  1127. dom.style.removeProperty('display');
  1128. }
  1129. },
  1130. hideRankListFunc = () => {
  1131. let lower = document.getElementsByClassName('lower-row')[0];
  1132. if (!lower) {
  1133. return;
  1134. }
  1135.  
  1136. let dom = lower.getElementsByClassName('right-ctnr')[0]
  1137. if (dom) {
  1138. noneDisplay(dom, hideRankListCheckbox.checked);
  1139. let t = workerTimer.setTimeout(() => {
  1140. workerTimer.clearTimeout(t);
  1141. t = null;
  1142. adaptBililiveSeeker();
  1143. }, 100);
  1144. }
  1145. },
  1146. showLiveAreaFunc = () => {
  1147. let dom = document.getElementsByClassName('live-area')[0];
  1148. if (dom) {
  1149. blockDisplay(dom, showLiveAreaCheckbox.checked);
  1150. adaptBililiveSeeker();
  1151. }
  1152. },
  1153. lotteryFunc = () => {
  1154. window.localStorage.setItem(lotteryChecked, lotteryCheckbox.checked);
  1155. let dom = document.getElementById('hesitationCheckboxDiv');
  1156. if (dom) {
  1157. noneDisplay(dom, !lotteryCheckbox.checked)
  1158. }
  1159. },
  1160. closeLotteryFunc = () => {
  1161. window.localStorage.setItem(closeLotteryChecked, closeLotteryCheckbox.checked);
  1162. },
  1163. hesitationFunc = () => {
  1164. window.localStorage.setItem(hesitationChecked, hesitationCheckbox.checked);
  1165. window.localStorage.setItem(hesitationExpiry, hesitateInput.value);
  1166. },
  1167. setPublicFunc = () => {},
  1168. usePublicFunc = () => {
  1169. let obj = null;
  1170. if (usePublicCheckbox.checked) {
  1171. obj = getGmValue(baseInfo.default, null);
  1172. }
  1173. else {
  1174. obj = source;
  1175. }
  1176. if (obj) {
  1177. setSource(obj);
  1178. }
  1179. },
  1180. clickLikeBtn = () => {
  1181. let dom = document.getElementsByClassName('like-btn')[0];
  1182. if (dom) {
  1183. dom.click();
  1184. // console.log(`===> 【${new Date().toLocaleString()}】点击一次点赞按钮`);
  1185. }
  1186. },
  1187. autoLikeFunc = () => {
  1188. if (autoLikeCheckbox.checked) {
  1189. if (autoLikeTimer) {
  1190. return;
  1191. }
  1192.  
  1193. clickLikeBtn();
  1194. // console.log(`===> 开启自动点赞功能【${new Date().toLocaleString()}】`);
  1195. autoLikeTimer = workerTimer.setInterval(() => {
  1196. let rdTimeout = Math.floor(Math.random() * 15000);
  1197. if (1000 > rdTimeout) {
  1198. // console.log(`===> 【${new Date().toLocaleString()}】立刻点击点赞按钮`);
  1199. clickLikeBtn();
  1200. }
  1201. else {
  1202. // console.log(`===> 【${new Date().toLocaleString()}】设置【${rdTimeout}】毫秒后点击点赞按钮`);
  1203. clickLikeBtnTimer = workerTimer.setTimeout(() => {
  1204. workerTimer.clearTimeout(clickLikeBtnTimer);
  1205. clickLikeBtnTimer = null;
  1206. clickLikeBtn();
  1207. }, rdTimeout);
  1208. }
  1209. }, 15e3);
  1210. }
  1211. else {
  1212. if (autoLikeTimer) {
  1213. workerTimer.clearInterval(autoLikeTimer);
  1214. autoLikeTimer = null;
  1215. // console.log(`===> 关闭自动点赞功能`);
  1216. }
  1217. if (clickLikeBtnTimer) {
  1218. workerTimer.clearTimeout(clickLikeBtnTimer);
  1219. clickLikeBtnTimer = null;
  1220. // console.log(`===> 关闭超时点击点赞按钮功能`);
  1221. }
  1222. }
  1223. },
  1224. clickPlay = () => {
  1225. // let dom = document.getElementsByClassName('_tip-btn_6f52f')[0];
  1226. // if (dom) {
  1227. // dom.click();
  1228. // }
  1229.  
  1230. // dom = document.getElementsByClassName('_tip-text_6f52f')[0];
  1231. // if (dom) {
  1232. // dom.click();
  1233. // }
  1234. },
  1235. autoPlay = () => {
  1236. let t = workerTimer.setTimeout(() => {
  1237. workerTimer.clearTimeout(t);
  1238. clickPlay();
  1239. t = workerTimer.setTimeout(() => {
  1240. workerTimer.clearTimeout(t);
  1241. t = null;
  1242. clickPlay();
  1243. }, 2e3);
  1244. }, 3e3);
  1245. },
  1246. noSleepFunc = () => {
  1247. if (noSleepCheckbox.checked) {
  1248. if (!noSleepTimer) {
  1249. console.log('===> 开启防休眠功能');
  1250. noSleepTimer = workerTimer.setInterval(() => {
  1251. noSleepTimeouter = workerTimer.setTimeout(() => {
  1252. workerTimer.clearTimeout(noSleepTimeouter);
  1253. document.body.dispatchEvent(new MouseEvent("mousemove", { bubbles: true }));
  1254. }, Math.random() * 3e3);
  1255. }, 17e3);
  1256. }
  1257. } else {
  1258. console.log('===> 关闭防休眠功能');
  1259. if (noSleepTimer) {
  1260. workerTimer.clearInterval(noSleepTimer);
  1261. noSleepTimer = null;
  1262. }
  1263. if (noSleepTimeouter) {
  1264. workerTimer.clearTimeout(noSleepTimeouter);
  1265. noSleepTimeouter = null;
  1266. }
  1267. }
  1268. },
  1269. biliMiniClose = () => {
  1270. if (!miniCloseTimer) {
  1271. let miniCloseCount = 3;
  1272. miniCloseTimer = workerTimer.setInterval(() => {
  1273. let mini_close = document.getElementsByClassName('bili-mini-close')[0];
  1274. if (!mini_close) {
  1275. if (0 >= --miniCloseCount) {
  1276. workerTimer.clearInterval(miniCloseTimer);
  1277. miniCloseTimer = null;
  1278. }
  1279.  
  1280. return;
  1281. }
  1282.  
  1283. mini_close.click();
  1284. workerTimer.clearInterval(miniCloseTimer);
  1285. miniCloseTimer = null;
  1286. }, 10e3);
  1287. }
  1288. },
  1289. closeLottery = delay => {
  1290. let btnClose = document.getElementsByClassName('close-btn bg-contain')[0];
  1291. if (btnClose) {
  1292. if (isNull(delay)) {
  1293. btnClose.click();
  1294. } else {
  1295. closeLotteryTimer = workerTimer.setTimeout(() => {
  1296. workerTimer.clearTimeout(closeLotteryTimer);
  1297. btnClose.click();
  1298. }, delay * 1000);
  1299. }
  1300. }
  1301. },
  1302. lottery = btn => {
  1303. if (!btn) {
  1304. console.warn('===> 没有抽奖按钮DOM');
  1305. return;
  1306. }
  1307. if ('false' === window.localStorage.getItem(lotteryChecked)) {
  1308. if ('true' === window.localStorage.getItem(closeLotteryChecked)) {
  1309. console.log('===> 不参与天选时刻抽奖,关闭弹窗');
  1310. closeLottery();
  1311. }
  1312.  
  1313. return;
  1314. }
  1315. if ('true' === window.localStorage.getItem(hesitationChecked)) {
  1316. let expiry = window.localStorage.getItem(hesitationExpiry);
  1317. expiry = isNull(expiry) || 0 >= expiry ? 10e4 : expiry * 1000;
  1318. expiry = Math.ceil(Math.random() * expiry);
  1319. // console.log(`===> 【${new Date().toLocaleString()}】随机时间【${expiry}】`);
  1320. let lotteryTimer = workerTimer.setTimeout(() => {
  1321. workerTimer.clearTimeout(lotteryTimer);
  1322. console.log(`===> ${new Date().toLocaleString()}】犹豫期过后自动参加抽奖`);
  1323. btn.click();
  1324. if ('true' === window.localStorage.getItem(closeLotteryChecked)) {
  1325. console.log('===> 参加成功,延迟关闭弹窗');
  1326. closeLottery(3);
  1327. }
  1328. }, expiry);
  1329. } else {
  1330. console.log('===> 立刻自动参加抽奖');
  1331. btn.click();
  1332. if ('true' === window.localStorage.getItem(closeLotteryChecked)) {
  1333. console.log('===> 参加成功,延迟关闭弹窗');
  1334. closeLottery(3);
  1335. }
  1336. }
  1337. },
  1338. listPlus = nodes => {
  1339. if (!nodes || 0 >= nodes.length) return;
  1340. Array.prototype.forEach.call(nodes, y => {
  1341. let aNode = y.firstChild;
  1342. if (!aNode) return;
  1343. let a_d2 = aNode.children[1];
  1344. if (!a_d2) return;
  1345. let a_d2_d2 = a_d2.children[1];
  1346. if (!a_d2_d2) return;
  1347. let a_d2_d2_d2 = a_d2_d2.children[1];
  1348. if (!a_d2_d2_d2) return;
  1349. let a_d2_d2_d2_d1 = a_d2_d2_d2.children[0];
  1350. if (!a_d2_d2_d2_d1) return;
  1351. aNode.title = a_d2_d2_d2_d1.textContent;
  1352. let a_d2_d1 = a_d2.children[0];
  1353. if (a_d2_d1) {
  1354. let a_d2_d1_last = a_d2_d1.lastChild;
  1355. if (a_d2_d1_last && a_d2_d1_last.style.display && 'none' == a_d2_d1_last.style.display) {
  1356. a_d2_d1_last.remove();
  1357. }
  1358. }
  1359. });
  1360. },
  1361. getConfig = () => {
  1362. let c = {};
  1363. if (baseInfo.config) {
  1364. c = getGmValue(baseInfo.config, {});
  1365. if (isNull(c.lottery)) {
  1366. c.lottery = false;
  1367. }
  1368. if (isNull(c.closeLottery)) {
  1369. c.closeLottery = false;
  1370. }
  1371. if (isNull(c.hesitation)) {
  1372. c.hesitation = false;
  1373. }
  1374. }
  1375.  
  1376. return c;
  1377. },
  1378. checkVersion = () => {
  1379. config = getConfig();
  1380. if (!config || !config.moduleVersion || !divUpdateInfo) {
  1381. return;
  1382. }
  1383. if (0 < compareVersion(config.moduleVersion, version)) {
  1384. divUpdateInfo.innerHTML = `<span class="update-info-text">有新版本,刷新网页进行更新</span>`;
  1385. let dom = document.getElementById('otherContainer');
  1386. if (dom) {
  1387. dom.style.fontSize = '18px';
  1388. dom.style.textAlign = 'center';
  1389. dom.appendChild(divUpdateInfo.cloneNode(true));
  1390. }
  1391. }
  1392. },
  1393. loadData = () => {
  1394. let obj = getGmValue(roomId, null),
  1395. key = roomId;
  1396. if (obj) {
  1397. if (obj.usePublic && baseInfo.default) {
  1398. let ps = getGmValue(baseInfo.default, null);
  1399. if (ps) {
  1400. obj.data1.values = ps.data1 ? ps.data1.values : obj.data1.values;
  1401. obj.data2.values = ps.data2 ? ps.data2.values : obj.data2.values;
  1402. obj.data3.values = ps.data3 ? ps.data3.values : obj.data3.values;
  1403. obj.data4.values = ps.data4 ? ps.data4.values : obj.data4.values;
  1404. obj.data5.values = ps.data5 ? ps.data5.values : obj.data5.values;
  1405. }
  1406. }
  1407. if (source.version === obj.version) {
  1408. source = {...source, ...obj};
  1409. }
  1410. else if (obj.version === 2) {
  1411. source.data1 = obj.data1;
  1412. source.data2 = obj.data2;
  1413. source.data3 = obj.data3;
  1414. source.data4 = obj.data4;
  1415. source.data5 = obj.data5;
  1416. setGmValue(key, source);
  1417. }
  1418. else {
  1419. source.data1.values = obj.data1 ? obj.data1.values : source.data1.values;
  1420. source.data2.values = obj.data2 ? obj.data2.values : source.data2.values;
  1421. source.data3.values = obj.data3 ? obj.data3.values : source.data3.values;
  1422. source.data4.values = obj.data4 ? obj.data4.values : source.data4.values;
  1423. source.data5.values = obj.data5 ? obj.data5.values : source.data5.values;
  1424. setGmValue(key, source);
  1425. }
  1426. }
  1427. config = getConfig();
  1428. signInput.value = isNull(config.signInText) ? '' : config.signInText;
  1429. hesitateInput.value = isNull(config.hesitationExpiry) ? '100' : config.hesitationExpiry;
  1430. setSource(source);
  1431. setOperationSetting();
  1432. initData();
  1433. },
  1434. closeWsHeartBeatTimer = () => workerTimer.clearInterval(wsHeartBeatTimer),
  1435. closeWsReconnectTimer = () => {
  1436. workerTimer.clearInterval(wsReconnectWsTimer);
  1437. wsReconnectWsTimer = null;
  1438. },
  1439. handleWsPacket = async event => {
  1440. const packet = await decode(event.data);
  1441. switch (packet.op) {
  1442. // 认证包回复
  1443. case 8:
  1444. closeWsReconnectTimer();
  1445. console.log(`===> 成功加入房间`);
  1446. break;
  1447. // 心跳包回复(人气值)
  1448. case 3:
  1449. closeWsReconnectTimer();
  1450. const count = packet.body.count
  1451. console.log(`===> 人气:${count}`);
  1452. break;
  1453. // 普通包(命令)
  1454. case 5:
  1455. packet.body.forEach((body) => {
  1456. switch (body.cmd) {
  1457. case 'DANMU_MSG':
  1458. console.log(`===> ${body.info[2][1]}: ${body.info[1]}`);
  1459. break;
  1460. // 主播准备中(0 未轮播;1 正在轮播)
  1461. case 'PREPARING':
  1462. console.log(`===> 直播间【${body.roomid}】状态【${body.round}】`);
  1463. break;
  1464. // 直播开始
  1465. case 'LIVE':
  1466. console.log(`===> 直播间【${body.roomid}】在【${new Date(body.live_time).toLocaleString()}】开始直播`);
  1467. break;
  1468. // 直播间信息更改
  1469. case 'ROOM_CHANGE':
  1470. console.log(`===> 直播信息变更,标题【${body.data.title}】,分区【${body.data.area_name}】,上级分区【${body.data.parent_area_name}】`);
  1471. break;
  1472. // 下播的直播间
  1473. case 'STOP_LIVE_ROOM_LIST':
  1474. console.log(`===> 当前直播间是否下播【${body.data.room_id_list.includes(roomId)}】`);
  1475. break;
  1476. // 切断
  1477. case 'CUT_OFF':
  1478. console.log(`===> 直播间【${body.roomid}】已被切断【${body.msg}】`);
  1479. break;
  1480. // 天选时刻开始
  1481. case 'ANCHOR_LOT_START':
  1482. console.log(`===> 直播间【${body.data.room_id}】在【${new Date(body.data.current_time).toLocaleDateString()}】开始天选时刻抽奖,id${body.data.id};max_time${body.data.max_time};danmu${body.data.danmu};time${body.data.time}`);
  1483. break;
  1484. // 天选时刻结束
  1485. case 'ANCHOR_LOT_END':
  1486. console.log(`===> 天选时刻结束【${body}】`);
  1487. break;
  1488. // 重进直播间
  1489. case 'REENTER_LIVE_ROOM':
  1490. console.log(`===> 重进直播间【${body}】`);
  1491. break;
  1492. default:
  1493. if (!wsCmds.includes(body.cmd)) {
  1494. wsCmds.push(body.cmd);
  1495. }
  1496. // console.log(`===> ${body}`);
  1497. }
  1498. })
  1499. break;
  1500. default:
  1501. console.log(packet);
  1502. }
  1503. },
  1504. getWsInfo = async () => {
  1505. console.log(`===> 开始获取WS info`);
  1506. const resp = await fetch(baseInfo.wsInfo.replace('#rid#', roomId), {method: 'GET', cache: 'no-store', credentials: 'include'});
  1507. if (!resp.ok) {
  1508. console.warn(`===> WS host信息请求不成功`);
  1509. return;
  1510. }
  1511.  
  1512. const obj = await resp.json();
  1513. if (0 !== obj?.code) {
  1514. console.warn(`===> 获取WS host失败【${obj?.message}】`);
  1515. return;
  1516. }
  1517. if (!obj?.data?.host_list || 1 > obj?.data?.host_list.length) {
  1518. console.warn(`===> WS host信息为空`);
  1519. return;
  1520. }
  1521.  
  1522. wsConnectCount = 30;
  1523. wsToken = obj.data.token || '';
  1524. wsHostList = obj.data.host_list;
  1525. console.log(`===> 获取WS info完成`);
  1526. },
  1527. connectWS = () => {
  1528. if (1 > wsHostList.length) {
  1529. console.warn(`===> WS host list为空`);
  1530. return;
  1531. }
  1532. if (wsHostIndex >= wsHostList.length) {
  1533. wsHostIndex = 0;
  1534. }
  1535. console.log(`===> 开始进行WS连接`);
  1536. ws = new WebSocket(`wss://${wsHostList[wsHostIndex].host}:${wsHostList[wsHostIndex].wss_port}/sub`);
  1537. ws.onopen = event => {
  1538. console.log(`===> WS连接成功,准备发认证包`);
  1539. const o = {
  1540. "uid": 0 < uid ? uid : ruid,
  1541. "roomid": roomId,
  1542. "protover": 3,
  1543. "platform": "web",
  1544. "type": 2
  1545. },
  1546. buvid = getCookie('buvid3');
  1547. if (!isNull(buvid)) {
  1548. o.buvid = buvid;
  1549. }
  1550. if (!isNull(wsToken)) {
  1551. o.key = wsToken;
  1552. }
  1553. const str = JSON.stringify(o);
  1554. console.log(`===> 序列化认证包【${str}】`);
  1555. ws.send(encode(str, 7));
  1556. console.log(`===> 认证包已经发送`);
  1557. };
  1558. ws.onmessage = event => handleWsPacket(event);
  1559. ws.onclose = event => {
  1560. closeWsHeartBeatTimer();
  1561. ws = null;
  1562. console.warn(`===> WS连接已关闭`);
  1563. };
  1564. ws.onerror = error => console.error('===> WebSocket error: ', error);
  1565. },
  1566. heartBeatWS = () => {
  1567. wsHeartBeatTimer = workerTimer.setInterval(() => {
  1568. if (!ws) {
  1569. closeWsHeartBeatTimer();
  1570. return;
  1571. }
  1572.  
  1573. console.log(`===> 准备发送心跳包`);
  1574. ws.send(encode('', 2));
  1575. console.log(`===> 心跳包已经发送`);
  1576. reconnectWS();
  1577. }, 30e3);
  1578. },
  1579. initWS = async () => {
  1580. if (ws || isNull(baseInfo?.wsInfo)) {
  1581. console.warn(`===> WS已初始化,或wsInfo为空`);
  1582. return;
  1583. }
  1584. if (1 > wsHostList.length || 0 >= wsConnectCount) {
  1585. await getWsInfo();
  1586. }
  1587. connectWS();
  1588. heartBeatWS();
  1589. },
  1590. reconnectWS = () => {
  1591. if (wsReconnectWsTimer) {
  1592. return;
  1593. }
  1594.  
  1595. console.log(`===> 启动重连定时器`);
  1596. wsReconnectWsTimer = workerTimer.setInterval(() => {
  1597. if (ws) {
  1598. console.warn(`===> 心跳包响应超时,进行WS关闭操作`);
  1599. ws.close();
  1600. }
  1601. console.log(`===> 尝试WS重连`);
  1602. ++wsHostIndex;
  1603. --wsConnectCount;
  1604. initWS();
  1605. }, 65e3);
  1606. },
  1607. initSettingPanel = (div, isLogin) => {
  1608. let settingPanel = document.getElementById('danmu-setting-panel');
  1609. if (div && !settingPanel) {
  1610. // console.log('===> 进行面板初始化');
  1611. let btnSend = document.getElementsByClassName('bl-button bl-button--primary')[0];
  1612. if (btnSend) {
  1613. buildPanel();
  1614. btnStart(btnSend);
  1615. if (isLogin) {
  1616. dmButtonSend.style.marginTop = '4px';
  1617. dmButtonSend.style.marginLeft = '30px';
  1618. btnSend.style.marginLeft = '30px';
  1619. div.appendChild(dmButtonSend);
  1620. div.parentNode.insertBefore(btnSetting(), div);
  1621. }
  1622. else {
  1623. let funcDiv = document.createElement('div');
  1624. funcDiv.style.position = 'absolute';
  1625. funcDiv.appendChild(dmButtonSend);
  1626. funcDiv.appendChild(btnSetting());
  1627. div.appendChild(funcDiv);
  1628. }
  1629. initWS();
  1630. loadData();
  1631. // console.log('===> 面板初始化完成');
  1632. } else {
  1633. console.warn('===> 发送按钮丢失');
  1634. return false;
  1635. }
  1636. }
  1637.  
  1638. return true;
  1639. },
  1640. main = (div, isLogin) => {
  1641. waiters[waiters.length] = workerTimer.setInterval(() => {
  1642. if (initSettingPanel(div, isLogin)) {
  1643. clearWaiters();
  1644. } else {
  1645. --waitCount;
  1646. if (0 >= waitCount) {
  1647. clearWaiters();
  1648. console.log('===> 创建面板失败,停止初始化');
  1649. }
  1650. }
  1651. }, 1.5e3);
  1652. },
  1653. PluginBtn = (clazz, isLogin) => {
  1654. let div = document.getElementsByClassName(clazz)[0];
  1655. if (div) {
  1656. main(div, isLogin);
  1657. } else {
  1658. let count = 0;
  1659. divSendBtnTimer = workerTimer.setInterval(() => {
  1660. div = document.getElementsByClassName(clazz)[0];
  1661. if (div) {
  1662. workerTimer.clearInterval(divSendBtnTimer);
  1663. main(div, isLogin);
  1664. } else if (count++ >= 10) {
  1665. workerTimer.clearInterval(divSendBtnTimer);
  1666. // console.log(`===> 页面【${window.location.href}】没有定位位置`);
  1667. }
  1668. }, 1e3);
  1669. }
  1670. },
  1671. PluginLogout = () => PluginBtn('bottom-actions p-relative', false),
  1672. PluginLogin = () => PluginBtn('right-actions border-box', true),
  1673. PluginLottery = () => {
  1674. let btn = document.getElementsByClassName('particitation-btn')[0];
  1675. if (btn) {
  1676. lottery(btn);
  1677. } else {
  1678. btnLotteryTimer = workerTimer.setTimeout(() => {
  1679. workerTimer.clearTimeout(btnLotteryTimer);
  1680. btn = document.getElementsByClassName('particitation-btn')[0];
  1681. if (btn) {
  1682. lottery(btn);
  1683. }
  1684. }, 2e3);
  1685. }
  1686. },
  1687. PluginListPlus = () => {
  1688. let listTimer = workerTimer.setTimeout(() => {
  1689. workerTimer.clearTimeout(listTimer);
  1690. let obsConfig = {childList: true},
  1691. tags = ['all__card-list-ctnr', 'all__special-area-recommend-list-ctnr'];
  1692. tags.forEach(x => {
  1693. let dom = document.getElementsByClassName(x)[0];
  1694. if (!dom) return;
  1695. Array.prototype.forEach.call(dom.children, y => {
  1696. if (/^index_/i.test(y.className)) {
  1697. listPlus(y.children);
  1698. let obs = new MutationObserver(mrs => {
  1699. if (!mrs || 0 >= mrs.length) return;
  1700. Array.prototype.forEach.call(mrs, z => listPlus(z.addedNodes));
  1701. });
  1702. obs.observe(y, obsConfig);
  1703. }
  1704. });
  1705. });
  1706. }, 0.5e3);
  1707. },
  1708. debug = () => {debugger;},
  1709. printWsInfos = () => {
  1710. console.log(`==> wsConnectCount: ${wsConnectCount}`);
  1711. console.log(wsHostList);
  1712. console.info(wsCmds);
  1713. },
  1714. runWS = () => initWS(),
  1715. stopWS = () => {
  1716. closeWsReconnectTimer();
  1717. if (ws) {
  1718. ws.close();
  1719. }
  1720. },
  1721. runStart = () => {
  1722. PluginLottery();
  1723. PluginListPlus();
  1724. PluginLogout();
  1725. PluginLogin();
  1726. };
  1727.  
  1728. initCss();
  1729. initScript();
  1730.  
  1731. window.debug = debug;
  1732. window.runWS = runWS;
  1733. window.stopWS = stopWS;
  1734. window.printWsInfos = printWsInfos;
  1735.  
  1736. window.runStart = runStart;
  1737. window.arrayInfo = arrayInfo;
  1738. window.setGmNotice = setGmNotice;
  1739. window.setGmGetValue = setGmGetValue;
  1740. window.setGmSetValue = setGmSetValue;
  1741. window.setGmDelValue = setGmDelValue;
  1742. window.setBaseInfo = setBaseInfo;
  1743. window.checkVersion = checkVersion;
  1744. })();