b-live-random-send-test

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

目前為 2024-10-08 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/447936/1461241/b-live-random-send-test.js

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