Greasy Fork 还支持 简体中文。

NGA Filter

NGA 屏蔽插件,支持用户、标记、关键字、属地、小号、流量号、低声望、匿名过滤。troll must die。

目前為 2024-01-22 提交的版本,檢視 最新版本

  1. 3// ==UserScript==
  2. // @name NGA Filter
  3. // @namespace https://greasyfork.org/users/263018
  4. // @version 2.2.3
  5. // @author snyssss
  6. // @description NGA 屏蔽插件,支持用户、标记、关键字、属地、小号、流量号、低声望、匿名过滤。troll must die。
  7. // @license MIT
  8.  
  9. // @match *://bbs.nga.cn/*
  10. // @match *://ngabbs.com/*
  11. // @match *://nga.178.com/*
  12.  
  13. // @grant GM_addStyle
  14. // @grant GM_setValue
  15. // @grant GM_getValue
  16. // @grant GM_registerMenuCommand
  17. // @grant unsafeWindow
  18.  
  19. // @run-at document-start
  20. // @noframes
  21. // ==/UserScript==
  22.  
  23. (() => {
  24. // 声明泥潭主模块、主题模块、回复模块
  25. let commonui, topicModule, replyModule;
  26.  
  27. // KEY
  28. const DATA_KEY = "NGAFilter";
  29. const USER_AGENT_KEY = "USER_AGENT_KEY";
  30. const PRE_FILTER_KEY = "PRE_FILTER_KEY";
  31. const CLEAR_TIME_KEY = "CLEAR_TIME_KEY";
  32.  
  33. // TIPS
  34. const TIPS = {
  35. filterMode:
  36. "过滤顺序:用户 &gt; 标记 &gt; 关键字 &gt; 属地<br/>过滤级别:显示 &gt; 隐藏 &gt; 遮罩 &gt; 标记 &gt; 继承",
  37. addTags: `一次性添加多个标记用"|"隔开,不会添加重名标记`,
  38. keyword: `支持正则表达式。比如同类型的可以写在一条规则内用"|"隔开,"ABC|DEF"即为屏蔽带有ABC或者DEF的内容。`,
  39. hunter: "猎巫模块需要占用额外的资源,请谨慎开启",
  40. };
  41.  
  42. // STYLE
  43. GM_addStyle(`
  44. .filter-table-wrapper {
  45. max-height: 80vh;
  46. overflow-y: auto;
  47. }
  48. .filter-table {
  49. margin: 0;
  50. }
  51. .filter-table th,
  52. .filter-table td {
  53. position: relative;
  54. white-space: nowrap;
  55. }
  56. .filter-table th {
  57. position: sticky;
  58. top: 2px;
  59. z-index: 1;
  60. }
  61. .filter-table input:not([type]), .filter-table input[type="text"] {
  62. margin: 0;
  63. box-sizing: border-box;
  64. height: 100%;
  65. width: 100%;
  66. }
  67. .filter-input-wrapper {
  68. position: absolute;
  69. top: 6px;
  70. right: 6px;
  71. bottom: 6px;
  72. left: 6px;
  73. }
  74. .filter-text-ellipsis {
  75. display: flex;
  76. }
  77. .filter-text-ellipsis > * {
  78. flex: 1;
  79. width: 1px;
  80. overflow: hidden;
  81. text-overflow: ellipsis;
  82. }
  83. .filter-button-group {
  84. margin: -.1em -.2em;
  85. }
  86. .filter-tags {
  87. margin: 2px -0.2em 0;
  88. text-align: left;
  89. }
  90. .filter-mask {
  91. margin: 1px;
  92. color: #81C7D4;
  93. background: #81C7D4;
  94. }
  95. .filter-mask-block {
  96. display: block;
  97. border: 1px solid #66BAB7;
  98. text-align: center !important;
  99. }
  100. .filter-input-wrapper {
  101. position: absolute;
  102. top: 6px;
  103. right: 6px;
  104. bottom: 6px;
  105. left: 6px;
  106. }
  107. `);
  108.  
  109. /**
  110. * 工具类
  111. */
  112. class Tools {
  113. /**
  114. * 返回当前值的类型
  115. * @param {*} value 值
  116. * @returns {String} 值的类型
  117. */
  118. static getType = (value) => {
  119. return Object.prototype.toString.call(value).slice(8, -1).toLowerCase();
  120. };
  121.  
  122. /**
  123. * 返回当前值是否为指定的类型
  124. * @param {*} value 值
  125. * @param {Array<String>} types 类型名称集合
  126. * @returns {Boolean} 值是否为指定的类型
  127. */
  128. static isType = (value, ...types) => {
  129. return types.includes(this.getType(value));
  130. };
  131.  
  132. /**
  133. * 拦截属性
  134. * @param {Object} target 目标对象
  135. * @param {String} property 属性或函数名称
  136. * @param {Function} beforeGet 获取属性前事件
  137. * @param {Function} beforeSet 设置属性前事件
  138. * @param {Function} afterGet 获取属性后事件
  139. * @param {Function} afterSet 设置属性前事件
  140. */
  141. static interceptProperty = (
  142. target,
  143. property,
  144. { beforeGet, beforeSet, afterGet, afterSet }
  145. ) => {
  146. // 缓存数据
  147. let source = target[property];
  148.  
  149. // 如果已经有结果,则直接处理写入后操作
  150. if (Object.hasOwn(target, property)) {
  151. if (afterSet) {
  152. afterSet.apply(target, [source]);
  153. }
  154. }
  155.  
  156. // 拦截
  157. Object.defineProperty(target, property, {
  158. get: () => {
  159. // 如果是函数
  160. if (this.isType(source, "function")) {
  161. return (...args) => {
  162. try {
  163. // 执行前操作
  164. // 可以在这一步修改参数
  165. // 可以通过在这一步抛出来阻止执行
  166. if (beforeGet) {
  167. args = beforeGet.apply(target, args);
  168. }
  169.  
  170. // 执行函数
  171. const returnValue = source.apply(target, args);
  172.  
  173. // 返回的可能是一个 Promise
  174. const result =
  175. returnValue instanceof Promise
  176. ? returnValue
  177. : Promise.resolve(returnValue);
  178.  
  179. // 执行后操作
  180. if (afterGet) {
  181. result.then((value) => {
  182. afterGet.apply(target, [value, args, source]);
  183. });
  184. }
  185. } catch {}
  186. };
  187. }
  188.  
  189. try {
  190. // 返回前操作
  191. // 可以在这一步修改返回结果
  192. // 可以通过在这一步抛出来返回 undefined
  193. const result = beforeGet
  194. ? beforeGet.apply(target, [source])
  195. : source;
  196.  
  197. // 返回后操作
  198. // 实际上是在返回前完成的,并不能叫返回后操作,但是我们可以配合 beforeGet 来操作处理后的数据
  199. if (afterGet) {
  200. afterGet.apply(target, [result, source]);
  201. }
  202.  
  203. // 返回结果
  204. return result;
  205. } catch {
  206. return undefined;
  207. }
  208. },
  209. set: (value) => {
  210. try {
  211. // 写入前操作
  212. // 可以在这一步修改写入结果
  213. // 可以通过在这一步抛出来写入 undefined
  214. const result = beforeSet
  215. ? beforeSet.apply(target, [source, value])
  216. : value;
  217.  
  218. // 写入结果
  219. source = result;
  220.  
  221. // 写入后操作
  222. if (afterSet) {
  223. afterSet.apply(target, [result, value]);
  224. }
  225. } catch {
  226. source = undefined;
  227. }
  228. },
  229. });
  230. };
  231.  
  232. /**
  233. * 合并数据
  234. * @param {*} target 目标对象
  235. * @param {Array} sources 来源对象集合
  236. * @returns 合并后的对象
  237. */
  238. static merge = (target, ...sources) => {
  239. for (const source of sources) {
  240. const targetType = this.getType(target);
  241. const sourceType = this.getType(source);
  242.  
  243. // 如果来源对象的类型与目标对象不一致,替换为来源对象
  244. if (sourceType !== targetType) {
  245. target = source;
  246. continue;
  247. }
  248.  
  249. // 如果来源对象是数组,直接合并
  250. if (targetType === "array") {
  251. target = [...target, ...source];
  252. continue;
  253. }
  254.  
  255. // 如果来源对象是对象,合并对象
  256. if (sourceType === "object") {
  257. for (const key in source) {
  258. if (Object.hasOwn(target, key)) {
  259. target[key] = this.merge(target[key], source[key]);
  260. } else {
  261. target[key] = source[key];
  262. }
  263. }
  264. continue;
  265. }
  266.  
  267. // 其他情况,更新值
  268. target = source;
  269. }
  270.  
  271. return target;
  272. };
  273.  
  274. /**
  275. * 数组排序
  276. * @param {Array} collection 数据集合
  277. * @param {Array<String | Function>} iterators 迭代器,要排序的属性名或排序函数
  278. */
  279. static sortBy = (collection, ...iterators) =>
  280. collection.slice().sort((a, b) => {
  281. for (let i = 0; i < iterators.length; i += 1) {
  282. const iteratee = iterators[i];
  283.  
  284. const valueA = this.isType(iteratee, "function")
  285. ? iteratee(a)
  286. : a[iteratee];
  287. const valueB = this.isType(iteratee, "function")
  288. ? iteratee(b)
  289. : b[iteratee];
  290.  
  291. if (valueA < valueB) {
  292. return -1;
  293. }
  294.  
  295. if (valueA > valueB) {
  296. return 1;
  297. }
  298. }
  299.  
  300. return 0;
  301. });
  302.  
  303. /**
  304. * 读取论坛数据
  305. * @param {Response} response 请求响应
  306. * @param {Boolean} toJSON 是否转为 JSON 格式
  307. */
  308. static readForumData = async (response, toJSON = true) => {
  309. return new Promise(async (resolve) => {
  310. const blob = await response.blob();
  311.  
  312. const reader = new FileReader();
  313.  
  314. reader.onload = () => {
  315. const text = reader.result.replace(
  316. "window.script_muti_get_var_store=",
  317. ""
  318. );
  319.  
  320. if (toJSON) {
  321. try {
  322. resolve(JSON.parse(text));
  323. } catch {
  324. resolve({});
  325. }
  326. return;
  327. }
  328.  
  329. resolve(text);
  330. };
  331.  
  332. reader.readAsText(blob, "GBK");
  333. });
  334. };
  335.  
  336. /**
  337. * 获取成对括号的内容
  338. * @param {String} content 内容
  339. * @param {String} keyword 起始位置关键字
  340. * @param {String} start 左括号
  341. * @param {String} end 右括号
  342. * @returns {String} 包含括号的内容
  343. */
  344. static searchPair = (content, keyword, start = "{", end = "}") => {
  345. // 获取成对括号的位置
  346. const getLastIndex = (content, position, start = "{", end = "}") => {
  347. if (position >= 0) {
  348. let nextIndex = position + 1;
  349.  
  350. while (nextIndex < content.length) {
  351. if (content[nextIndex] === end) {
  352. return nextIndex;
  353. }
  354.  
  355. if (content[nextIndex] === start) {
  356. nextIndex = getLastIndex(content, nextIndex, start, end);
  357.  
  358. if (nextIndex < 0) {
  359. break;
  360. }
  361. }
  362.  
  363. nextIndex = nextIndex + 1;
  364. }
  365. }
  366.  
  367. return -1;
  368. };
  369.  
  370. // 起始位置
  371. const str = keyword + start;
  372.  
  373. // 起始下标
  374. const index = content.indexOf(str) + str.length;
  375.  
  376. // 结尾下标
  377. const lastIndex = getLastIndex(content, index, start, end);
  378.  
  379. if (lastIndex >= 0) {
  380. return start + content.substring(index, lastIndex) + end;
  381. }
  382.  
  383. return null;
  384. };
  385.  
  386. /**
  387. * 计算字符串的颜色
  388. *
  389. * 采用的是泥潭的颜色方案,参见 commonui.htmlName
  390. * @param {String} value 字符串
  391. * @returns {String} RGB代码
  392. */
  393. static generateColor(value) {
  394. const hash = (() => {
  395. let h = 5381;
  396.  
  397. for (var i = 0; i < value.length; i++) {
  398. h = ((h << 5) + h + value.charCodeAt(i)) & 0xffffffff;
  399. }
  400.  
  401. return h;
  402. })();
  403.  
  404. const hex = Math.abs(hash).toString(16) + "000000";
  405.  
  406. const hsv = [
  407. `0x${hex.substring(2, 4)}` / 255,
  408. `0x${hex.substring(2, 4)}` / 255 / 2 + 0.25,
  409. `0x${hex.substring(4, 6)}` / 255 / 2 + 0.25,
  410. ];
  411.  
  412. const rgb = commonui.hsvToRgb(hsv[0], hsv[1], hsv[2]);
  413.  
  414. return ["#", ...rgb].reduce((a, b) => {
  415. return a + ("0" + b.toString(16)).slice(-2);
  416. });
  417. }
  418. }
  419.  
  420. /**
  421. * IndexedDB
  422. *
  423. * 简单制造轮子,暂不打算引入 dexie.js,待其云方案正式推出后再考虑
  424. */
  425.  
  426. class DBStorage {
  427. /**
  428. * 数据库名称
  429. */
  430. name = "NGA_FILTER_CACHE";
  431.  
  432. /**
  433. * 模块列表
  434. */
  435. modules = {};
  436.  
  437. /**
  438. * 当前实例
  439. */
  440. instance = null;
  441.  
  442. /**
  443. * 初始化
  444. * @param {*} modules 模块列表
  445. */
  446. constructor(modules) {
  447. this.modules = modules;
  448. }
  449.  
  450. /**
  451. * 是否支持
  452. */
  453. isSupport() {
  454. return unsafeWindow.indexedDB !== undefined;
  455. }
  456.  
  457. /**
  458. * 打开数据库并创建表
  459. * @returns {Promise<IDBDatabase>} 实例
  460. */
  461. async open() {
  462. // 创建实例
  463. if (this.instance === null) {
  464. // 声明一个数组,用于等待全部表处理完毕
  465. const queue = [];
  466.  
  467. // 创建实例
  468. await new Promise((resolve, reject) => {
  469. // 版本
  470. const version = Object.values(this.modules)
  471. .map(({ version }) => version)
  472. .reduce((a, b) => Math.max(a, b), 0);
  473.  
  474. // 创建请求
  475. const request = unsafeWindow.indexedDB.open(this.name, version);
  476.  
  477. // 创建或者升级表
  478. request.onupgradeneeded = (event) => {
  479. this.instance = event.target.result;
  480.  
  481. const transaction = event.target.transaction;
  482. const oldVersion = event.oldVersion;
  483.  
  484. Object.entries(this.modules).forEach(([key, values]) => {
  485. if (values.version > oldVersion) {
  486. queue.push(this.createOrUpdateStore(key, values, transaction));
  487. }
  488. });
  489. };
  490.  
  491. // 成功后处理
  492. request.onsuccess = (event) => {
  493. this.instance = event.target.result;
  494. resolve();
  495. };
  496.  
  497. // 失败后处理
  498. request.onerror = () => {
  499. reject();
  500. };
  501. });
  502.  
  503. // 等待全部表处理完毕
  504. await Promise.all(queue);
  505. }
  506.  
  507. // 返回实例
  508. return this.instance;
  509. }
  510.  
  511. /**
  512. * 获取表
  513. * @param {String} name 表名
  514. * @param {IDBTransaction} transaction 事务,空则根据表名创建新事务
  515. * @param {String} mode 事务模式,默认为只读
  516. * @returns {Promise<IDBObjectStore>} 表
  517. */
  518. async getStore(name, transaction = null, mode = "readonly") {
  519. const db = await this.open();
  520.  
  521. if (transaction === null) {
  522. transaction = db.transaction(name, mode);
  523. }
  524.  
  525. return transaction.objectStore(name);
  526. }
  527.  
  528. /**
  529. * 创建或升级表
  530. * @param {String} name 表名
  531. * @param {IDBTransaction} transaction 事务,空则根据表名创建新事务
  532. * @returns {Promise}
  533. */
  534. async createOrUpdateStore(name, { keyPath, indexes }, transaction) {
  535. const db = transaction.db;
  536. const data = [];
  537.  
  538. // 检查是否存在表,如果存在,缓存数据并删除旧表
  539. if (db.objectStoreNames.contains(name)) {
  540. // 获取并缓存全部数据
  541. const result = await this.bulkGet(name, [], transaction);
  542.  
  543. if (result) {
  544. data.push(...result);
  545. }
  546.  
  547. // 删除旧表
  548. db.deleteObjectStore(name);
  549. }
  550.  
  551. // 创建表
  552. const store = db.createObjectStore(name, {
  553. keyPath,
  554. });
  555.  
  556. // 创建索引
  557. if (indexes) {
  558. indexes.forEach((index) => {
  559. store.createIndex(index, index);
  560. });
  561. }
  562.  
  563. // 迁移数据
  564. if (data.length > 0) {
  565. await this.bulkAdd(name, data, transaction);
  566. }
  567. }
  568.  
  569. /**
  570. * 插入指定表的数据
  571. * @param {String} name 表名
  572. * @param {*} data 数据
  573. * @param {IDBTransaction} transaction 事务,空则根据表名创建新事务
  574. * @returns {Promise}
  575. */
  576. async add(name, data, transaction = null) {
  577. // 获取表
  578. const store = await this.getStore(name, transaction, "readwrite");
  579.  
  580. // 插入数据
  581. const result = await new Promise((resolve, reject) => {
  582. // 创建请求
  583. const request = store.add(data);
  584.  
  585. // 成功后处理
  586. request.onsuccess = (event) => {
  587. resolve(event.target.result);
  588. };
  589.  
  590. // 失败后处理
  591. request.onerror = (event) => {
  592. reject(event);
  593. };
  594. });
  595.  
  596. // 返回结果
  597. return result;
  598. }
  599.  
  600. /**
  601. * 删除指定表的数据
  602. * @param {String} name 表名
  603. * @param {String} key 主键
  604. * @param {IDBTransaction} transaction 事务,空则根据表名创建新事务
  605. * @returns {Promise}
  606. */
  607. async delete(name, key, transaction = null) {
  608. // 获取表
  609. const store = await this.getStore(name, transaction, "readwrite");
  610.  
  611. // 删除数据
  612. const result = await new Promise((resolve, reject) => {
  613. // 创建请求
  614. const request = store.delete(key);
  615.  
  616. // 成功后处理
  617. request.onsuccess = (event) => {
  618. resolve(event.target.result);
  619. };
  620.  
  621. // 失败后处理
  622. request.onerror = (event) => {
  623. reject(event);
  624. };
  625. });
  626.  
  627. // 返回结果
  628. return result;
  629. }
  630.  
  631. /**
  632. * 插入或修改指定表的数据
  633. * @param {String} name 表名
  634. * @param {*} data 数据
  635. * @param {IDBTransaction} transaction 事务,空则根据表名创建新事务
  636. * @returns {Promise}
  637. */
  638. async put(name, data, transaction = null) {
  639. // 获取表
  640. const store = await this.getStore(name, transaction, "readwrite");
  641.  
  642. // 插入或修改数据
  643. const result = await new Promise((resolve, reject) => {
  644. // 创建请求
  645. const request = store.put(data);
  646.  
  647. // 成功后处理
  648. request.onsuccess = (event) => {
  649. resolve(event.target.result);
  650. };
  651.  
  652. // 失败后处理
  653. request.onerror = (event) => {
  654. reject(event);
  655. };
  656. });
  657.  
  658. // 返回结果
  659. return result;
  660. }
  661.  
  662. /**
  663. * 获取指定表的数据
  664. * @param {String} name 表名
  665. * @param {String} key 主键
  666. * @param {IDBTransaction} transaction 事务,空则根据表名创建新事务
  667. * @returns {Promise} 数据
  668. */
  669. async get(name, key, transaction = null) {
  670. // 获取表
  671. const store = await this.getStore(name, transaction);
  672.  
  673. // 查询数据
  674. const result = await new Promise((resolve, reject) => {
  675. // 创建请求
  676. const request = store.get(key);
  677.  
  678. // 成功后处理
  679. request.onsuccess = (event) => {
  680. resolve(event.target.result);
  681. };
  682.  
  683. // 失败后处理
  684. request.onerror = (event) => {
  685. reject(event);
  686. };
  687. });
  688.  
  689. // 返回结果
  690. return result;
  691. }
  692.  
  693. /**
  694. * 批量插入指定表的数据
  695. * @param {String} name 表名
  696. * @param {Array} data 数据集合
  697. * @param {IDBTransaction} transaction 事务,空则根据表名创建新事务
  698. * @returns {Promise<number>} 成功数量
  699. */
  700. async bulkAdd(name, data, transaction = null) {
  701. // 等待操作结果
  702. const result = await Promise.all(
  703. data.map((item) =>
  704. this.add(name, item, transaction)
  705. .then(() => true)
  706. .catch(() => false)
  707. )
  708. );
  709.  
  710. // 返回受影响的数量
  711. return result.filter((item) => item).length;
  712. }
  713.  
  714. /**
  715. * 批量删除指定表的数据
  716. * @param {String} name 表名
  717. * @param {Array<String>} keys 主键集合,空则删除全部
  718. * @param {IDBTransaction} transaction 事务,空则根据表名创建新事务
  719. * @returns {Promise<number>} 成功数量,删除全部时返回 -1
  720. */
  721. async bulkDelete(name, keys = [], transaction = null) {
  722. // 如果 keys 为空,删除全部数据
  723. if (keys.length === 0) {
  724. // 获取表
  725. const store = await this.getStore(name, transaction, "readwrite");
  726.  
  727. // 清空数据
  728. await new Promise((resolve, reject) => {
  729. // 创建请求
  730. const request = store.clear();
  731.  
  732. // 成功后处理
  733. request.onsuccess = (event) => {
  734. resolve(event.target.result);
  735. };
  736.  
  737. // 失败后处理
  738. request.onerror = (event) => {
  739. reject(event);
  740. };
  741. });
  742.  
  743. return -1;
  744. }
  745.  
  746. // 等待操作结果
  747. const result = await Promise.all(
  748. data.map((item) =>
  749. this.delete(name, item, transaction)
  750. .then(() => true)
  751. .catch(() => false)
  752. )
  753. );
  754.  
  755. // 返回受影响的数量
  756. return result.filter((item) => item).length;
  757. }
  758.  
  759. /**
  760. * 批量插入或修改指定表的数据
  761. * @param {String} name 表名
  762. * @param {Array} data 数据集合
  763. * @param {IDBTransaction} transaction 事务,空则根据表名创建新事务
  764. * @returns {Promise<number>} 成功数量
  765. */
  766. async bulkPut(name, data, transaction = null) {
  767. // 等待操作结果
  768. const result = await Promise.all(
  769. data.map((item) =>
  770. this.put(name, item, transaction)
  771. .then(() => true)
  772. .catch(() => false)
  773. )
  774. );
  775.  
  776. // 返回受影响的数量
  777. return result.filter((item) => item).length;
  778. }
  779.  
  780. /**
  781. * 批量获取指定表的数据
  782. * @param {String} name 表名
  783. * @param {Array<String>} keys 主键集合,空则获取全部
  784. * @param {IDBTransaction} transaction 事务,空则根据表名创建新事务
  785. * @returns {Promise<Array>} 数据集合
  786. */
  787. async bulkGet(name, keys = [], transaction = null) {
  788. // 如果 keys 为空,查询全部数据
  789. if (keys.length === 0) {
  790. // 获取表
  791. const store = await this.getStore(name, transaction);
  792.  
  793. // 查询数据
  794. const result = await new Promise((resolve, reject) => {
  795. // 创建请求
  796. const request = store.getAll();
  797.  
  798. // 成功后处理
  799. request.onsuccess = (event) => {
  800. resolve(event.target.result || []);
  801. };
  802.  
  803. // 失败后处理
  804. request.onerror = (event) => {
  805. reject(event);
  806. };
  807. });
  808.  
  809. // 返回结果
  810. return result;
  811. }
  812.  
  813. // 返回符合的结果
  814. const result = [];
  815.  
  816. await Promise.all(
  817. keys.map((key) =>
  818. this.get(name, key, transaction)
  819. .then((item) => {
  820. result.push(item);
  821. })
  822. .catch(() => {})
  823. )
  824. );
  825.  
  826. return result;
  827. }
  828. }
  829.  
  830. /**
  831. * 油猴存储
  832. *
  833. * 虽然使用了不支持 Promise 的 GM_getValue 与 GM_setValue,但是为了配合 IndexedDB,统一视为 Promise
  834. */
  835. class GMStorage extends DBStorage {
  836. /**
  837. * 初始化
  838. * @param {*} modules 模块列表
  839. */
  840. constructor(modules) {
  841. super(modules);
  842. }
  843.  
  844. /**
  845. * 插入指定表的数据
  846. * @param {String} name 表名
  847. * @param {*} data 数据
  848. * @returns {Promise}
  849. */
  850. async add(name, data) {
  851. // 如果不在模块列表里,写入全部数据
  852. if (Object.hasOwn(this.modules, name) === false) {
  853. return GM_setValue(name, data);
  854. }
  855.  
  856. // 如果支持 IndexedDB,使用 IndexedDB
  857. if (super.isSupport()) {
  858. return super.add(name, data);
  859. }
  860.  
  861. // 获取对应的主键
  862. const keyPath = this.modules[name].keyPath;
  863. const key = data[keyPath];
  864.  
  865. // 如果数据中不包含主键,抛出异常
  866. if (key === undefined) {
  867. throw new Error();
  868. }
  869.  
  870. // 获取全部数据
  871. const values = GM_getValue(name, {});
  872.  
  873. // 如果对应主键已存在,抛出异常
  874. if (Object.hasOwn(values, key)) {
  875. throw new Error();
  876. }
  877.  
  878. // 插入数据
  879. values[key] = data;
  880.  
  881. // 保存数据
  882. GM_setValue(name, values);
  883. }
  884.  
  885. /**
  886. * 删除指定表的数据
  887. * @param {String} name 表名
  888. * @param {String} key 主键
  889. * @returns {Promise}
  890. */
  891. async delete(name, key) {
  892. // 如果不在模块列表里,忽略 key,删除全部数据
  893. if (Object.hasOwn(this.modules, name) === false) {
  894. return GM_setValue(name, {});
  895. }
  896.  
  897. // 如果支持 IndexedDB,使用 IndexedDB
  898. if (super.isSupport()) {
  899. return super.delete(name, key);
  900. }
  901.  
  902. // 获取全部数据
  903. const values = GM_getValue(name, {});
  904.  
  905. // 如果对应主键不存在,抛出异常
  906. if (Object.hasOwn(values, key) === false) {
  907. throw new Error();
  908. }
  909.  
  910. // 删除数据
  911. delete values[key];
  912.  
  913. // 保存数据
  914. GM_setValue(name, values);
  915. }
  916.  
  917. /**
  918. * 插入或修改指定表的数据
  919. * @param {String} name 表名
  920. * @param {*} data 数据
  921. * @returns {Promise}
  922. */
  923. async put(name, data) {
  924. // 如果不在模块列表里,写入全部数据
  925. if (Object.hasOwn(this.modules, name) === false) {
  926. return GM_setValue(name, data);
  927. }
  928.  
  929. // 如果支持 IndexedDB,使用 IndexedDB
  930. if (super.isSupport()) {
  931. return super.put(name, data);
  932. }
  933.  
  934. // 获取对应的主键
  935. const keyPath = this.modules[name].keyPath;
  936. const key = data[keyPath];
  937.  
  938. // 如果数据中不包含主键,抛出异常
  939. if (key === undefined) {
  940. throw new Error();
  941. }
  942.  
  943. // 获取全部数据
  944. const values = GM_getValue(name, {});
  945.  
  946. // 插入或修改数据
  947. values[key] = data;
  948.  
  949. // 保存数据
  950. GM_setValue(name, values);
  951. }
  952.  
  953. /**
  954. * 获取指定表的数据
  955. * @param {String} name 表名
  956. * @param {String} key 主键
  957. * @returns {Promise} 数据
  958. */
  959. async get(name, key) {
  960. // 如果不在模块列表里,忽略 key,返回全部数据
  961. if (Object.hasOwn(this.modules, name) === false) {
  962. return GM_getValue(name);
  963. }
  964.  
  965. // 如果支持 IndexedDB,使用 IndexedDB
  966. if (super.isSupport()) {
  967. return super.get(name, key);
  968. }
  969.  
  970. // 获取全部数据
  971. const values = GM_getValue(name, {});
  972.  
  973. // 如果对应主键不存在,抛出异常
  974. if (Object.hasOwn(values, key) === false) {
  975. throw new Error();
  976. }
  977.  
  978. // 返回结果
  979. return values[key];
  980. }
  981.  
  982. /**
  983. * 批量插入指定表的数据
  984. * @param {String} name 表名
  985. * @param {Array} data 数据集合
  986. * @returns {Promise<number>} 成功数量
  987. */
  988. async bulkAdd(name, data) {
  989. // 如果不在模块列表里,写入全部数据
  990. if (Object.hasOwn(this.modules, name) === false) {
  991. return GM_setValue(name, {});
  992. }
  993.  
  994. // 如果支持 IndexedDB,使用 IndexedDB
  995. if (super.isSupport()) {
  996. return super.bulkAdd(name, data);
  997. }
  998.  
  999. // 获取对应的主键
  1000. const keyPath = this.modules[name].keyPath;
  1001.  
  1002. // 获取全部数据
  1003. const values = GM_getValue(name, {});
  1004.  
  1005. // 添加数据
  1006. const result = data.map((item) => {
  1007. const key = item[keyPath];
  1008.  
  1009. // 如果数据中不包含主键,抛出异常
  1010. if (key === undefined) {
  1011. return false;
  1012. }
  1013.  
  1014. // 如果对应主键已存在,抛出异常
  1015. if (Object.hasOwn(values, key)) {
  1016. return false;
  1017. }
  1018.  
  1019. // 插入数据
  1020. values[key] = item;
  1021.  
  1022. return true;
  1023. });
  1024.  
  1025. // 保存数据
  1026. GM_setValue(name, values);
  1027.  
  1028. // 返回受影响的数量
  1029. return result.filter((item) => item).length;
  1030. }
  1031.  
  1032. /**
  1033. * 批量删除指定表的数据
  1034. * @param {String} name 表名
  1035. * @param {Array<String>} keys 主键集合,空则删除全部
  1036. * @returns {Promise<number>} 成功数量,删除全部时返回 -1
  1037. */
  1038. async bulkDelete(name, keys = []) {
  1039. // 如果不在模块列表里,忽略 keys,删除全部数据
  1040. if (Object.hasOwn(this.modules, name) === false) {
  1041. return GM_setValue(name, {});
  1042. }
  1043.  
  1044. // 如果支持 IndexedDB,使用 IndexedDB
  1045. if (super.isSupport()) {
  1046. return super.bulkDelete(name, keys);
  1047. }
  1048.  
  1049. // 如果 keys 为空,删除全部数据
  1050. if (keys.length === 0) {
  1051. GM_setValue(name, {});
  1052.  
  1053. return -1;
  1054. }
  1055.  
  1056. // 获取全部数据
  1057. const values = GM_getValue(name, {});
  1058.  
  1059. // 删除数据
  1060. const result = keys.map((key) => {
  1061. // 如果对应主键不存在,抛出异常
  1062. if (Object.hasOwn(values, key) === false) {
  1063. return false;
  1064. }
  1065.  
  1066. // 删除数据
  1067. delete values[key];
  1068.  
  1069. return true;
  1070. });
  1071.  
  1072. // 保存数据
  1073. GM_setValue(name, values);
  1074.  
  1075. // 返回受影响的数量
  1076. return result.filter((item) => item).length;
  1077. }
  1078.  
  1079. /**
  1080. * 批量插入或修改指定表的数据
  1081. * @param {String} name 表名
  1082. * @param {Array} data 数据集合
  1083. * @returns {Promise<number>} 成功数量
  1084. */
  1085. async bulkPut(name, data) {
  1086. // 如果不在模块列表里,写入全部数据
  1087. if (Object.hasOwn(this.modules, name) === false) {
  1088. return GM_setValue(name, data);
  1089. }
  1090.  
  1091. // 如果支持 IndexedDB,使用 IndexedDB
  1092. if (super.isSupport()) {
  1093. return super.bulkPut(name, keys);
  1094. }
  1095.  
  1096. // 获取对应的主键
  1097. const keyPath = this.modules[name].keyPath;
  1098.  
  1099. // 获取全部数据
  1100. const values = GM_getValue(name, {});
  1101.  
  1102. // 添加数据
  1103. const result = data.map((item) => {
  1104. const key = item[keyPath];
  1105.  
  1106. // 如果数据中不包含主键,抛出异常
  1107. if (key === undefined) {
  1108. return false;
  1109. }
  1110.  
  1111. // 插入数据
  1112. values[key] = item;
  1113.  
  1114. return true;
  1115. });
  1116.  
  1117. // 保存数据
  1118. GM_setValue(name, values);
  1119.  
  1120. // 返回受影响的数量
  1121. return result.filter((item) => item).length;
  1122. }
  1123.  
  1124. /**
  1125. * 批量获取指定表的数据,如果不在模块列表里,返回全部数据
  1126. * @param {String} name 表名
  1127. * @param {Array<String>} keys 主键集合,空则获取全部
  1128. * @returns {Promise<Array>} 数据集合
  1129. */
  1130. async bulkGet(name, keys = []) {
  1131. // 如果不在模块列表里,忽略 keys,返回全部数据
  1132. if (Object.hasOwn(this.modules, name) === false) {
  1133. return GM_getValue(name);
  1134. }
  1135.  
  1136. // 如果支持 IndexedDB,使用 IndexedDB
  1137. if (super.isSupport()) {
  1138. return super.bulkGet(name, keys);
  1139. }
  1140.  
  1141. // 获取全部数据
  1142. const values = GM_getValue(name, {});
  1143.  
  1144. // 如果 keys 为空,返回全部数据
  1145. if (keys.length === 0) {
  1146. return Object.values(values);
  1147. }
  1148.  
  1149. // 返回符合的结果
  1150. const result = [];
  1151.  
  1152. keys.forEach((key) => {
  1153. if (Object.hasOwn(values, key)) {
  1154. result.push(values[key]);
  1155. }
  1156. });
  1157.  
  1158. return result;
  1159. }
  1160. }
  1161.  
  1162. /**
  1163. * 缓存管理
  1164. *
  1165. * 在存储的基础上,增加了过期时间和持久化选项,自动清理缓存
  1166. */
  1167. class Cache extends GMStorage {
  1168. /**
  1169. * 增加模块列表的 timestamp 索引
  1170. * @param {*} modules 模块列表
  1171. */
  1172. constructor(modules) {
  1173. Object.values(modules).forEach((item) => {
  1174. item.indexes = item.indexes || [];
  1175.  
  1176. if (item.indexes.includes("timestamp") === false) {
  1177. item.indexes.push("timestamp");
  1178. }
  1179. });
  1180.  
  1181. super(modules);
  1182.  
  1183. this.autoClear();
  1184. }
  1185.  
  1186. /**
  1187. * 插入指定表的数据,并增加 timestamp
  1188. * @param {String} name 表名
  1189. * @param {*} data 数据
  1190. * @returns {Promise}
  1191. */
  1192. async add(name, data) {
  1193. // 如果在模块里,增加 timestamp
  1194. if (Object.hasOwn(this.modules, name)) {
  1195. data.timestamp = data.timestamp || new Date().getTime();
  1196. }
  1197.  
  1198. return super.add(name, data);
  1199. }
  1200.  
  1201. /**
  1202. * 插入或修改指定表的数据,并增加 timestamp
  1203. * @param {String} name 表名
  1204. * @param {*} data 数据
  1205. * @returns {Promise}
  1206. */
  1207. async put(name, data) {
  1208. // 如果在模块里,增加 timestamp
  1209. if (Object.hasOwn(this.modules, name)) {
  1210. data.timestamp = data.timestamp || new Date().getTime();
  1211. }
  1212.  
  1213. return super.put(name, data);
  1214. }
  1215.  
  1216. /**
  1217. * 获取指定表的数据,并移除过期数据
  1218. * @param {String} name 表名
  1219. * @param {String} key 主键
  1220. * @returns {Promise} 数据
  1221. */
  1222. async get(name, key) {
  1223. // 获取数据
  1224. const value = await super.get(name, key).catch(() => null);
  1225.  
  1226. // 如果不在模块里,直接返回结果
  1227. if (Object.hasOwn(this.modules, name) === false) {
  1228. return value;
  1229. }
  1230.  
  1231. // 如果有结果的话,移除超时数据
  1232. if (value) {
  1233. // 读取模块配置
  1234. const { expireTime, persistent } = this.modules[name];
  1235.  
  1236. // 持久化或未超时
  1237. if (persistent || value.timestamp + expireTime > new Date().getTime()) {
  1238. return value;
  1239. }
  1240.  
  1241. // 移除超时数据
  1242. await super.delete(name, key);
  1243. }
  1244.  
  1245. return null;
  1246. }
  1247.  
  1248. /**
  1249. * 批量插入指定表的数据,并增加 timestamp
  1250. * @param {String} name 表名
  1251. * @param {Array} data 数据集合
  1252. * @returns {Promise<number>} 成功数量
  1253. */
  1254. async bulkAdd(name, data) {
  1255. // 如果在模块里,增加 timestamp
  1256. if (Object.hasOwn(this.modules, name)) {
  1257. data.forEach((item) => {
  1258. item.timestamp = item.timestamp || new Date().getTime();
  1259. });
  1260. }
  1261.  
  1262. return super.bulkAdd(name, data);
  1263. }
  1264.  
  1265. /**
  1266. * 批量删除指定表的数据
  1267. * @param {String} name 表名
  1268. * @param {Array<String>} keys 主键集合,空则删除全部
  1269. * @param {boolean} force 是否强制删除,否则只删除过期数据
  1270. * @returns {Promise<number>} 成功数量,删除全部时返回 -1
  1271. */
  1272. async bulkDelete(name, keys = [], force = false) {
  1273. // 如果不在模块里,强制删除
  1274. if (Object.hasOwn(this.modules, name) === false) {
  1275. force = true;
  1276. }
  1277.  
  1278. // 强制删除
  1279. if (force) {
  1280. return super.bulkDelete(name, keys);
  1281. }
  1282.  
  1283. // 批量获取指定表的数据,并移除过期数据
  1284. const result = this.bulkGet(name, keys);
  1285.  
  1286. // 返回成功数量
  1287. if (keys.length === 0) {
  1288. return -1;
  1289. }
  1290.  
  1291. return keys.length - result.length;
  1292. }
  1293.  
  1294. /**
  1295. * 批量插入或修改指定表的数据,并增加 timestamp
  1296. * @param {String} name 表名
  1297. * @param {Array} data 数据集合
  1298. * @returns {Promise<number>} 成功数量
  1299. */
  1300. async bulkPut(name, data) {
  1301. // 如果在模块里,增加 timestamp
  1302. if (Object.hasOwn(this.modules, name)) {
  1303. data.forEach((item) => {
  1304. item.timestamp = item.timestamp || new Date().getTime();
  1305. });
  1306. }
  1307.  
  1308. return super.bulkPut(name, data);
  1309. }
  1310.  
  1311. /**
  1312. * 批量获取指定表的数据,并移除过期数据
  1313. * @param {String} name 表名
  1314. * @param {Array<String>} keys 主键集合,空则获取全部
  1315. * @returns {Promise<Array>} 数据集合
  1316. */
  1317. async bulkGet(name, keys = []) {
  1318. // 获取数据
  1319. const values = await super.bulkGet(name, keys).catch(() => []);
  1320.  
  1321. // 如果不在模块里,直接返回结果
  1322. if (Object.hasOwn(this.modules, name) === false) {
  1323. return values;
  1324. }
  1325.  
  1326. // 读取模块配置
  1327. const { keyPath, expireTime, persistent } = this.modules[name];
  1328.  
  1329. // 筛选出超时数据
  1330. const result = [];
  1331. const expired = [];
  1332.  
  1333. values.forEach((value) => {
  1334. // 持久化或未超时
  1335. if (persistent || value.timestamp + expireTime > new Date().getTime()) {
  1336. result.push(value);
  1337. return;
  1338. }
  1339.  
  1340. // 记录超时数据
  1341. expired.push(value[keyPath]);
  1342. });
  1343.  
  1344. // 移除超时数据
  1345. await super.bulkDelete(name, expired);
  1346.  
  1347. // 返回结果
  1348. return result;
  1349. }
  1350.  
  1351. /**
  1352. * 自动清理缓存
  1353. */
  1354. async autoClear() {
  1355. const data = await this.get(CLEAR_TIME_KEY);
  1356.  
  1357. const now = new Date();
  1358. const clearTime = new Date(data || 0);
  1359.  
  1360. const isToday =
  1361. now.getDate() === clearTime.getDate() &&
  1362. now.getMonth() === clearTime.getMonth() &&
  1363. now.getFullYear() === clearTime.getFullYear();
  1364.  
  1365. if (isToday) {
  1366. return;
  1367. }
  1368.  
  1369. await Promise.all(
  1370. Object.keys(this.modules).map((name) => this.bulkDelete(name))
  1371. );
  1372.  
  1373. await this.put(CLEAR_TIME_KEY, now.getTime());
  1374. }
  1375. }
  1376.  
  1377. /**
  1378. * 设置
  1379. *
  1380. * 暂时整体处理模块设置,后续再拆分
  1381. */
  1382. class Settings {
  1383. /**
  1384. * 缓存管理
  1385. */
  1386. cache;
  1387.  
  1388. /**
  1389. * 当前设置
  1390. */
  1391. data = null;
  1392.  
  1393. /**
  1394. * 初始化并绑定缓存管理
  1395. * @param {Cache} cache 缓存管理
  1396. */
  1397. constructor(cache) {
  1398. this.cache = cache;
  1399. }
  1400.  
  1401. /**
  1402. * 读取设置
  1403. */
  1404. async load() {
  1405. // 读取设置
  1406. if (this.data === null) {
  1407. // 默认配置
  1408. const defaultData = {
  1409. tags: {},
  1410. users: {},
  1411. keywords: {},
  1412. locations: {},
  1413. options: {
  1414. filterRegdateLimit: 0,
  1415. filterPostnumLimit: 0,
  1416. filterTopicRateLimit: 100,
  1417. filterReputationLimit: NaN,
  1418. filterAnony: false,
  1419. filterMode: "隐藏",
  1420. },
  1421. };
  1422.  
  1423. // 读取数据
  1424. const storedData = await this.cache
  1425. .get(DATA_KEY)
  1426. .then((values) => values || {});
  1427.  
  1428. // 写入缓存
  1429. this.data = Tools.merge({}, defaultData, storedData);
  1430.  
  1431. // 写入默认模块选项
  1432. if (Object.hasOwn(this.data, "modules") === false) {
  1433. this.data.modules = ["user", "tag", "misc"];
  1434.  
  1435. if (Object.keys(this.data.keywords).length > 0) {
  1436. this.data.modules.push("keyword");
  1437. }
  1438.  
  1439. if (Object.keys(this.data.locations).length > 0) {
  1440. this.data.modules.push("location");
  1441. }
  1442. }
  1443. }
  1444.  
  1445. // 返回设置
  1446. return this.data;
  1447. }
  1448.  
  1449. /**
  1450. * 写入设置
  1451. */
  1452. async save() {
  1453. return this.cache.put(DATA_KEY, this.data);
  1454. }
  1455.  
  1456. /**
  1457. * 获取模块列表
  1458. */
  1459. get modules() {
  1460. return this.data.modules;
  1461. }
  1462.  
  1463. /**
  1464. * 设置模块列表
  1465. */
  1466. set modules(values) {
  1467. this.data.modules = values;
  1468. this.save();
  1469. }
  1470.  
  1471. /**
  1472. * 获取标签列表
  1473. */
  1474. get tags() {
  1475. return this.data.tags;
  1476. }
  1477.  
  1478. /**
  1479. * 设置标签列表
  1480. */
  1481. set tags(values) {
  1482. this.data.tags = values;
  1483. this.save();
  1484. }
  1485.  
  1486. /**
  1487. * 获取用户列表
  1488. */
  1489. get users() {
  1490. return this.data.users;
  1491. }
  1492.  
  1493. /**
  1494. * 设置用户列表
  1495. */
  1496. set users(values) {
  1497. this.data.users = values;
  1498. this.save();
  1499. }
  1500.  
  1501. /**
  1502. * 获取关键字列表
  1503. */
  1504. get keywords() {
  1505. return this.data.keywords;
  1506. }
  1507.  
  1508. /**
  1509. * 设置关键字列表
  1510. */
  1511. set keywords(values) {
  1512. this.data.keywords = values;
  1513. this.save();
  1514. }
  1515.  
  1516. /**
  1517. * 获取属地列表
  1518. */
  1519. get locations() {
  1520. return this.data.locations;
  1521. }
  1522.  
  1523. /**
  1524. * 设置属地列表
  1525. */
  1526. set locations(values) {
  1527. this.data.locations = values;
  1528. this.save();
  1529. }
  1530.  
  1531. /**
  1532. * 获取默认过滤模式
  1533. */
  1534. get defaultFilterMode() {
  1535. return this.data.options.filterMode;
  1536. }
  1537.  
  1538. /**
  1539. * 设置默认过滤模式
  1540. */
  1541. set defaultFilterMode(value) {
  1542. this.data.options.filterMode = value;
  1543. this.save();
  1544. }
  1545.  
  1546. /**
  1547. * 获取注册时间限制
  1548. */
  1549. get filterRegdateLimit() {
  1550. return this.data.options.filterRegdateLimit || 0;
  1551. }
  1552.  
  1553. /**
  1554. * 设置注册时间限制
  1555. */
  1556. set filterRegdateLimit(value) {
  1557. this.data.options.filterRegdateLimit = value;
  1558. this.save();
  1559. }
  1560.  
  1561. /**
  1562. * 获取发帖数量限制
  1563. */
  1564. get filterPostnumLimit() {
  1565. return this.data.options.filterPostnumLimit || 0;
  1566. }
  1567.  
  1568. /**
  1569. * 设置发帖数量限制
  1570. */
  1571. set filterPostnumLimit(value) {
  1572. this.data.options.filterPostnumLimit = value;
  1573. this.save();
  1574. }
  1575.  
  1576. /**
  1577. * 获取发帖比例限制
  1578. */
  1579. get filterTopicRateLimit() {
  1580. return this.data.options.filterTopicRateLimit || 100;
  1581. }
  1582.  
  1583. /**
  1584. * 设置发帖比例限制
  1585. */
  1586. set filterTopicRateLimit(value) {
  1587. this.data.options.filterTopicRateLimit = value;
  1588. this.save();
  1589. }
  1590.  
  1591. /**
  1592. * 获取版面声望限制
  1593. */
  1594. get filterReputationLimit() {
  1595. return this.data.options.filterReputationLimit || NaN;
  1596. }
  1597.  
  1598. /**
  1599. * 设置版面声望限制
  1600. */
  1601. set filterReputationLimit(value) {
  1602. this.data.options.filterReputationLimit = value;
  1603. this.save();
  1604. }
  1605.  
  1606. /**
  1607. * 获取是否过滤匿名
  1608. */
  1609. get filterAnonymous() {
  1610. return this.data.options.filterAnony || false;
  1611. }
  1612.  
  1613. /**
  1614. * 设置是否过滤匿名
  1615. */
  1616. set filterAnonymous(value) {
  1617. this.data.options.filterAnony = value;
  1618. this.save();
  1619. }
  1620.  
  1621. /**
  1622. * 获取代理设置
  1623. */
  1624. get userAgent() {
  1625. return this.cache.get(USER_AGENT_KEY).then((value) => {
  1626. if (value === undefined) {
  1627. return "Nga_Official";
  1628. }
  1629.  
  1630. return value;
  1631. });
  1632. }
  1633.  
  1634. /**
  1635. * 修改代理设置
  1636. */
  1637. set userAgent(value) {
  1638. this.cache.put(USER_AGENT_KEY, value).then(() => {
  1639. location.reload();
  1640. });
  1641. }
  1642.  
  1643. /**
  1644. * 获取是否启用前置过滤
  1645. */
  1646. get preFilterEnabled() {
  1647. return this.cache.get(PRE_FILTER_KEY).then((value) => {
  1648. if (value === undefined) {
  1649. return true;
  1650. }
  1651.  
  1652. return value;
  1653. });
  1654. }
  1655.  
  1656. /**
  1657. * 设置是否启用前置过滤
  1658. */
  1659. set preFilterEnabled(value) {
  1660. this.cache.put(PRE_FILTER_KEY, value).then(() => {
  1661. location.reload();
  1662. });
  1663. }
  1664.  
  1665. /**
  1666. * 获取过滤模式列表
  1667. *
  1668. * 模拟成从配置中获取
  1669. */
  1670. get filterModes() {
  1671. return ["继承", "标记", "遮罩", "隐藏", "显示"];
  1672. }
  1673.  
  1674. /**
  1675. * 获取指定下标过滤模式
  1676. * @param {Number} index 下标
  1677. */
  1678. getNameByMode(index) {
  1679. const modes = this.filterModes;
  1680.  
  1681. return modes[index] || "";
  1682. }
  1683.  
  1684. /**
  1685. * 获取指定过滤模式下标
  1686. * @param {String} name 过滤模式
  1687. */
  1688. getModeByName(name) {
  1689. const modes = this.filterModes;
  1690.  
  1691. return modes.indexOf(name);
  1692. }
  1693.  
  1694. /**
  1695. * 切换过滤模式
  1696. * @param {String} value 过滤模式
  1697. * @returns {String} 过滤模式
  1698. */
  1699. switchModeByName(value) {
  1700. const index = this.getModeByName(value);
  1701.  
  1702. const nextIndex = (index + 1) % this.filterModes.length;
  1703.  
  1704. return this.filterModes[nextIndex];
  1705. }
  1706. }
  1707.  
  1708. /**
  1709. * API
  1710. */
  1711. class API {
  1712. /**
  1713. * 缓存模块
  1714. */
  1715. static modules = {
  1716. TOPIC_NUM_CACHE: {
  1717. keyPath: "uid",
  1718. version: 1,
  1719. expireTime: 1000 * 60 * 60,
  1720. persistent: true,
  1721. },
  1722. USER_INFO_CACHE: {
  1723. keyPath: "uid",
  1724. version: 1,
  1725. expireTime: 1000 * 60 * 60,
  1726. persistent: false,
  1727. },
  1728. PAGE_CACHE: {
  1729. keyPath: "url",
  1730. version: 1,
  1731. expireTime: 1000 * 60 * 10,
  1732. persistent: false,
  1733. },
  1734. FORUM_POSTED_CACHE: {
  1735. keyPath: "url",
  1736. version: 2,
  1737. expireTime: 1000 * 60 * 60 * 24,
  1738. persistent: true,
  1739. },
  1740. };
  1741.  
  1742. /**
  1743. * 缓存管理
  1744. */
  1745. cache;
  1746.  
  1747. /**
  1748. * 设置
  1749. */
  1750. settings;
  1751.  
  1752. /**
  1753. * 初始化并绑定缓存管理、设置
  1754. * @param {Cache} cache 缓存管理
  1755. * @param {Settings} settings 设置
  1756. */
  1757. constructor(cache, settings) {
  1758. this.cache = cache;
  1759. this.settings = settings;
  1760. }
  1761.  
  1762. /**
  1763. * 简单的统一请求
  1764. * @param {String} url 请求地址
  1765. * @param {Object} config 请求参数
  1766. * @param {Boolean} toJSON 是否转为 JSON 格式
  1767. */
  1768. async request(url, config = {}, toJSON = true) {
  1769. const userAgent = await this.settings.userAgent;
  1770.  
  1771. const response = await fetch(url, {
  1772. headers: {
  1773. "X-User-Agent": userAgent,
  1774. },
  1775. ...config,
  1776. });
  1777.  
  1778. const result = await Tools.readForumData(response, toJSON);
  1779.  
  1780. return result;
  1781. }
  1782.  
  1783. /**
  1784. * 获取用户主题数量
  1785. * @param {number} uid 用户 ID
  1786. */
  1787. async getTopicNum(uid) {
  1788. const name = "TOPIC_NUM_CACHE";
  1789. const expireTime = API.modules[name];
  1790.  
  1791. const api = `/thread.php?lite=js&authorid=${uid}`;
  1792.  
  1793. const cache = await this.cache.get(name, uid);
  1794.  
  1795. // 仍在缓存期间内,直接返回
  1796. if (cache) {
  1797. const expired = cache.timestamp + expireTime < new Date().getTime();
  1798.  
  1799. if (expired === false) {
  1800. return cache.count;
  1801. }
  1802. }
  1803.  
  1804. // 请求数据
  1805. const result = await this.request(api);
  1806.  
  1807. // 服务器可能返回错误,遇到这种情况下,需要保留缓存
  1808. const count = (() => {
  1809. if (result.data) {
  1810. return result.data.__ROWS || 0;
  1811. }
  1812.  
  1813. if (cache) {
  1814. return cache.count;
  1815. }
  1816.  
  1817. return 0;
  1818. })();
  1819.  
  1820. // 更新缓存
  1821. this.cache.put(name, {
  1822. uid,
  1823. count,
  1824. });
  1825.  
  1826. return count;
  1827. }
  1828.  
  1829. /**
  1830. * 获取用户信息
  1831. * @param {number} uid 用户 ID
  1832. */
  1833. async getUserInfo(uid) {
  1834. const name = "USER_INFO_CACHE";
  1835.  
  1836. const api = `/nuke.php?lite=js&__lib=ucp&__act=get&uid=${uid}`;
  1837.  
  1838. const cache = await this.cache.get(name, uid);
  1839.  
  1840. if (cache) {
  1841. return cache.data;
  1842. }
  1843.  
  1844. const result = await this.request(api);
  1845.  
  1846. const data = result.data ? result.data[0] : null;
  1847.  
  1848. if (data) {
  1849. this.cache.put(name, {
  1850. uid,
  1851. data,
  1852. });
  1853. }
  1854.  
  1855. return data || {};
  1856. }
  1857.  
  1858. /**
  1859. * 获取帖子内容、用户信息(主要是发帖数量,常规的获取用户信息方法不一定有结果)、版面声望
  1860. * @param {number} tid 主题 ID
  1861. * @param {number} pid 回复 ID
  1862. */
  1863. async getPostInfo(tid, pid) {
  1864. const name = "PAGE_CACHE";
  1865.  
  1866. const api = pid ? `/read.php?pid=${pid}` : `/read.php?tid=${tid}`;
  1867.  
  1868. const cache = await this.cache.get(name, api);
  1869.  
  1870. if (cache) {
  1871. return cache.data;
  1872. }
  1873.  
  1874. const result = await this.request(api, {}, false);
  1875.  
  1876. const parser = new DOMParser();
  1877.  
  1878. const doc = parser.parseFromString(result, "text/html");
  1879.  
  1880. // 验证帖子正常
  1881. const verify = doc.querySelector("#m_posts");
  1882.  
  1883. if (verify === null) {
  1884. return {};
  1885. }
  1886.  
  1887. // 声明返回值
  1888. const data = {};
  1889.  
  1890. // 取得顶楼 UID
  1891. data.uid = (() => {
  1892. const ele = doc.querySelector("#postauthor0");
  1893.  
  1894. if (ele) {
  1895. const res = ele.getAttribute("href").match(/uid=(\S+)/);
  1896.  
  1897. if (res) {
  1898. return res[1];
  1899. }
  1900. }
  1901.  
  1902. return 0;
  1903. })();
  1904.  
  1905. // 取得顶楼标题
  1906. data.subject = doc.querySelector("#postsubject0").innerHTML;
  1907.  
  1908. // 取得顶楼内容
  1909. data.content = doc.querySelector("#postcontent0").innerHTML;
  1910.  
  1911. // 非匿名用户可以继续取得用户信息和版面声望
  1912. if (data.uid > 0) {
  1913. // 取得用户信息
  1914. data.userInfo = (() => {
  1915. const text = Tools.searchPair(result, `"${data.uid}":`);
  1916.  
  1917. if (text) {
  1918. try {
  1919. return JSON.parse(text);
  1920. } catch {
  1921. return null;
  1922. }
  1923. }
  1924.  
  1925. return null;
  1926. })();
  1927.  
  1928. // 取得用户声望
  1929. data.reputation = (() => {
  1930. const reputations = (() => {
  1931. const text = Tools.searchPair(result, `"__REPUTATIONS":`);
  1932.  
  1933. if (text) {
  1934. try {
  1935. return JSON.parse(text);
  1936. } catch {
  1937. return null;
  1938. }
  1939. }
  1940.  
  1941. return null;
  1942. })();
  1943.  
  1944. if (reputations) {
  1945. for (let fid in reputations) {
  1946. return reputations[fid][data.uid] || 0;
  1947. }
  1948. }
  1949.  
  1950. return NaN;
  1951. })();
  1952. }
  1953.  
  1954. // 写入缓存
  1955. this.cache.put(name, {
  1956. url: api,
  1957. data,
  1958. });
  1959.  
  1960. // 返回结果
  1961. return data;
  1962. }
  1963.  
  1964. /**
  1965. * 获取版面信息
  1966. * @param {number} fid 版面 ID
  1967. */
  1968. async getForumInfo(fid) {
  1969. if (Number.isNaN(fid)) {
  1970. return null;
  1971. }
  1972.  
  1973. const api = `/thread.php?lite=js&fid=${fid}`;
  1974.  
  1975. const result = await this.request(api);
  1976.  
  1977. const info = result.data ? result.data.__F : null;
  1978.  
  1979. return info;
  1980. }
  1981.  
  1982. /**
  1983. * 获取版面发言记录
  1984. * @param {number} fid 版面 ID
  1985. * @param {number} uid 用户 ID
  1986. */
  1987. async getForumPosted(fid, uid) {
  1988. const name = "FORUM_POSTED_CACHE";
  1989. const expireTime = API.modules[name];
  1990.  
  1991. const api = `/thread.php?lite=js&authorid=${uid}&fid=${fid}`;
  1992.  
  1993. const cache = await this.cache.get(name, api);
  1994.  
  1995. if (cache) {
  1996. // 发言是无法撤销的,只要有记录就永远不需要再获取
  1997. // 手动处理没有记录的缓存数据
  1998. const expired = cache.timestamp + expireTime < new Date().getTime();
  1999. if (expired && cache.data === false) {
  2000. await this.cache.delete(name, api);
  2001. }
  2002.  
  2003. return cache.data;
  2004. }
  2005.  
  2006. let isComplete = false;
  2007. let isBusy = false;
  2008.  
  2009. const func = async (url) => {
  2010. if (isComplete || isBusy) {
  2011. return;
  2012. }
  2013.  
  2014. const result = await this.request(url, {}, false);
  2015.  
  2016. // 将所有匹配的 FID 写入缓存,即使并不在设置里
  2017. const matched = result.match(/"fid":(-?\d+),/g);
  2018.  
  2019. if (matched) {
  2020. const list = [
  2021. ...new Set(
  2022. matched.map((item) => parseInt(item.match(/-?\d+/)[0], 10))
  2023. ),
  2024. ];
  2025.  
  2026. list.forEach((item) => {
  2027. const key = api.replace(`&fid=${fid}`, `&fid=${item}`);
  2028.  
  2029. // 写入缓存
  2030. this.cache.put(name, {
  2031. url: key,
  2032. data: true,
  2033. });
  2034.  
  2035. // 已有结果,无需继续查询
  2036. if (fid === item) {
  2037. isComplete = true;
  2038. }
  2039. });
  2040. }
  2041.  
  2042. // 泥潭给版面查询接口增加了限制,经常会出现“服务器忙,请稍后重试”的错误
  2043. if (result.indexOf("服务器忙") > 0) {
  2044. isBusy = true;
  2045. }
  2046. };
  2047.  
  2048. // 先获取回复记录的第一页,顺便可以获取其他版面的记录
  2049. // 没有再通过版面接口获取,避免频繁出现“服务器忙,请稍后重试”的错误
  2050. await func(api.replace(`&fid=${fid}`, `&searchpost=1`));
  2051. await func(api + "&searchpost=1");
  2052. await func(api);
  2053.  
  2054. // 无论成功与否都写入缓存
  2055. if (isComplete === false) {
  2056. // 遇到服务器忙的情况,手动调整缓存时间至 1 小时
  2057. const timestamp = isBusy
  2058. ? new Date().getTime() - (expireTime - 1000 * 60 * 60)
  2059. : new Date().getTime();
  2060.  
  2061. // 写入失败缓存
  2062. this.cache.put(name, {
  2063. url: api,
  2064. data: false,
  2065. timestamp,
  2066. });
  2067. }
  2068.  
  2069. return isComplete;
  2070. }
  2071. }
  2072.  
  2073. /**
  2074. * UI
  2075. */
  2076. class UI {
  2077. /**
  2078. * 标签
  2079. */
  2080. static label = "屏蔽";
  2081.  
  2082. /**
  2083. * 设置
  2084. */
  2085. settings;
  2086.  
  2087. /**
  2088. * API
  2089. */
  2090. api;
  2091.  
  2092. /**
  2093. * 模块列表
  2094. */
  2095. modules = {};
  2096.  
  2097. /**
  2098. * 菜单元素
  2099. */
  2100. menu = null;
  2101.  
  2102. /**
  2103. * 视图元素
  2104. */
  2105. views = {};
  2106.  
  2107. /**
  2108. * 初始化并绑定设置、API,注册脚本菜单
  2109. * @param {Settings} settings 设置
  2110. * @param {API} api API
  2111. */
  2112. constructor(settings, api) {
  2113. this.settings = settings;
  2114. this.api = api;
  2115.  
  2116. this.init();
  2117. }
  2118.  
  2119. /**
  2120. * 初始化,创建基础视图,初始化通用设置
  2121. */
  2122. init() {
  2123. const tabs = this.createTabs({
  2124. className: "right_",
  2125. });
  2126.  
  2127. const content = this.createElement("DIV", [], {
  2128. style: "width: 80vw;",
  2129. });
  2130.  
  2131. const container = this.createElement("DIV", [tabs, content]);
  2132.  
  2133. this.views = {
  2134. tabs,
  2135. content,
  2136. container,
  2137. };
  2138.  
  2139. this.initSettings();
  2140. }
  2141.  
  2142. /**
  2143. * 初始化设置
  2144. */
  2145. initSettings() {
  2146. // 创建基础视图
  2147. const settings = this.createElement("DIV", []);
  2148.  
  2149. // 添加设置项
  2150. const add = (order, ...elements) => {
  2151. const items = [...settings.childNodes];
  2152.  
  2153. if (items.find((item) => item.order === order)) {
  2154. return;
  2155. }
  2156.  
  2157. const item = this.createElement(
  2158. "DIV",
  2159. [...elements, this.createElement("BR", [])],
  2160. {
  2161. order,
  2162. }
  2163. );
  2164.  
  2165. const anchor = items.find((item) => item.order > order);
  2166.  
  2167. settings.insertBefore(item, anchor || null);
  2168.  
  2169. return item;
  2170. };
  2171.  
  2172. // 绑定事件
  2173. Object.assign(settings, {
  2174. add,
  2175. });
  2176.  
  2177. // 合并视图
  2178. Object.assign(this.views, {
  2179. settings,
  2180. });
  2181.  
  2182. // 创建标签页
  2183. const { tabs, content } = this.views;
  2184.  
  2185. this.createTab(tabs, "设置", Number.MAX_SAFE_INTEGER, {
  2186. onclick: () => {
  2187. content.innerHTML = "";
  2188. content.appendChild(settings);
  2189. },
  2190. });
  2191. }
  2192.  
  2193. /**
  2194. * 弹窗确认
  2195. * @param {String} message 提示信息
  2196. * @returns {Promise}
  2197. */
  2198. confirm(message = "是否确认?") {
  2199. return new Promise((resolve, reject) => {
  2200. const result = confirm(message);
  2201.  
  2202. if (result) {
  2203. resolve();
  2204. return;
  2205. }
  2206.  
  2207. reject();
  2208. });
  2209. }
  2210.  
  2211. /**
  2212. * 折叠
  2213. * @param {String | Number} key 标识
  2214. * @param {HTMLElement} element 目标元素
  2215. * @param {String} content 内容
  2216. */
  2217. collapse(key, element, content) {
  2218. key = "collapsed_" + key;
  2219.  
  2220. element.innerHTML = `
  2221. <div class="lessernuke" style="background: #81C7D4; border-color: #66BAB7;">
  2222. <span class="crimson">Troll must die.</span>
  2223. <a href="javascript:void(0)" onclick="[...document.getElementsByName('${key}')].forEach(item => item.style.display = '')">点击查看</a>
  2224. <div style="display: none;" name="${key}">
  2225. ${content}
  2226. </div>
  2227. </div>`;
  2228. }
  2229.  
  2230. /**
  2231. * 创建元素
  2232. * @param {String} tagName 标签
  2233. * @param {HTMLElement | HTMLElement[] | String} content 内容,元素或者 innerHTML
  2234. * @param {*} properties 额外属性
  2235. * @returns {HTMLElement} 元素
  2236. */
  2237. createElement(tagName, content, properties = {}) {
  2238. const element = document.createElement(tagName);
  2239.  
  2240. // 写入内容
  2241. if (typeof content === "string") {
  2242. element.innerHTML = content;
  2243. } else {
  2244. if (Array.isArray(content) === false) {
  2245. content = [content];
  2246. }
  2247.  
  2248. content.forEach((item) => {
  2249. if (item === null) {
  2250. return;
  2251. }
  2252.  
  2253. if (typeof item === "string") {
  2254. element.append(item);
  2255. return;
  2256. }
  2257.  
  2258. element.appendChild(item);
  2259. });
  2260. }
  2261.  
  2262. // 对 A 标签的额外处理
  2263. if (tagName.toUpperCase() === "A") {
  2264. if (Object.hasOwn(properties, "href") === false) {
  2265. properties.href = "javascript: void(0);";
  2266. }
  2267. }
  2268.  
  2269. // 附加属性
  2270. Object.entries(properties).forEach(([key, value]) => {
  2271. element[key] = value;
  2272. });
  2273.  
  2274. return element;
  2275. }
  2276.  
  2277. /**
  2278. * 创建按钮
  2279. * @param {String} text 文字
  2280. * @param {Function} onclick 点击事件
  2281. * @param {*} properties 额外属性
  2282. */
  2283. createButton(text, onclick, properties = {}) {
  2284. return this.createElement("BUTTON", text, {
  2285. ...properties,
  2286. onclick,
  2287. });
  2288. }
  2289.  
  2290. /**
  2291. * 创建按钮组
  2292. * @param {Array} buttons 按钮集合
  2293. */
  2294. createButtonGroup(...buttons) {
  2295. return this.createElement("DIV", buttons, {
  2296. className: "filter-button-group",
  2297. });
  2298. }
  2299.  
  2300. /**
  2301. * 创建表格
  2302. * @param {Array} headers 表头集合
  2303. * @param {*} properties 额外属性
  2304. * @returns {HTMLElement} 元素和相关函数
  2305. */
  2306. createTable(headers, properties = {}) {
  2307. const rows = [];
  2308.  
  2309. const ths = headers.map((item, index) =>
  2310. this.createElement("TH", item.label, {
  2311. ...item,
  2312. className: `c${index + 1}`,
  2313. })
  2314. );
  2315.  
  2316. const tr =
  2317. ths.length > 0
  2318. ? this.createElement("TR", ths, {
  2319. className: "block_txt_c0",
  2320. })
  2321. : null;
  2322.  
  2323. const thead = tr !== null ? this.createElement("THEAD", tr) : null;
  2324.  
  2325. const tbody = this.createElement("TBODY", []);
  2326.  
  2327. const table = this.createElement("TABLE", [thead, tbody], {
  2328. ...properties,
  2329. className: "filter-table forumbox",
  2330. });
  2331.  
  2332. const wrapper = this.createElement("DIV", table, {
  2333. className: "filter-table-wrapper",
  2334. });
  2335.  
  2336. const intersectionObserver = new IntersectionObserver((entries) => {
  2337. if (entries[0].intersectionRatio <= 0) return;
  2338.  
  2339. const list = rows.splice(0, 10);
  2340.  
  2341. if (list.length === 0) {
  2342. return;
  2343. }
  2344.  
  2345. intersectionObserver.disconnect();
  2346.  
  2347. tbody.append(...list);
  2348.  
  2349. intersectionObserver.observe(tbody.lastElementChild);
  2350. });
  2351.  
  2352. const add = (...columns) => {
  2353. const tds = columns.map((column, index) => {
  2354. if (ths[index]) {
  2355. const { center, ellipsis } = ths[index];
  2356.  
  2357. const properties = {};
  2358.  
  2359. if (center) {
  2360. properties.style = "text-align: center;";
  2361. }
  2362.  
  2363. if (ellipsis) {
  2364. properties.className = "filter-text-ellipsis";
  2365. }
  2366.  
  2367. column = this.createElement("DIV", column, properties);
  2368. }
  2369.  
  2370. return this.createElement("TD", column, {
  2371. className: `c${index + 1}`,
  2372. });
  2373. });
  2374.  
  2375. const tr = this.createElement("TR", tds, {
  2376. className: `row${(rows.length % 2) + 1}`,
  2377. });
  2378.  
  2379. intersectionObserver.disconnect();
  2380.  
  2381. rows.push(tr);
  2382.  
  2383. intersectionObserver.observe(tbody.lastElementChild || tbody);
  2384. };
  2385.  
  2386. const update = (e, ...columns) => {
  2387. const row = e.target.closest("TR");
  2388.  
  2389. if (row) {
  2390. const tds = row.querySelectorAll("TD");
  2391.  
  2392. columns.map((column, index) => {
  2393. if (ths[index]) {
  2394. const { center, ellipsis } = ths[index];
  2395.  
  2396. const properties = {};
  2397.  
  2398. if (center) {
  2399. properties.style = "text-align: center;";
  2400. }
  2401.  
  2402. if (ellipsis) {
  2403. properties.className = "filter-text-ellipsis";
  2404. }
  2405.  
  2406. column = this.createElement("DIV", column, properties);
  2407. }
  2408.  
  2409. if (tds[index]) {
  2410. tds[index].innerHTML = "";
  2411. tds[index].append(column);
  2412. }
  2413. });
  2414. }
  2415. };
  2416.  
  2417. const remove = (e) => {
  2418. const row = e.target.closest("TR");
  2419.  
  2420. if (row) {
  2421. tbody.removeChild(row);
  2422. }
  2423. };
  2424.  
  2425. const clear = () => {
  2426. rows.splice(0);
  2427. intersectionObserver.disconnect();
  2428.  
  2429. tbody.innerHTML = "";
  2430. };
  2431.  
  2432. Object.assign(wrapper, {
  2433. add,
  2434. update,
  2435. remove,
  2436. clear,
  2437. });
  2438.  
  2439. return wrapper;
  2440. }
  2441.  
  2442. /**
  2443. * 创建标签组
  2444. * @param {*} properties 额外属性
  2445. */
  2446. createTabs(properties = {}) {
  2447. const tabs = this.createElement(
  2448. "DIV",
  2449. `<table class="stdbtn" cellspacing="0">
  2450. <tbody>
  2451. <tr></tr>
  2452. </tbody>
  2453. </table>`,
  2454. properties
  2455. );
  2456.  
  2457. return this.createElement(
  2458. "DIV",
  2459. [
  2460. tabs,
  2461. this.createElement("DIV", [], {
  2462. className: "clear",
  2463. }),
  2464. ],
  2465. {
  2466. style: "display: none; margin-bottom: 5px;",
  2467. }
  2468. );
  2469. }
  2470.  
  2471. /**
  2472. * 创建标签
  2473. * @param {Element} tabs 标签组
  2474. * @param {String} label 标签名称
  2475. * @param {Number} order 标签顺序,重复则跳过
  2476. * @param {*} properties 额外属性
  2477. */
  2478. createTab(tabs, label, order, properties = {}) {
  2479. const group = tabs.querySelector("TR");
  2480.  
  2481. const items = [...group.childNodes];
  2482.  
  2483. if (items.find((item) => item.order === order)) {
  2484. return;
  2485. }
  2486.  
  2487. if (items.length > 0) {
  2488. tabs.style.removeProperty("display");
  2489. }
  2490.  
  2491. const tab = this.createElement("A", label, {
  2492. ...properties,
  2493. className: "nobr silver",
  2494. onclick: () => {
  2495. if (tab.className === "nobr") {
  2496. return;
  2497. }
  2498.  
  2499. group.querySelectorAll("A").forEach((item) => {
  2500. if (item === tab) {
  2501. item.className = "nobr";
  2502. } else {
  2503. item.className = "nobr silver";
  2504. }
  2505. });
  2506.  
  2507. if (properties.onclick) {
  2508. properties.onclick();
  2509. }
  2510. },
  2511. });
  2512.  
  2513. const wrapper = this.createElement("TD", tab, {
  2514. order,
  2515. });
  2516.  
  2517. const anchor = items.find((item) => item.order > order);
  2518.  
  2519. group.insertBefore(wrapper, anchor || null);
  2520.  
  2521. return wrapper;
  2522. }
  2523.  
  2524. /**
  2525. * 创建对话框
  2526. * @param {HTMLElement | null} anchor 要绑定的元素,如果为空,直接弹出
  2527. * @param {String} title 对话框的标题
  2528. * @param {HTMLElement} content 对话框的内容
  2529. */
  2530. createDialog(anchor, title, content) {
  2531. let window;
  2532.  
  2533. const show = () => {
  2534. if (window === undefined) {
  2535. window = commonui.createCommmonWindow();
  2536. }
  2537.  
  2538. window._.addContent(null);
  2539. window._.addTitle(title);
  2540. window._.addContent(content);
  2541. window._.show();
  2542. };
  2543.  
  2544. if (anchor) {
  2545. anchor.onclick = show;
  2546. } else {
  2547. show();
  2548. }
  2549.  
  2550. return window;
  2551. }
  2552.  
  2553. /**
  2554. * 渲染菜单
  2555. */
  2556. renderMenu() {
  2557. // 如果泥潭的右上角菜单还没有加载完成,说明模块尚未加载完毕,跳过
  2558. const anchor = document.querySelector("#mainmenu .td:last-child");
  2559.  
  2560. if (anchor === null) {
  2561. return;
  2562. }
  2563.  
  2564. const menu = this.createElement("A", this.constructor.label, {
  2565. className: "mmdefault nobr",
  2566. });
  2567.  
  2568. const container = this.createElement("DIV", menu, {
  2569. className: "td",
  2570. });
  2571.  
  2572. // 插入菜单
  2573. anchor.before(container);
  2574.  
  2575. // 绑定菜单元素
  2576. this.menu = menu;
  2577. }
  2578.  
  2579. /**
  2580. * 渲染视图
  2581. */
  2582. renderView() {
  2583. // 如果菜单还没有渲染,说明模块尚未加载完毕,跳过
  2584. if (this.menu === null) {
  2585. return;
  2586. }
  2587.  
  2588. // 绑定菜单点击事件.
  2589. this.createDialog(
  2590. this.menu,
  2591. this.constructor.label,
  2592. this.views.container
  2593. );
  2594.  
  2595. // 启用第一个模块
  2596. this.views.tabs.querySelector("A").click();
  2597. }
  2598.  
  2599. /**
  2600. * 渲染
  2601. */
  2602. render() {
  2603. this.renderMenu();
  2604. this.renderView();
  2605. }
  2606. }
  2607.  
  2608. /**
  2609. * 基础模块
  2610. */
  2611. class Module {
  2612. /**
  2613. * 模块名称
  2614. */
  2615. static name;
  2616.  
  2617. /**
  2618. * 模块标签
  2619. */
  2620. static label;
  2621.  
  2622. /**
  2623. * 顺序
  2624. */
  2625. static order;
  2626.  
  2627. /**
  2628. * 依赖模块
  2629. */
  2630. static depends = [];
  2631.  
  2632. /**
  2633. * 附加模块
  2634. */
  2635. static addons = [];
  2636.  
  2637. /**
  2638. * 设置
  2639. */
  2640. settings;
  2641.  
  2642. /**
  2643. * API
  2644. */
  2645. api;
  2646.  
  2647. /**
  2648. * UI
  2649. */
  2650. ui;
  2651.  
  2652. /**
  2653. * 过滤列表
  2654. */
  2655. data = [];
  2656.  
  2657. /**
  2658. * 依赖模块
  2659. */
  2660. depends = {};
  2661.  
  2662. /**
  2663. * 附加模块
  2664. */
  2665. addons = {};
  2666.  
  2667. /**
  2668. * 视图元素
  2669. */
  2670. views = {};
  2671.  
  2672. /**
  2673. * 初始化并绑定设置、API、UI、过滤列表,注册 UI
  2674. * @param {Settings} settings 设置
  2675. * @param {API} api API
  2676. * @param {UI} ui UI
  2677. */
  2678. constructor(settings, api, ui, data) {
  2679. this.settings = settings;
  2680. this.api = api;
  2681. this.ui = ui;
  2682.  
  2683. this.data = data;
  2684.  
  2685. this.init();
  2686. }
  2687.  
  2688. /**
  2689. * 创建实例
  2690. * @param {Settings} settings 设置
  2691. * @param {API} api API
  2692. * @param {UI} ui UI
  2693. * @param {Array} data 过滤列表
  2694. * @returns {Module | null} 成功后返回模块实例
  2695. */
  2696. static create(settings, api, ui, data) {
  2697. // 读取设置里的模块列表
  2698. const modules = settings.modules;
  2699.  
  2700. // 如果不包含自己或依赖的模块,则返回空
  2701. const index = [this, ...this.depends].findIndex(
  2702. (module) => modules.includes(module.name) === false
  2703. );
  2704.  
  2705. if (index >= 0) {
  2706. return null;
  2707. }
  2708.  
  2709. // 创建实例
  2710. const instance = new this(settings, api, ui, data);
  2711.  
  2712. // 返回实例
  2713. return instance;
  2714. }
  2715.  
  2716. /**
  2717. * 判断指定附加模块是否启用
  2718. * @param {typeof Module} module 模块
  2719. */
  2720. hasAddon(module) {
  2721. return Object.hasOwn(this.addons, module.name);
  2722. }
  2723.  
  2724. /**
  2725. * 初始化,创建基础视图和组件
  2726. */
  2727. init() {
  2728. if (this.views.container) {
  2729. this.destroy();
  2730. }
  2731.  
  2732. const { ui } = this;
  2733.  
  2734. const container = ui.createElement("DIV", []);
  2735.  
  2736. this.views = {
  2737. container,
  2738. };
  2739.  
  2740. this.initComponents();
  2741. }
  2742.  
  2743. /**
  2744. * 初始化组件
  2745. */
  2746. initComponents() {}
  2747.  
  2748. /**
  2749. * 销毁
  2750. */
  2751. destroy() {
  2752. Object.values(this.views).forEach((view) => {
  2753. if (view.parentNode) {
  2754. view.parentNode.removeChild(view);
  2755. }
  2756. });
  2757.  
  2758. this.views = {};
  2759. }
  2760.  
  2761. /**
  2762. * 渲染
  2763. * @param {HTMLElement} container 容器
  2764. */
  2765. render(container) {
  2766. container.innerHTML = "";
  2767. container.appendChild(this.views.container);
  2768. }
  2769.  
  2770. /**
  2771. * 过滤
  2772. * @param {*} item 绑定的 nFilter
  2773. * @param {*} result 过滤结果
  2774. */
  2775. async filter(item, result) {}
  2776.  
  2777. /**
  2778. * 通知
  2779. * @param {*} item 绑定的 nFilter
  2780. * @param {*} result 过滤结果
  2781. */
  2782. async notify(item, result) {}
  2783. }
  2784.  
  2785. /**
  2786. * 过滤器
  2787. */
  2788. class Filter {
  2789. /**
  2790. * 设置
  2791. */
  2792. settings;
  2793.  
  2794. /**
  2795. * API
  2796. */
  2797. api;
  2798.  
  2799. /**
  2800. * UI
  2801. */
  2802. ui;
  2803.  
  2804. /**
  2805. * 过滤列表
  2806. */
  2807. data = [];
  2808.  
  2809. /**
  2810. * 模块列表
  2811. */
  2812. modules = {};
  2813.  
  2814. /**
  2815. * 初始化并绑定设置、API、UI
  2816. * @param {Settings} settings 设置
  2817. * @param {API} api API
  2818. * @param {UI} ui UI
  2819. */
  2820. constructor(settings, api, ui) {
  2821. this.settings = settings;
  2822. this.api = api;
  2823. this.ui = ui;
  2824. }
  2825.  
  2826. /**
  2827. * 绑定两个模块的互相关系
  2828. * @param {Module} moduleA 模块A
  2829. * @param {Module} moduleB 模块B
  2830. */
  2831. bindModule(moduleA, moduleB) {
  2832. const nameA = moduleA.constructor.name;
  2833. const nameB = moduleB.constructor.name;
  2834.  
  2835. // A 依赖 B
  2836. if (moduleA.constructor.depends.findIndex((i) => i.name === nameB) >= 0) {
  2837. moduleA.depends[nameB] = moduleB;
  2838. moduleA.init();
  2839. }
  2840.  
  2841. // B 依赖 A
  2842. if (moduleB.constructor.depends.findIndex((i) => i.name === nameA) >= 0) {
  2843. moduleB.depends[nameA] = moduleA;
  2844. moduleB.init();
  2845. }
  2846.  
  2847. // A 附加 B
  2848. if (moduleA.constructor.addons.findIndex((i) => i.name === nameB) >= 0) {
  2849. moduleA.addons[nameB] = moduleB;
  2850. moduleA.init();
  2851. }
  2852.  
  2853. // B 附加 A
  2854. if (moduleB.constructor.addons.findIndex((i) => i.name === nameA) >= 0) {
  2855. moduleB.addons[nameA] = moduleA;
  2856. moduleB.init();
  2857. }
  2858. }
  2859.  
  2860. /**
  2861. * 加载模块
  2862. * @param {typeof Module} module 模块
  2863. */
  2864. initModule(module) {
  2865. // 如果已经加载过则跳过
  2866. if (Object.hasOwn(this.modules, module.name)) {
  2867. return;
  2868. }
  2869.  
  2870. // 创建模块
  2871. const instance = module.create(
  2872. this.settings,
  2873. this.api,
  2874. this.ui,
  2875. this.data
  2876. );
  2877.  
  2878. // 如果创建失败则跳过
  2879. if (instance === null) {
  2880. return;
  2881. }
  2882.  
  2883. // 绑定依赖模块和附加模块
  2884. Object.values(this.modules).forEach((item) => {
  2885. this.bindModule(item, instance);
  2886. });
  2887.  
  2888. // 合并模块
  2889. this.modules[module.name] = instance;
  2890.  
  2891. // 按照顺序重新整理模块
  2892. this.modules = Tools.sortBy(
  2893. Object.values(this.modules),
  2894. (item) => item.constructor.order
  2895. ).reduce(
  2896. (result, item) => ({
  2897. ...result,
  2898. [item.constructor.name]: item,
  2899. }),
  2900. {}
  2901. );
  2902. }
  2903.  
  2904. /**
  2905. * 加载模块列表
  2906. * @param {typeof Module[]} modules 模块列表
  2907. */
  2908. initModules(...modules) {
  2909. // 根据依赖和附加模块决定初始化的顺序
  2910. Tools.sortBy(
  2911. modules,
  2912. (item) => item.depends.length,
  2913. (item) => item.addons.length
  2914. ).forEach((module) => {
  2915. this.initModule(module);
  2916. });
  2917. }
  2918.  
  2919. /**
  2920. * 添加到过滤列表
  2921. * @param {*} item 绑定的 nFilter
  2922. */
  2923. pushData(item) {
  2924. // 清除掉无效数据
  2925. for (let i = 0; i < this.data.length; ) {
  2926. if (document.body.contains(this.data[i].container) === false) {
  2927. this.data.splice(i, 1);
  2928. continue;
  2929. }
  2930.  
  2931. i += 1;
  2932. }
  2933.  
  2934. // 加入过滤列表
  2935. if (this.data.includes(item) === false) {
  2936. this.data.push(item);
  2937. }
  2938. }
  2939.  
  2940. /**
  2941. * 判断指定 UID 是否是自己
  2942. * @param {Number} uid 用户 ID
  2943. */
  2944. isSelf(uid) {
  2945. return unsafeWindow.__CURRENT_UID === uid;
  2946. }
  2947.  
  2948. /**
  2949. * 获取过滤模式
  2950. * @param {*} item 绑定的 nFilter
  2951. */
  2952. async getFilterMode(item) {
  2953. // 获取链接参数
  2954. const params = new URLSearchParams(location.search);
  2955.  
  2956. // 跳过屏蔽(插件自定义)
  2957. if (params.has("nofilter")) {
  2958. return;
  2959. }
  2960.  
  2961. // 收藏
  2962. if (params.has("favor")) {
  2963. return;
  2964. }
  2965.  
  2966. // 只看某人
  2967. if (params.has("authorid")) {
  2968. return;
  2969. }
  2970.  
  2971. // 跳过自己
  2972. if (this.isSelf(item.uid)) {
  2973. return;
  2974. }
  2975.  
  2976. // 声明结果
  2977. const result = {
  2978. mode: -1,
  2979. reason: ``,
  2980. };
  2981.  
  2982. // 根据模块依次过滤
  2983. for (const module of Object.values(this.modules)) {
  2984. await module.filter(item, result);
  2985. }
  2986.  
  2987. // 写入过滤模式和过滤原因
  2988. item.filterMode = this.settings.getNameByMode(result.mode);
  2989. item.reason = result.reason;
  2990.  
  2991. // 通知各模块过滤结果
  2992. for (const module of Object.values(this.modules)) {
  2993. await module.notify(item, result);
  2994. }
  2995.  
  2996. // 继承模式下返回默认过滤模式
  2997. if (item.filterMode === "继承") {
  2998. return this.settings.defaultFilterMode;
  2999. }
  3000.  
  3001. // 返回结果
  3002. return item.filterMode;
  3003. }
  3004.  
  3005. /**
  3006. * 过滤主题
  3007. * @param {*} item 主题内容,见 commonui.topicArg.data
  3008. */
  3009. filterTopic(item) {
  3010. // 绑定事件
  3011. if (item.nFilter === undefined) {
  3012. // 主题 ID
  3013. const tid = item[8];
  3014.  
  3015. // 主题标题
  3016. const title = item[1];
  3017. const subject = title.innerText;
  3018.  
  3019. // 主题作者
  3020. const author = item[2];
  3021. const uid =
  3022. parseInt(author.getAttribute("href").match(/uid=(\S+)/)[1], 10) || 0;
  3023. const username = author.innerText;
  3024.  
  3025. // 主题容器
  3026. const container = title.closest("tr");
  3027.  
  3028. // 过滤函数
  3029. const execute = async () => {
  3030. // 获取过滤模式
  3031. const filterMode = await this.getFilterMode(item.nFilter);
  3032.  
  3033. // 样式处理
  3034. (() => {
  3035. // 还原样式
  3036. // TODO 应该整体采用 className 来实现
  3037. (() => {
  3038. // 标记模式
  3039. title.style.removeProperty("textDecoration");
  3040.  
  3041. // 遮罩模式
  3042. title.classList.remove("filter-mask");
  3043. author.classList.remove("filter-mask");
  3044. })();
  3045.  
  3046. // 样式处理
  3047. (() => {
  3048. // 标记模式下,主题标记会有删除线标识
  3049. if (filterMode === "标记") {
  3050. title.style.textDecoration = "line-through";
  3051. return;
  3052. }
  3053.  
  3054. // 遮罩模式下,主题和作者会有遮罩样式
  3055. if (filterMode === "遮罩") {
  3056. title.classList.add("filter-mask");
  3057. author.classList.add("filter-mask");
  3058. return;
  3059. }
  3060.  
  3061. // 隐藏模式下,容器会被隐藏
  3062. if (filterMode === "隐藏") {
  3063. container.style.display = "none";
  3064. return;
  3065. }
  3066. })();
  3067.  
  3068. // 非隐藏模式下,恢复显示
  3069. if (filterMode !== "隐藏") {
  3070. container.style.removeProperty("display");
  3071. }
  3072. })();
  3073. };
  3074.  
  3075. // 绑定事件
  3076. item.nFilter = {
  3077. tid,
  3078. pid: 0,
  3079. uid,
  3080. username,
  3081. container,
  3082. title,
  3083. author,
  3084. subject,
  3085. action: null,
  3086. tags: null,
  3087. execute,
  3088. };
  3089.  
  3090. // 添加至列表
  3091. this.pushData(item.nFilter);
  3092. }
  3093.  
  3094. // 开始过滤
  3095. item.nFilter.execute();
  3096. }
  3097.  
  3098. /**
  3099. * 过滤回复
  3100. * @param {*} item 回复内容,见 commonui.postArg.data
  3101. */
  3102. filterReply(item) {
  3103. // 绑定事件
  3104. if (item.nFilter === undefined) {
  3105. // 主题 ID
  3106. const tid = item.tid;
  3107.  
  3108. // 回复 ID
  3109. const pid = item.pid;
  3110.  
  3111. // 判断是否是楼层
  3112. const isFloor = typeof item.i === "number";
  3113.  
  3114. // 回复容器
  3115. const container = isFloor
  3116. ? item.uInfoC.closest("tr")
  3117. : item.uInfoC.closest(".comment_c");
  3118.  
  3119. // 回复标题
  3120. const title = item.subjectC;
  3121. const subject = title.innerText;
  3122.  
  3123. // 回复内容
  3124. const content = item.contentC;
  3125. const contentBak = content.innerHTML;
  3126.  
  3127. // 回复作者
  3128. const author =
  3129. container.querySelector(".posterInfoLine") || item.uInfoC;
  3130. const uid = parseInt(item.pAid, 10) || 0;
  3131. const username = author.querySelector(".author").innerText;
  3132. const avatar = author.querySelector(".avatar");
  3133.  
  3134. // 找到用户 ID,将其视为操作按钮
  3135. const action = container.querySelector('[name="uid"]');
  3136.  
  3137. // 创建一个元素,用于展示标记列表
  3138. // 贴条和高赞不显示
  3139. const tags = (() => {
  3140. if (isFloor === false) {
  3141. return null;
  3142. }
  3143.  
  3144. const element = document.createElement("div");
  3145.  
  3146. element.className = "filter-tags";
  3147.  
  3148. author.appendChild(element);
  3149.  
  3150. return element;
  3151. })();
  3152.  
  3153. // 过滤函数
  3154. const execute = async () => {
  3155. // 获取过滤模式
  3156. const filterMode = await this.getFilterMode(item.nFilter);
  3157.  
  3158. // 样式处理
  3159. (() => {
  3160. // 还原样式
  3161. // TODO 应该整体采用 className 来实现
  3162. (() => {
  3163. // 标记模式
  3164. if (avatar) {
  3165. avatar.style.removeProperty("display");
  3166. }
  3167.  
  3168. content.innerHTML = contentBak;
  3169.  
  3170. // 遮罩模式
  3171. const caption = container.parentNode.querySelector("CAPTION");
  3172.  
  3173. if (caption) {
  3174. container.parentNode.removeChild(caption);
  3175. container.style.removeProperty("display");
  3176. }
  3177. })();
  3178.  
  3179. // 样式处理
  3180. (() => {
  3181. // 标记模式下,隐藏头像,采用泥潭的折叠样式
  3182. if (filterMode === "标记") {
  3183. if (avatar) {
  3184. avatar.style.display = "none";
  3185. }
  3186.  
  3187. this.ui.collapse(uid, content, contentBak);
  3188. return;
  3189. }
  3190.  
  3191. // 遮罩模式下,楼层会有遮罩样式
  3192. if (filterMode === "遮罩") {
  3193. const caption = document.createElement("CAPTION");
  3194.  
  3195. if (isFloor) {
  3196. caption.className = "filter-mask filter-mask-block";
  3197. } else {
  3198. caption.className = "filter-mask filter-mask-block left";
  3199. caption.style.width = "47%";
  3200. }
  3201.  
  3202. caption.innerHTML = `<span class="crimson">Troll must die.</span>`;
  3203. caption.onclick = () => {
  3204. const caption = container.parentNode.querySelector("CAPTION");
  3205.  
  3206. if (caption) {
  3207. container.parentNode.removeChild(caption);
  3208. container.style.removeProperty("display");
  3209. }
  3210. };
  3211.  
  3212. container.parentNode.insertBefore(caption, container);
  3213. container.style.display = "none";
  3214. return;
  3215. }
  3216.  
  3217. // 隐藏模式下,容器会被隐藏
  3218. if (filterMode === "隐藏") {
  3219. container.style.display = "none";
  3220. return;
  3221. }
  3222. })();
  3223.  
  3224. // 非隐藏模式下,恢复显示
  3225. // 楼层的遮罩模式下仍需隐藏
  3226. if (["遮罩", "隐藏"].includes(filterMode) === false) {
  3227. container.style.removeProperty("display");
  3228. }
  3229. })();
  3230.  
  3231. // 过滤引用
  3232. this.filterQuote(item);
  3233. };
  3234.  
  3235. // 绑定事件
  3236. item.nFilter = {
  3237. tid,
  3238. pid,
  3239. uid,
  3240. username,
  3241. container,
  3242. title,
  3243. author,
  3244. subject,
  3245. content: content.innerText,
  3246. action,
  3247. tags,
  3248. execute,
  3249. };
  3250.  
  3251. // 添加至列表
  3252. this.pushData(item.nFilter);
  3253. }
  3254.  
  3255. // 开始过滤
  3256. item.nFilter.execute();
  3257. }
  3258.  
  3259. /**
  3260. * 过滤引用
  3261. * @param {*} item 回复内容,见 commonui.postArg.data
  3262. */
  3263. filterQuote(item) {
  3264. // 未绑定事件,直接跳过
  3265. if (item.nFilter === undefined) {
  3266. return;
  3267. }
  3268.  
  3269. // 回复内容
  3270. const content = item.contentC;
  3271.  
  3272. // 找到所有引用
  3273. const quotes = content.querySelectorAll(".quote");
  3274.  
  3275. // 处理引用
  3276. [...quotes].map(async (quote) => {
  3277. const uid = (() => {
  3278. const ele = quote.querySelector("a[href^='/nuke.php']");
  3279.  
  3280. if (ele) {
  3281. const res = ele.getAttribute("href").match(/uid=(\S+)/);
  3282.  
  3283. if (res) {
  3284. return parseInt(res[1], 10);
  3285. }
  3286. }
  3287.  
  3288. return 0;
  3289. })();
  3290.  
  3291. const { tid, pid } = (() => {
  3292. const ele = quote.querySelector("[title='快速浏览这个帖子']");
  3293.  
  3294. if (ele) {
  3295. const res = ele
  3296. .getAttribute("onclick")
  3297. .match(/fastViewPost(.+,(\S+),(\S+|undefined),.+)/);
  3298.  
  3299. if (res) {
  3300. return {
  3301. tid: parseInt(res[2], 10),
  3302. pid: parseInt(res[3], 10) || 0,
  3303. };
  3304. }
  3305. }
  3306.  
  3307. return {};
  3308. })();
  3309.  
  3310. // 临时的 nFilter
  3311. const nFilter = {
  3312. uid,
  3313. tid,
  3314. pid,
  3315. subject: "",
  3316. content: quote.innerText,
  3317. action: null,
  3318. tags: null,
  3319. };
  3320.  
  3321. // 获取过滤模式
  3322. const filterMode = await this.getFilterMode(nFilter);
  3323.  
  3324. (() => {
  3325. if (filterMode === "标记") {
  3326. this.ui.collapse(uid, quote, quote.innerHTML);
  3327. return;
  3328. }
  3329.  
  3330. if (filterMode === "遮罩") {
  3331. const source = document.createElement("DIV");
  3332.  
  3333. source.innerHTML = quote.innerHTML;
  3334. source.style.display = "none";
  3335.  
  3336. const caption = document.createElement("CAPTION");
  3337.  
  3338. caption.className = "filter-mask filter-mask-block";
  3339.  
  3340. caption.innerHTML = `<span class="crimson">Troll must die.</span>`;
  3341. caption.onclick = () => {
  3342. quote.removeChild(caption);
  3343.  
  3344. source.style.display = "";
  3345. };
  3346.  
  3347. quote.innerHTML = "";
  3348. quote.appendChild(source);
  3349. quote.appendChild(caption);
  3350. return;
  3351. }
  3352.  
  3353. if (filterMode === "隐藏") {
  3354. quote.innerHTML = "";
  3355. return;
  3356. }
  3357. })();
  3358.  
  3359. // 绑定引用
  3360. item.nFilter.quotes = item.nFilter.quotes || {};
  3361. item.nFilter.quotes[uid] = nFilter.filterMode;
  3362. });
  3363. }
  3364. }
  3365.  
  3366. /**
  3367. * 列表模块
  3368. */
  3369. class ListModule extends Module {
  3370. /**
  3371. * 模块名称
  3372. */
  3373. static name = "list";
  3374.  
  3375. /**
  3376. * 模块标签
  3377. */
  3378. static label = "列表";
  3379.  
  3380. /**
  3381. * 顺序
  3382. */
  3383. static order = 10;
  3384.  
  3385. /**
  3386. * 表格列
  3387. * @returns {Array} 表格列集合
  3388. */
  3389. columns() {
  3390. return [
  3391. { label: "内容", ellipsis: true },
  3392. { label: "过滤模式", center: true, width: 1 },
  3393. { label: "原因", width: 1 },
  3394. ];
  3395. }
  3396.  
  3397. /**
  3398. * 表格项
  3399. * @param {*} item 绑定的 nFilter
  3400. * @returns {Array} 表格项集合
  3401. */
  3402. column(item) {
  3403. const { ui } = this;
  3404. const { tid, pid, filterMode, reason } = item;
  3405.  
  3406. // 移除 BR 标签
  3407. item.content = (item.content || "").replace(/<br>/g, "");
  3408.  
  3409. // 主题
  3410. const subject = (() => {
  3411. if (tid) {
  3412. // 如果有 TID 但没有标题,是引用,采用内容逻辑
  3413. if (item.subject.length === 0) {
  3414. return ui.createElement("A", item.content, {
  3415. href: `/read.php?tid=${tid}&nofilter`,
  3416. });
  3417. }
  3418.  
  3419. return ui.createElement("A", item.subject, {
  3420. href: `/read.php?tid=${tid}&nofilter`,
  3421. title: item.content,
  3422. className: "b nobr",
  3423. });
  3424. }
  3425.  
  3426. return item.subject;
  3427. })();
  3428.  
  3429. // 内容
  3430. const content = (() => {
  3431. if (subject) {
  3432. return subject;
  3433. }
  3434.  
  3435. if (pid) {
  3436. return ui.createElement("A", item.content, {
  3437. href: `/read.php?pid=${pid}&nofilter`,
  3438. });
  3439. }
  3440.  
  3441. return item.content;
  3442. })();
  3443.  
  3444. return [content, filterMode, reason];
  3445. }
  3446.  
  3447. /**
  3448. * 初始化组件
  3449. */
  3450. initComponents() {
  3451. super.initComponents();
  3452.  
  3453. const { tabs, content } = this.ui.views;
  3454.  
  3455. const table = this.ui.createTable(this.columns());
  3456.  
  3457. const tab = this.ui.createTab(
  3458. tabs,
  3459. this.constructor.label,
  3460. this.constructor.order,
  3461. {
  3462. onclick: () => {
  3463. this.render(content);
  3464. },
  3465. }
  3466. );
  3467.  
  3468. Object.assign(this.views, {
  3469. tab,
  3470. table,
  3471. });
  3472.  
  3473. this.views.container.appendChild(table);
  3474. }
  3475.  
  3476. /**
  3477. * 渲染
  3478. * @param {HTMLElement} container 容器
  3479. */
  3480. render(container) {
  3481. super.render(container);
  3482.  
  3483. const { table } = this.views;
  3484.  
  3485. if (table) {
  3486. const { add, clear } = table;
  3487.  
  3488. clear();
  3489.  
  3490. const list = this.data.filter((item) => {
  3491. return (item.filterMode || "显示") !== "显示";
  3492. });
  3493.  
  3494. Object.values(list).forEach((item) => {
  3495. const column = this.column(item);
  3496.  
  3497. add(...column);
  3498. });
  3499. }
  3500. }
  3501.  
  3502. /**
  3503. * 通知
  3504. * @param {*} item 绑定的 nFilter
  3505. */
  3506. async notify() {
  3507. // 获取过滤后的数量
  3508. const count = this.data.filter((item) => {
  3509. return (item.filterMode || "显示") !== "显示";
  3510. }).length;
  3511.  
  3512. // 更新菜单文字
  3513. const { ui } = this;
  3514. const { menu } = ui;
  3515.  
  3516. if (menu === null) {
  3517. return;
  3518. }
  3519.  
  3520. if (count) {
  3521. menu.innerHTML = `${ui.constructor.label} <span class="small_colored_text_btn stxt block_txt_c0 vertmod">${count}</span>`;
  3522. } else {
  3523. menu.innerHTML = `${ui.constructor.label}`;
  3524. }
  3525.  
  3526. // 重新渲染
  3527. // TODO 应该给 table 增加一个判重的逻辑,这样只需要更新过滤后的内容即可
  3528. const { tab } = this.views;
  3529.  
  3530. if (tab.querySelector("A").className === "nobr") {
  3531. this.render(ui.views.content);
  3532. }
  3533. }
  3534. }
  3535.  
  3536. /**
  3537. * 用户模块
  3538. */
  3539. class UserModule extends Module {
  3540. /**
  3541. * 模块名称
  3542. */
  3543. static name = "user";
  3544.  
  3545. /**
  3546. * 模块标签
  3547. */
  3548. static label = "用户";
  3549.  
  3550. /**
  3551. * 顺序
  3552. */
  3553. static order = 20;
  3554.  
  3555. /**
  3556. * 获取列表
  3557. */
  3558. get list() {
  3559. return this.settings.users;
  3560. }
  3561.  
  3562. /**
  3563. * 获取用户
  3564. * @param {Number} uid 用户 ID
  3565. */
  3566. get(uid) {
  3567. // 获取列表
  3568. const list = this.list;
  3569.  
  3570. // 如果存在,则返回信息
  3571. if (list[uid]) {
  3572. return list[uid];
  3573. }
  3574.  
  3575. return null;
  3576. }
  3577.  
  3578. /**
  3579. * 添加用户
  3580. * @param {Number} uid 用户 ID
  3581. */
  3582. add(uid, values) {
  3583. // 获取列表
  3584. const list = this.list;
  3585.  
  3586. // 如果已存在,则返回信息
  3587. if (list[uid]) {
  3588. return list[uid];
  3589. }
  3590.  
  3591. // 写入用户信息
  3592. list[uid] = values;
  3593.  
  3594. // 保存数据
  3595. this.settings.users = list;
  3596.  
  3597. // 重新过滤
  3598. this.reFilter(uid);
  3599.  
  3600. // 返回添加的用户
  3601. return values;
  3602. }
  3603.  
  3604. /**
  3605. * 编辑用户
  3606. * @param {Number} uid 用户 ID
  3607. * @param {*} values 用户信息
  3608. */
  3609. update(uid, values) {
  3610. // 获取列表
  3611. const list = this.list;
  3612.  
  3613. // 如果不存在则跳过
  3614. if (Object.hasOwn(list, uid) === false) {
  3615. return null;
  3616. }
  3617.  
  3618. // 获取用户
  3619. const entity = list[uid];
  3620.  
  3621. // 更新用户
  3622. Object.assign(entity, values);
  3623.  
  3624. // 保存数据
  3625. this.settings.users = list;
  3626.  
  3627. // 重新过滤
  3628. this.reFilter(uid);
  3629.  
  3630. // 返回编辑的用户
  3631. return entity;
  3632. }
  3633.  
  3634. /**
  3635. * 删除用户
  3636. * @param {Number} uid 用户 ID
  3637. * @returns {Object | null} 删除的用户
  3638. */
  3639. remove(uid) {
  3640. // 获取列表
  3641. const list = this.list;
  3642.  
  3643. // 如果不存在则跳过
  3644. if (Object.hasOwn(list, uid) === false) {
  3645. return null;
  3646. }
  3647.  
  3648. // 获取用户
  3649. const entity = list[uid];
  3650.  
  3651. // 删除用户
  3652. delete list[uid];
  3653.  
  3654. // 保存数据
  3655. this.settings.users = list;
  3656.  
  3657. // 重新过滤
  3658. this.reFilter(uid);
  3659.  
  3660. // 返回删除的用户
  3661. return entity;
  3662. }
  3663.  
  3664. /**
  3665. * 格式化
  3666. * @param {Number} uid 用户 ID
  3667. * @param {String | undefined} name 用户名称
  3668. */
  3669. format(uid, name) {
  3670. if (uid <= 0) {
  3671. return null;
  3672. }
  3673.  
  3674. const { ui } = this;
  3675.  
  3676. const user = this.get(uid);
  3677.  
  3678. if (user) {
  3679. name = user.name;
  3680. }
  3681.  
  3682. const username = name ? "@" + name : "#" + uid;
  3683.  
  3684. return ui.createElement("A", `[${username}]`, {
  3685. className: "b nobr",
  3686. href: `/nuke.php?func=ucp&uid=${uid}`,
  3687. });
  3688. }
  3689.  
  3690. /**
  3691. * 表格列
  3692. * @returns {Array} 表格列集合
  3693. */
  3694. columns() {
  3695. return [
  3696. { label: "昵称" },
  3697. { label: "过滤模式", center: true, width: 1 },
  3698. { label: "操作", width: 1 },
  3699. ];
  3700. }
  3701.  
  3702. /**
  3703. * 表格项
  3704. * @param {*} item 用户信息
  3705. * @returns {Array} 表格项集合
  3706. */
  3707. column(item) {
  3708. const { ui } = this;
  3709. const { table } = this.views;
  3710. const { id, name, filterMode } = item;
  3711.  
  3712. // 昵称
  3713. const user = this.format(id, name);
  3714.  
  3715. // 切换过滤模式
  3716. const switchMode = ui.createButton(
  3717. filterMode || this.settings.filterModes[0],
  3718. () => {
  3719. const newMode = this.settings.switchModeByName(switchMode.innerText);
  3720.  
  3721. this.update(id, {
  3722. filterMode: newMode,
  3723. });
  3724.  
  3725. switchMode.innerText = newMode;
  3726. }
  3727. );
  3728.  
  3729. // 操作
  3730. const buttons = (() => {
  3731. const remove = ui.createButton("删除", (e) => {
  3732. ui.confirm().then(() => {
  3733. this.remove(id);
  3734.  
  3735. table.remove(e);
  3736. });
  3737. });
  3738.  
  3739. return ui.createButtonGroup(remove);
  3740. })();
  3741.  
  3742. return [user, switchMode, buttons];
  3743. }
  3744.  
  3745. /**
  3746. * 初始化组件
  3747. */
  3748. initComponents() {
  3749. super.initComponents();
  3750.  
  3751. const { ui } = this;
  3752. const { tabs, content, settings } = ui.views;
  3753. const { add } = settings;
  3754.  
  3755. const table = ui.createTable(this.columns());
  3756.  
  3757. const tab = ui.createTab(
  3758. tabs,
  3759. this.constructor.label,
  3760. this.constructor.order,
  3761. {
  3762. onclick: () => {
  3763. this.render(content);
  3764. },
  3765. }
  3766. );
  3767.  
  3768. Object.assign(this.views, {
  3769. tab,
  3770. table,
  3771. });
  3772.  
  3773. this.views.container.appendChild(table);
  3774.  
  3775. // 删除非激活中的用户
  3776. {
  3777. const list = ui.createElement("DIV", [], {
  3778. style: "white-space: normal;",
  3779. });
  3780.  
  3781. const button = ui.createButton("删除非激活中的用户", () => {
  3782. ui.confirm().then(() => {
  3783. list.innerHTML = "";
  3784.  
  3785. const users = Object.values(this.list);
  3786.  
  3787. const waitingQueue = users.map(
  3788. ({ id }) =>
  3789. () =>
  3790. this.api.getUserInfo(id).then(({ bit }) => {
  3791. const activeInfo = commonui.activeInfo(0, 0, bit);
  3792. const activeType = activeInfo[1];
  3793.  
  3794. if (["ACTIVED", "LINKED"].includes(activeType)) {
  3795. return;
  3796. }
  3797.  
  3798. list.append(this.format(id));
  3799.  
  3800. this.remove(id);
  3801. })
  3802. );
  3803.  
  3804. const queueLength = waitingQueue.length;
  3805.  
  3806. const execute = () => {
  3807. if (waitingQueue.length) {
  3808. const next = waitingQueue.shift();
  3809.  
  3810. button.disabled = true;
  3811. button.innerHTML = `删除非激活中的用户 (${
  3812. queueLength - waitingQueue.length
  3813. }/${queueLength})`;
  3814.  
  3815. next().finally(execute);
  3816. return;
  3817. }
  3818.  
  3819. button.disabled = false;
  3820. };
  3821.  
  3822. execute();
  3823. });
  3824. });
  3825.  
  3826. const element = ui.createElement("DIV", [button, list]);
  3827.  
  3828. add(this.constructor.order + 0, element);
  3829. }
  3830. }
  3831.  
  3832. /**
  3833. * 渲染
  3834. * @param {HTMLElement} container 容器
  3835. */
  3836. render(container) {
  3837. super.render(container);
  3838.  
  3839. const { table } = this.views;
  3840.  
  3841. if (table) {
  3842. const { add, clear } = table;
  3843.  
  3844. clear();
  3845.  
  3846. Object.values(this.list).forEach((item) => {
  3847. const column = this.column(item);
  3848.  
  3849. add(...column);
  3850. });
  3851. }
  3852. }
  3853.  
  3854. /**
  3855. * 渲染详情
  3856. * @param {Number} uid 用户 ID
  3857. * @param {String | undefined} name 用户名称
  3858. * @param {Function} callback 回调函数
  3859. */
  3860. renderDetails(uid, name, callback = () => {}) {
  3861. const { ui, settings } = this;
  3862.  
  3863. // 只允许同时存在一个详情页
  3864. if (this.views.details) {
  3865. if (this.views.details.parentNode) {
  3866. this.views.details.parentNode.removeChild(this.views.details);
  3867. }
  3868. }
  3869.  
  3870. // 获取用户信息
  3871. const user = this.get(uid);
  3872.  
  3873. if (user) {
  3874. name = user.name;
  3875. }
  3876.  
  3877. const title =
  3878. (user ? "编辑" : "添加") + `用户 - ${name ? name : "#" + uid}`;
  3879.  
  3880. const filterMode = user ? user.filterMode : settings.filterModes[0];
  3881.  
  3882. const switchMode = ui.createButton(filterMode, () => {
  3883. const newMode = settings.switchModeByName(switchMode.innerText);
  3884.  
  3885. switchMode.innerText = newMode;
  3886. });
  3887.  
  3888. const buttons = ui.createElement(
  3889. "DIV",
  3890. (() => {
  3891. const remove = user
  3892. ? ui.createButton("删除", () => {
  3893. ui.confirm().then(() => {
  3894. this.remove(uid);
  3895.  
  3896. this.views.details._.hide();
  3897.  
  3898. callback("REMOVE");
  3899. });
  3900. })
  3901. : null;
  3902.  
  3903. const save = ui.createButton("保存", () => {
  3904. if (user === null) {
  3905. const entity = this.add(uid, {
  3906. id: uid,
  3907. name,
  3908. tags: [],
  3909. filterMode: switchMode.innerText,
  3910. });
  3911.  
  3912. this.views.details._.hide();
  3913.  
  3914. callback("ADD", entity);
  3915. } else {
  3916. const entity = this.update(uid, {
  3917. name,
  3918. filterMode: switchMode.innerText,
  3919. });
  3920.  
  3921. this.views.details._.hide();
  3922.  
  3923. callback("UPDATE", entity);
  3924. }
  3925. });
  3926.  
  3927. return ui.createButtonGroup(remove, save);
  3928. })(),
  3929. {
  3930. className: "right_",
  3931. }
  3932. );
  3933.  
  3934. const actions = ui.createElement(
  3935. "DIV",
  3936. [ui.createElement("SPAN", "过滤模式:"), switchMode, buttons],
  3937. {
  3938. style: "margin-top: 10px;",
  3939. }
  3940. );
  3941.  
  3942. const tips = ui.createElement("DIV", TIPS.filterMode, {
  3943. className: "silver",
  3944. style: "margin-top: 10px;",
  3945. });
  3946.  
  3947. const content = ui.createElement("DIV", [actions, tips], {
  3948. style: "width: 80vw",
  3949. });
  3950.  
  3951. // 创建弹出框
  3952. this.views.details = ui.createDialog(null, title, content);
  3953. }
  3954.  
  3955. /**
  3956. * 过滤
  3957. * @param {*} item 绑定的 nFilter
  3958. * @param {*} result 过滤结果
  3959. */
  3960. async filter(item, result) {
  3961. // 获取用户信息
  3962. const user = this.get(item.uid);
  3963.  
  3964. // 没有则跳过
  3965. if (user === null) {
  3966. return;
  3967. }
  3968.  
  3969. // 获取用户过滤模式
  3970. const mode = this.settings.getModeByName(user.filterMode);
  3971.  
  3972. // 不高于当前过滤模式则跳过
  3973. if (mode <= result.mode) {
  3974. return;
  3975. }
  3976.  
  3977. // 更新过滤模式和原因
  3978. result.mode = mode;
  3979. result.reason = `用户模式: ${user.filterMode}`;
  3980. }
  3981.  
  3982. /**
  3983. * 通知
  3984. * @param {*} item 绑定的 nFilter
  3985. */
  3986. async notify(item) {
  3987. const { uid, username, action } = item;
  3988.  
  3989. // 如果没有 action 组件则跳过
  3990. if (action === null) {
  3991. return;
  3992. }
  3993.  
  3994. // 如果是匿名,隐藏组件
  3995. if (uid <= 0) {
  3996. action.style.display = "none";
  3997. return;
  3998. }
  3999.  
  4000. // 获取当前用户
  4001. const user = this.get(uid);
  4002.  
  4003. // 修改操作按钮文字
  4004. action.innerText = "屏蔽";
  4005.  
  4006. // 修改操作按钮颜色
  4007. if (user) {
  4008. action.style.background = "#CB4042";
  4009. } else {
  4010. action.style.background = "#AAA";
  4011. }
  4012.  
  4013. // 绑定事件
  4014. action.onclick = () => {
  4015. this.renderDetails(uid, username);
  4016. };
  4017. }
  4018.  
  4019. /**
  4020. * 重新过滤
  4021. * @param {Number} uid 用户 ID
  4022. */
  4023. reFilter(uid) {
  4024. this.data.forEach((item) => {
  4025. // 如果用户 ID 一致,则重新过滤
  4026. if (item.uid === uid) {
  4027. item.execute();
  4028. return;
  4029. }
  4030.  
  4031. // 如果有引用,也重新过滤
  4032. if (Object.hasOwn(item.quotes || {}, uid)) {
  4033. item.execute();
  4034. return;
  4035. }
  4036. });
  4037. }
  4038. }
  4039.  
  4040. /**
  4041. * 标记模块
  4042. */
  4043. class TagModule extends Module {
  4044. /**
  4045. * 模块名称
  4046. */
  4047. static name = "tag";
  4048.  
  4049. /**
  4050. * 模块标签
  4051. */
  4052. static label = "标记";
  4053.  
  4054. /**
  4055. * 顺序
  4056. */
  4057. static order = 30;
  4058.  
  4059. /**
  4060. * 依赖模块
  4061. */
  4062. static depends = [UserModule];
  4063.  
  4064. /**
  4065. * 依赖的用户模块
  4066. * @returns {UserModule} 用户模块
  4067. */
  4068. get userModule() {
  4069. return this.depends[UserModule.name];
  4070. }
  4071.  
  4072. /**
  4073. * 获取列表
  4074. */
  4075. get list() {
  4076. return this.settings.tags;
  4077. }
  4078.  
  4079. /**
  4080. * 获取标记
  4081. * @param {Number} id 标记 ID
  4082. * @param {String} name 标记名称
  4083. */
  4084. get({ id, name }) {
  4085. // 获取列表
  4086. const list = this.list;
  4087.  
  4088. // 通过 ID 获取标记
  4089. if (list[id]) {
  4090. return list[id];
  4091. }
  4092.  
  4093. // 通过名称获取标记
  4094. if (name) {
  4095. const tag = Object.values(list).find((item) => item.name === name);
  4096.  
  4097. if (tag) {
  4098. return tag;
  4099. }
  4100. }
  4101.  
  4102. return null;
  4103. }
  4104.  
  4105. /**
  4106. * 添加标记
  4107. * @param {String} name 标记名称
  4108. */
  4109. add(name) {
  4110. // 获取对应的标记
  4111. const tag = this.get({ name });
  4112.  
  4113. // 如果标记已存在,则返回标记信息,否则增加标记
  4114. if (tag) {
  4115. return tag;
  4116. }
  4117.  
  4118. // 获取列表
  4119. const list = this.list;
  4120.  
  4121. // ID 为最大值 + 1
  4122. const id = Math.max(...Object.keys(list), 0) + 1;
  4123.  
  4124. // 标记的颜色
  4125. const color = Tools.generateColor(name);
  4126.  
  4127. // 写入标记信息
  4128. list[id] = {
  4129. id,
  4130. name,
  4131. color,
  4132. filterMode: this.settings.filterModes[0],
  4133. };
  4134.  
  4135. // 保存数据
  4136. this.settings.tags = list;
  4137.  
  4138. // 返回添加的标记
  4139. return list[id];
  4140. }
  4141.  
  4142. /**
  4143. * 编辑标记
  4144. * @param {Number} id 标记 ID
  4145. * @param {*} values 标记信息
  4146. */
  4147. update(id, values) {
  4148. // 获取列表
  4149. const list = this.list;
  4150.  
  4151. // 如果不存在则跳过
  4152. if (Object.hasOwn(list, id) === false) {
  4153. return null;
  4154. }
  4155.  
  4156. // 获取标记
  4157. const entity = list[id];
  4158.  
  4159. // 获取相关的用户
  4160. const users = Object.values(this.userModule.list).filter((user) =>
  4161. user.tags.includes(id)
  4162. );
  4163.  
  4164. // 更新标记
  4165. Object.assign(entity, values);
  4166.  
  4167. // 保存数据
  4168. this.settings.tags = list;
  4169.  
  4170. // 重新过滤
  4171. this.reFilter(users);
  4172. }
  4173.  
  4174. /**
  4175. * 删除标记
  4176. * @param {Number} id 标记 ID
  4177. */
  4178. remove(id) {
  4179. // 获取列表
  4180. const list = this.list;
  4181.  
  4182. // 如果不存在则跳过
  4183. if (Object.hasOwn(list, id) === false) {
  4184. return null;
  4185. }
  4186.  
  4187. // 获取标记
  4188. const entity = list[id];
  4189.  
  4190. // 获取相关的用户
  4191. const users = Object.values(this.userModule.list).filter((user) =>
  4192. user.tags.includes(id)
  4193. );
  4194.  
  4195. // 删除标记
  4196. delete list[id];
  4197.  
  4198. // 删除相关的用户标记
  4199. users.forEach((user) => {
  4200. const index = user.tags.findIndex((item) => item === id);
  4201.  
  4202. if (index >= 0) {
  4203. user.tags.splice(index, 1);
  4204. }
  4205. });
  4206.  
  4207. // 保存数据
  4208. this.settings.tags = list;
  4209.  
  4210. // 重新过滤
  4211. this.reFilter(users);
  4212.  
  4213. // 返回删除的标记
  4214. return entity;
  4215. }
  4216.  
  4217. /**
  4218. * 格式化
  4219. * @param {Number} id 标记 ID
  4220. * @param {String | undefined} name 标记名称
  4221. * @param {String | undefined} name 标记颜色
  4222. */
  4223. format(id, name, color) {
  4224. const { ui } = this;
  4225.  
  4226. if (id >= 0) {
  4227. const tag = this.get({ id });
  4228.  
  4229. if (tag) {
  4230. name = tag.name;
  4231. color = tag.color;
  4232. }
  4233. }
  4234.  
  4235. if (name && color) {
  4236. return ui.createElement("B", name, {
  4237. className: "block_txt nobr",
  4238. style: `background: ${color}; color: #FFF; margin: 0.1em 0.2em;`,
  4239. });
  4240. }
  4241.  
  4242. return "";
  4243. }
  4244.  
  4245. /**
  4246. * 表格列
  4247. * @returns {Array} 表格列集合
  4248. */
  4249. columns() {
  4250. return [
  4251. { label: "标记", width: 1 },
  4252. { label: "列表" },
  4253. { label: "过滤模式", width: 1 },
  4254. { label: "操作", width: 1 },
  4255. ];
  4256. }
  4257.  
  4258. /**
  4259. * 表格项
  4260. * @param {*} item 标记信息
  4261. * @returns {Array} 表格项集合
  4262. */
  4263. column(item) {
  4264. const { ui } = this;
  4265. const { table } = this.views;
  4266. const { id, filterMode } = item;
  4267.  
  4268. // 标记
  4269. const tag = this.format(id);
  4270.  
  4271. // 用户列表
  4272. const list = Object.values(this.userModule.list)
  4273. .filter(({ tags }) => tags.includes(id))
  4274. .map(({ id }) => this.userModule.format(id));
  4275.  
  4276. const group = ui.createElement("DIV", list, {
  4277. style: "white-space: normal; display: none;",
  4278. });
  4279.  
  4280. const switchButton = ui.createButton(list.length.toString(), () => {
  4281. if (group.style.display === "none") {
  4282. group.style.removeProperty("display");
  4283. } else {
  4284. group.style.display = "none";
  4285. }
  4286. });
  4287.  
  4288. // 切换过滤模式
  4289. const switchMode = ui.createButton(
  4290. filterMode || this.settings.filterModes[0],
  4291. () => {
  4292. const newMode = this.settings.switchModeByName(switchMode.innerText);
  4293.  
  4294. this.update(id, {
  4295. filterMode: newMode,
  4296. });
  4297.  
  4298. switchMode.innerText = newMode;
  4299. }
  4300. );
  4301.  
  4302. // 操作
  4303. const buttons = (() => {
  4304. const remove = ui.createButton("删除", (e) => {
  4305. ui.confirm().then(() => {
  4306. this.remove(id);
  4307.  
  4308. table.remove(e);
  4309. });
  4310. });
  4311.  
  4312. return ui.createButtonGroup(remove);
  4313. })();
  4314.  
  4315. return [tag, [switchButton, group], switchMode, buttons];
  4316. }
  4317.  
  4318. /**
  4319. * 初始化组件
  4320. */
  4321. initComponents() {
  4322. super.initComponents();
  4323.  
  4324. const { ui } = this;
  4325. const { tabs, content, settings } = ui.views;
  4326. const { add } = settings;
  4327.  
  4328. const table = ui.createTable(this.columns());
  4329.  
  4330. const tab = ui.createTab(
  4331. tabs,
  4332. this.constructor.label,
  4333. this.constructor.order,
  4334. {
  4335. onclick: () => {
  4336. this.render(content);
  4337. },
  4338. }
  4339. );
  4340.  
  4341. Object.assign(this.views, {
  4342. tab,
  4343. table,
  4344. });
  4345.  
  4346. this.views.container.appendChild(table);
  4347.  
  4348. // 删除没有标记的用户
  4349. {
  4350. const button = ui.createButton("删除没有标记的用户", () => {
  4351. ui.confirm().then(() => {
  4352. const users = Object.values(this.userModule.list);
  4353.  
  4354. users.forEach(({ id, tags }) => {
  4355. if (tags.length > 0) {
  4356. return;
  4357. }
  4358.  
  4359. this.userModule.remove(id);
  4360. });
  4361. });
  4362. });
  4363.  
  4364. const element = ui.createElement("DIV", button);
  4365.  
  4366. add(this.constructor.order + 0, element);
  4367. }
  4368.  
  4369. // 删除没有用户的标记
  4370. {
  4371. const button = ui.createButton("删除没有用户的标记", () => {
  4372. ui.confirm().then(() => {
  4373. const items = Object.values(this.list);
  4374. const users = Object.values(this.userModule.list);
  4375.  
  4376. items.forEach(({ id }) => {
  4377. if (users.find(({ tags }) => tags.includes(id))) {
  4378. return;
  4379. }
  4380.  
  4381. this.remove(id);
  4382. });
  4383. });
  4384. });
  4385.  
  4386. const element = ui.createElement("DIV", button);
  4387.  
  4388. add(this.constructor.order + 1, element);
  4389. }
  4390. }
  4391.  
  4392. /**
  4393. * 渲染
  4394. * @param {HTMLElement} container 容器
  4395. */
  4396. render(container) {
  4397. super.render(container);
  4398.  
  4399. const { table } = this.views;
  4400.  
  4401. if (table) {
  4402. const { add, clear } = table;
  4403.  
  4404. clear();
  4405.  
  4406. Object.values(this.list).forEach((item) => {
  4407. const column = this.column(item);
  4408.  
  4409. add(...column);
  4410. });
  4411. }
  4412. }
  4413.  
  4414. /**
  4415. * 过滤
  4416. * @param {*} item 绑定的 nFilter
  4417. * @param {*} result 过滤结果
  4418. */
  4419. async filter(item, result) {
  4420. // 获取用户信息
  4421. const user = this.userModule.get(item.uid);
  4422.  
  4423. // 没有则跳过
  4424. if (user === null) {
  4425. return;
  4426. }
  4427.  
  4428. // 获取用户标记
  4429. const tags = user.tags;
  4430.  
  4431. // 取最高的过滤模式
  4432. // 低于当前的过滤模式则跳过
  4433. let max = result.mode;
  4434. let tag = null;
  4435.  
  4436. for (const id of tags) {
  4437. const entity = this.get({ id });
  4438.  
  4439. if (entity === null) {
  4440. continue;
  4441. }
  4442.  
  4443. // 获取过滤模式
  4444. const mode = this.settings.getModeByName(entity.filterMode);
  4445.  
  4446. if (mode <= max) {
  4447. continue;
  4448. }
  4449.  
  4450. max = mode;
  4451. tag = entity;
  4452. }
  4453.  
  4454. // 没有匹配的则跳过
  4455. if (tag === null) {
  4456. return;
  4457. }
  4458.  
  4459. // 更新过滤模式和原因
  4460. result.mode = max;
  4461. result.reason = `标记: ${tag.name}`;
  4462. }
  4463.  
  4464. /**
  4465. * 通知
  4466. * @param {*} item 绑定的 nFilter
  4467. */
  4468. async notify(item) {
  4469. const { uid, tags } = item;
  4470.  
  4471. // 如果没有 tags 组件则跳过
  4472. if (tags === null) {
  4473. return;
  4474. }
  4475.  
  4476. // 如果是匿名,隐藏组件
  4477. if (uid <= 0) {
  4478. tags.style.display = "none";
  4479. return;
  4480. }
  4481.  
  4482. // 删除旧标记
  4483. [...tags.querySelectorAll("[tid]")].forEach((item) => {
  4484. tags.removeChild(item);
  4485. });
  4486.  
  4487. // 获取当前用户
  4488. const user = this.userModule.get(uid);
  4489.  
  4490. // 如果没有用户,则跳过
  4491. if (user === null) {
  4492. return;
  4493. }
  4494.  
  4495. // 格式化标记
  4496. const items = user.tags.map((id) => {
  4497. const item = this.format(id);
  4498.  
  4499. if (item) {
  4500. item.setAttribute("tid", id);
  4501. }
  4502.  
  4503. return item;
  4504. });
  4505.  
  4506. // 加入组件
  4507. items.forEach((item) => {
  4508. if (item) {
  4509. tags.appendChild(item);
  4510. }
  4511. });
  4512. }
  4513.  
  4514. /**
  4515. * 重新过滤
  4516. * @param {Array} users 用户集合
  4517. */
  4518. reFilter(users) {
  4519. users.forEach((user) => {
  4520. this.userModule.reFilter(user.id);
  4521. });
  4522. }
  4523. }
  4524.  
  4525. /**
  4526. * 关键字模块
  4527. */
  4528. class KeywordModule extends Module {
  4529. /**
  4530. * 模块名称
  4531. */
  4532. static name = "keyword";
  4533.  
  4534. /**
  4535. * 模块标签
  4536. */
  4537. static label = "关键字";
  4538.  
  4539. /**
  4540. * 顺序
  4541. */
  4542. static order = 40;
  4543.  
  4544. /**
  4545. * 获取列表
  4546. */
  4547. get list() {
  4548. return this.settings.keywords;
  4549. }
  4550.  
  4551. /**
  4552. * 获取关键字
  4553. * @param {Number} id 关键字 ID
  4554. */
  4555. get(id) {
  4556. // 获取列表
  4557. const list = this.list;
  4558.  
  4559. // 如果存在,则返回信息
  4560. if (list[id]) {
  4561. return list[id];
  4562. }
  4563.  
  4564. return null;
  4565. }
  4566.  
  4567. /**
  4568. * 添加关键字
  4569. * @param {String} keyword 关键字
  4570. * @param {String} filterMode 过滤模式
  4571. * @param {Number} filterLevel 过滤等级: 0 - 仅过滤标题; 1 - 过滤标题和内容
  4572. */
  4573. add(keyword, filterMode, filterLevel) {
  4574. // 获取列表
  4575. const list = this.list;
  4576.  
  4577. // ID 为最大值 + 1
  4578. const id = Math.max(...Object.keys(list), 0) + 1;
  4579.  
  4580. // 写入关键字信息
  4581. list[id] = {
  4582. id,
  4583. keyword,
  4584. filterMode,
  4585. filterLevel,
  4586. };
  4587.  
  4588. // 保存数据
  4589. this.settings.keywords = list;
  4590.  
  4591. // 重新过滤
  4592. this.reFilter();
  4593.  
  4594. // 返回添加的关键字
  4595. return list[id];
  4596. }
  4597.  
  4598. /**
  4599. * 编辑关键字
  4600. * @param {Number} id 关键字 ID
  4601. * @param {*} values 关键字信息
  4602. */
  4603. update(id, values) {
  4604. // 获取列表
  4605. const list = this.list;
  4606.  
  4607. // 如果不存在则跳过
  4608. if (Object.hasOwn(list, id) === false) {
  4609. return null;
  4610. }
  4611.  
  4612. // 获取关键字
  4613. const entity = list[id];
  4614.  
  4615. // 更新关键字
  4616. Object.assign(entity, values);
  4617.  
  4618. // 保存数据
  4619. this.settings.keywords = list;
  4620.  
  4621. // 重新过滤
  4622. this.reFilter();
  4623. }
  4624.  
  4625. /**
  4626. * 删除关键字
  4627. * @param {Number} id 关键字 ID
  4628. */
  4629. remove(id) {
  4630. // 获取列表
  4631. const list = this.list;
  4632.  
  4633. // 如果不存在则跳过
  4634. if (Object.hasOwn(list, id) === false) {
  4635. return null;
  4636. }
  4637.  
  4638. // 获取关键字
  4639. const entity = list[id];
  4640.  
  4641. // 删除关键字
  4642. delete list[id];
  4643.  
  4644. // 保存数据
  4645. this.settings.keywords = list;
  4646.  
  4647. // 重新过滤
  4648. this.reFilter();
  4649.  
  4650. // 返回删除的关键字
  4651. return entity;
  4652. }
  4653.  
  4654. /**
  4655. * 获取帖子数据
  4656. * @param {*} item 绑定的 nFilter
  4657. */
  4658. async getPostInfo(item) {
  4659. const { tid, pid } = item;
  4660.  
  4661. // 请求帖子数据
  4662. const { subject, content, userInfo, reputation } =
  4663. await this.api.getPostInfo(tid, pid);
  4664.  
  4665. // 绑定用户信息和声望
  4666. if (userInfo) {
  4667. item.userInfo = userInfo;
  4668. item.username = userInfo.username;
  4669. item.reputation = reputation;
  4670. }
  4671.  
  4672. // 绑定标题和内容
  4673. item.subject = subject;
  4674. item.content = content;
  4675. }
  4676.  
  4677. /**
  4678. * 表格列
  4679. * @returns {Array} 表格列集合
  4680. */
  4681. columns() {
  4682. return [
  4683. { label: "关键字" },
  4684. { label: "过滤模式", center: true, width: 1 },
  4685. { label: "包括内容", center: true, width: 1 },
  4686. { label: "操作", width: 1 },
  4687. ];
  4688. }
  4689.  
  4690. /**
  4691. * 表格项
  4692. * @param {*} item 标记信息
  4693. * @returns {Array} 表格项集合
  4694. */
  4695. column(item) {
  4696. const { ui } = this;
  4697. const { table } = this.views;
  4698. const { id, keyword, filterLevel, filterMode } = item;
  4699.  
  4700. // 关键字
  4701. const input = ui.createElement("INPUT", [], {
  4702. type: "text",
  4703. value: keyword,
  4704. });
  4705.  
  4706. const inputWrapper = ui.createElement("DIV", input, {
  4707. className: "filter-input-wrapper",
  4708. });
  4709.  
  4710. // 切换过滤模式
  4711. const switchMode = ui.createButton(
  4712. filterMode || this.settings.filterModes[0],
  4713. () => {
  4714. const newMode = this.settings.switchModeByName(switchMode.innerText);
  4715.  
  4716. switchMode.innerText = newMode;
  4717. }
  4718. );
  4719.  
  4720. // 包括内容
  4721. const switchLevel = ui.createElement("INPUT", [], {
  4722. type: "checkbox",
  4723. checked: filterLevel > 0,
  4724. });
  4725.  
  4726. // 操作
  4727. const buttons = (() => {
  4728. const save = ui.createButton("保存", () => {
  4729. this.update(id, {
  4730. keyword: input.value,
  4731. filterMode: switchMode.innerText,
  4732. filterLevel: switchLevel.checked ? 1 : 0,
  4733. });
  4734. });
  4735.  
  4736. const remove = ui.createButton("删除", (e) => {
  4737. ui.confirm().then(() => {
  4738. this.remove(id);
  4739.  
  4740. table.remove(e);
  4741. });
  4742. });
  4743.  
  4744. return ui.createButtonGroup(save, remove);
  4745. })();
  4746.  
  4747. return [inputWrapper, switchMode, switchLevel, buttons];
  4748. }
  4749.  
  4750. /**
  4751. * 初始化组件
  4752. */
  4753. initComponents() {
  4754. super.initComponents();
  4755.  
  4756. const { ui } = this;
  4757. const { tabs, content } = ui.views;
  4758.  
  4759. const table = ui.createTable(this.columns());
  4760.  
  4761. const tips = ui.createElement("DIV", TIPS.keyword, {
  4762. className: "silver",
  4763. });
  4764.  
  4765. const tab = ui.createTab(
  4766. tabs,
  4767. this.constructor.label,
  4768. this.constructor.order,
  4769. {
  4770. onclick: () => {
  4771. this.render(content);
  4772. },
  4773. }
  4774. );
  4775.  
  4776. Object.assign(this.views, {
  4777. tab,
  4778. table,
  4779. });
  4780.  
  4781. this.views.container.appendChild(table);
  4782. this.views.container.appendChild(tips);
  4783. }
  4784.  
  4785. /**
  4786. * 渲染
  4787. * @param {HTMLElement} container 容器
  4788. */
  4789. render(container) {
  4790. super.render(container);
  4791.  
  4792. const { table } = this.views;
  4793.  
  4794. if (table) {
  4795. const { add, clear } = table;
  4796.  
  4797. clear();
  4798.  
  4799. Object.values(this.list).forEach((item) => {
  4800. const column = this.column(item);
  4801.  
  4802. add(...column);
  4803. });
  4804.  
  4805. this.renderNewLine();
  4806. }
  4807. }
  4808.  
  4809. /**
  4810. * 渲染新行
  4811. */
  4812. renderNewLine() {
  4813. const { ui } = this;
  4814. const { table } = this.views;
  4815.  
  4816. // 关键字
  4817. const input = ui.createElement("INPUT", [], {
  4818. type: "text",
  4819. });
  4820.  
  4821. const inputWrapper = ui.createElement("DIV", input, {
  4822. className: "filter-input-wrapper",
  4823. });
  4824.  
  4825. // 切换过滤模式
  4826. const switchMode = ui.createButton(this.settings.filterModes[0], () => {
  4827. const newMode = this.settings.switchModeByName(switchMode.innerText);
  4828.  
  4829. switchMode.innerText = newMode;
  4830. });
  4831.  
  4832. // 包括内容
  4833. const switchLevel = ui.createElement("INPUT", [], {
  4834. type: "checkbox",
  4835. });
  4836.  
  4837. // 操作
  4838. const buttons = (() => {
  4839. const save = ui.createButton("添加", (e) => {
  4840. const entity = this.add(
  4841. input.value,
  4842. switchMode.innerText,
  4843. switchLevel.checked ? 1 : 0
  4844. );
  4845.  
  4846. table.update(e, ...this.column(entity));
  4847.  
  4848. this.renderNewLine();
  4849. });
  4850.  
  4851. return ui.createButtonGroup(save);
  4852. })();
  4853.  
  4854. // 添加至列表
  4855. table.add(inputWrapper, switchMode, switchLevel, buttons);
  4856. }
  4857.  
  4858. /**
  4859. * 过滤
  4860. * @param {*} item 绑定的 nFilter
  4861. * @param {*} result 过滤结果
  4862. */
  4863. async filter(item, result) {
  4864. // 获取列表
  4865. const list = this.list;
  4866.  
  4867. // 跳过低于当前的过滤模式
  4868. const filtered = Object.values(list).filter(
  4869. (item) => this.settings.getModeByName(item.filterMode) > result.mode
  4870. );
  4871.  
  4872. // 没有则跳过
  4873. if (filtered.length === 0) {
  4874. return;
  4875. }
  4876.  
  4877. // 根据过滤模式依次判断
  4878. const sorted = Tools.sortBy(filtered, (item) =>
  4879. this.settings.getModeByName(item.filterMode)
  4880. );
  4881.  
  4882. for (let i = 0; i < sorted.length; i += 1) {
  4883. const { keyword, filterMode } = sorted[i];
  4884.  
  4885. // 过滤等级,0 为只过滤标题,1 为过滤标题和内容
  4886. const filterLevel = sorted[i].filterLevel || 0;
  4887.  
  4888. // 过滤标题
  4889. if (filterLevel >= 0) {
  4890. const { subject } = item;
  4891.  
  4892. const match = subject.match(keyword);
  4893.  
  4894. if (match) {
  4895. const mode = this.settings.getModeByName(filterMode);
  4896.  
  4897. // 更新过滤模式和原因
  4898. result.mode = mode;
  4899. result.reason = `关键字: ${match[0]}`;
  4900. return;
  4901. }
  4902. }
  4903.  
  4904. // 过滤内容
  4905. if (filterLevel >= 1) {
  4906. // 如果没有内容,则请求
  4907. if (item.content === undefined) {
  4908. await this.getPostInfo(item);
  4909. }
  4910.  
  4911. const { content } = item;
  4912.  
  4913. const match = content.match(keyword);
  4914.  
  4915. if (match) {
  4916. const mode = this.settings.getModeByName(filterMode);
  4917.  
  4918. // 更新过滤模式和原因
  4919. result.mode = mode;
  4920. result.reason = `关键字: ${match[0]}`;
  4921. return;
  4922. }
  4923. }
  4924. }
  4925. }
  4926.  
  4927. /**
  4928. * 重新过滤
  4929. */
  4930. reFilter() {
  4931. // 实际上应该根据过滤模式来筛选要过滤的部分
  4932. this.data.forEach((item) => {
  4933. item.execute();
  4934. });
  4935. }
  4936. }
  4937.  
  4938. /**
  4939. * 属地模块
  4940. */
  4941. class LocationModule extends Module {
  4942. /**
  4943. * 模块名称
  4944. */
  4945. static name = "location";
  4946.  
  4947. /**
  4948. * 模块标签
  4949. */
  4950. static label = "属地";
  4951.  
  4952. /**
  4953. * 顺序
  4954. */
  4955. static order = 50;
  4956.  
  4957. /**
  4958. * 请求缓存
  4959. */
  4960. cache = {};
  4961.  
  4962. /**
  4963. * 获取列表
  4964. */
  4965. get list() {
  4966. return this.settings.locations;
  4967. }
  4968.  
  4969. /**
  4970. * 获取属地
  4971. * @param {Number} id 属地 ID
  4972. */
  4973. get(id) {
  4974. // 获取列表
  4975. const list = this.list;
  4976.  
  4977. // 如果存在,则返回信息
  4978. if (list[id]) {
  4979. return list[id];
  4980. }
  4981.  
  4982. return null;
  4983. }
  4984.  
  4985. /**
  4986. * 添加属地
  4987. * @param {String} keyword 关键字
  4988. * @param {String} filterMode 过滤模式
  4989. */
  4990. add(keyword, filterMode) {
  4991. // 获取列表
  4992. const list = this.list;
  4993.  
  4994. // ID 为最大值 + 1
  4995. const id = Math.max(...Object.keys(list), 0) + 1;
  4996.  
  4997. // 写入属地信息
  4998. list[id] = {
  4999. id,
  5000. keyword,
  5001. filterMode,
  5002. };
  5003.  
  5004. // 保存数据
  5005. this.settings.locations = list;
  5006.  
  5007. // 重新过滤
  5008. this.reFilter();
  5009.  
  5010. // 返回添加的属地
  5011. return list[id];
  5012. }
  5013.  
  5014. /**
  5015. * 编辑属地
  5016. * @param {Number} id 属地 ID
  5017. * @param {*} values 属地信息
  5018. */
  5019. update(id, values) {
  5020. // 获取列表
  5021. const list = this.list;
  5022.  
  5023. // 如果不存在则跳过
  5024. if (Object.hasOwn(list, id) === false) {
  5025. return null;
  5026. }
  5027.  
  5028. // 获取属地
  5029. const entity = list[id];
  5030.  
  5031. // 更新属地
  5032. Object.assign(entity, values);
  5033.  
  5034. // 保存数据
  5035. this.settings.locations = list;
  5036.  
  5037. // 重新过滤
  5038. this.reFilter();
  5039. }
  5040.  
  5041. /**
  5042. * 删除属地
  5043. * @param {Number} id 属地 ID
  5044. */
  5045. remove(id) {
  5046. // 获取列表
  5047. const list = this.list;
  5048.  
  5049. // 如果不存在则跳过
  5050. if (Object.hasOwn(list, id) === false) {
  5051. return null;
  5052. }
  5053.  
  5054. // 获取属地
  5055. const entity = list[id];
  5056.  
  5057. // 删除属地
  5058. delete list[id];
  5059.  
  5060. // 保存数据
  5061. this.settings.locations = list;
  5062.  
  5063. // 重新过滤
  5064. this.reFilter();
  5065.  
  5066. // 返回删除的属地
  5067. return entity;
  5068. }
  5069.  
  5070. /**
  5071. * 获取 IP 属地
  5072. * @param {*} item 绑定的 nFilter
  5073. */
  5074. async getIpLocation(item) {
  5075. const { uid } = item;
  5076.  
  5077. // 如果是匿名直接跳过
  5078. if (uid <= 0) {
  5079. return;
  5080. }
  5081.  
  5082. // 如果已有缓存,直接返回
  5083. if (Object.hasOwn(this.cache, uid)) {
  5084. return this.cache[uid];
  5085. }
  5086.  
  5087. // 请求属地
  5088. const { ipLoc } = await this.api.getUserInfo(uid);
  5089.  
  5090. // 写入缓存
  5091. if (ipLoc) {
  5092. this.cache[uid] = ipLoc;
  5093. }
  5094.  
  5095. // 返回结果
  5096. return ipLoc;
  5097. }
  5098.  
  5099. /**
  5100. * 表格列
  5101. * @returns {Array} 表格列集合
  5102. */
  5103. columns() {
  5104. return [
  5105. { label: "关键字" },
  5106. { label: "过滤模式", center: true, width: 1 },
  5107. { label: "操作", width: 1 },
  5108. ];
  5109. }
  5110.  
  5111. /**
  5112. * 表格项
  5113. * @param {*} item 标记信息
  5114. * @returns {Array} 表格项集合
  5115. */
  5116. column(item) {
  5117. const { ui } = this;
  5118. const { table } = this.views;
  5119. const { id, keyword, filterMode } = item;
  5120.  
  5121. // 关键字
  5122. const input = ui.createElement("INPUT", [], {
  5123. type: "text",
  5124. value: keyword,
  5125. });
  5126.  
  5127. const inputWrapper = ui.createElement("DIV", input, {
  5128. className: "filter-input-wrapper",
  5129. });
  5130.  
  5131. // 切换过滤模式
  5132. const switchMode = ui.createButton(
  5133. filterMode || this.settings.filterModes[0],
  5134. () => {
  5135. const newMode = this.settings.switchModeByName(switchMode.innerText);
  5136.  
  5137. switchMode.innerText = newMode;
  5138. }
  5139. );
  5140.  
  5141. // 操作
  5142. const buttons = (() => {
  5143. const save = ui.createButton("保存", () => {
  5144. this.update(id, {
  5145. keyword: input.value,
  5146. filterMode: switchMode.innerText,
  5147. });
  5148. });
  5149.  
  5150. const remove = ui.createButton("删除", (e) => {
  5151. ui.confirm().then(() => {
  5152. this.remove(id);
  5153.  
  5154. table.remove(e);
  5155. });
  5156. });
  5157.  
  5158. return ui.createButtonGroup(save, remove);
  5159. })();
  5160.  
  5161. return [inputWrapper, switchMode, buttons];
  5162. }
  5163.  
  5164. /**
  5165. * 初始化组件
  5166. */
  5167. initComponents() {
  5168. super.initComponents();
  5169.  
  5170. const { ui } = this;
  5171. const { tabs, content } = ui.views;
  5172.  
  5173. const table = ui.createTable(this.columns());
  5174.  
  5175. const tips = ui.createElement("DIV", TIPS.keyword, {
  5176. className: "silver",
  5177. });
  5178.  
  5179. const tab = ui.createTab(
  5180. tabs,
  5181. this.constructor.label,
  5182. this.constructor.order,
  5183. {
  5184. onclick: () => {
  5185. this.render(content);
  5186. },
  5187. }
  5188. );
  5189.  
  5190. Object.assign(this.views, {
  5191. tab,
  5192. table,
  5193. });
  5194.  
  5195. this.views.container.appendChild(table);
  5196. this.views.container.appendChild(tips);
  5197. }
  5198.  
  5199. /**
  5200. * 渲染
  5201. * @param {HTMLElement} container 容器
  5202. */
  5203. render(container) {
  5204. super.render(container);
  5205.  
  5206. const { table } = this.views;
  5207.  
  5208. if (table) {
  5209. const { add, clear } = table;
  5210.  
  5211. clear();
  5212.  
  5213. Object.values(this.list).forEach((item) => {
  5214. const column = this.column(item);
  5215.  
  5216. add(...column);
  5217. });
  5218.  
  5219. this.renderNewLine();
  5220. }
  5221. }
  5222.  
  5223. /**
  5224. * 渲染新行
  5225. */
  5226. renderNewLine() {
  5227. const { ui } = this;
  5228. const { table } = this.views;
  5229.  
  5230. // 关键字
  5231. const input = ui.createElement("INPUT", [], {
  5232. type: "text",
  5233. });
  5234.  
  5235. const inputWrapper = ui.createElement("DIV", input, {
  5236. className: "filter-input-wrapper",
  5237. });
  5238.  
  5239. // 切换过滤模式
  5240. const switchMode = ui.createButton(this.settings.filterModes[0], () => {
  5241. const newMode = this.settings.switchModeByName(switchMode.innerText);
  5242.  
  5243. switchMode.innerText = newMode;
  5244. });
  5245.  
  5246. // 操作
  5247. const buttons = (() => {
  5248. const save = ui.createButton("添加", (e) => {
  5249. const entity = this.add(input.value, switchMode.innerText);
  5250.  
  5251. table.update(e, ...this.column(entity));
  5252.  
  5253. this.renderNewLine();
  5254. });
  5255.  
  5256. return ui.createButtonGroup(save);
  5257. })();
  5258.  
  5259. // 添加至列表
  5260. table.add(inputWrapper, switchMode, buttons);
  5261. }
  5262.  
  5263. /**
  5264. * 过滤
  5265. * @param {*} item 绑定的 nFilter
  5266. * @param {*} result 过滤结果
  5267. */
  5268. async filter(item, result) {
  5269. // 获取列表
  5270. const list = this.list;
  5271.  
  5272. // 跳过低于当前的过滤模式
  5273. const filtered = Object.values(list).filter(
  5274. (item) => this.settings.getModeByName(item.filterMode) > result.mode
  5275. );
  5276.  
  5277. // 没有则跳过
  5278. if (filtered.length === 0) {
  5279. return;
  5280. }
  5281.  
  5282. // 获取当前属地
  5283. const location = await this.getIpLocation(item);
  5284.  
  5285. // 请求失败则跳过
  5286. if (location === undefined) {
  5287. return;
  5288. }
  5289.  
  5290. // 根据过滤模式依次判断
  5291. const sorted = Tools.sortBy(filtered, (item) =>
  5292. this.settings.getModeByName(item.filterMode)
  5293. );
  5294.  
  5295. for (let i = 0; i < sorted.length; i += 1) {
  5296. const { keyword, filterMode } = sorted[i];
  5297.  
  5298. const match = location.match(keyword);
  5299.  
  5300. if (match) {
  5301. const mode = this.settings.getModeByName(filterMode);
  5302.  
  5303. // 更新过滤模式和原因
  5304. result.mode = mode;
  5305. result.reason = `属地: ${match[0]}`;
  5306. return;
  5307. }
  5308. }
  5309. }
  5310.  
  5311. /**
  5312. * 重新过滤
  5313. */
  5314. reFilter() {
  5315. // 实际上应该根据过滤模式来筛选要过滤的部分
  5316. this.data.forEach((item) => {
  5317. item.execute();
  5318. });
  5319. }
  5320. }
  5321.  
  5322. /**
  5323. * 猎巫模块
  5324. *
  5325. * 其实是通过 Cache 模块读取配置,而非 Settings
  5326. */
  5327. class HunterModule extends Module {
  5328. /**
  5329. * 模块名称
  5330. */
  5331. static name = "hunter";
  5332.  
  5333. /**
  5334. * 模块标签
  5335. */
  5336. static label = "猎巫";
  5337.  
  5338. /**
  5339. * 顺序
  5340. */
  5341. static order = 60;
  5342.  
  5343. /**
  5344. * 请求缓存
  5345. */
  5346. cache = {};
  5347.  
  5348. /**
  5349. * 请求队列
  5350. */
  5351. queue = [];
  5352.  
  5353. /**
  5354. * 获取列表
  5355. */
  5356. get list() {
  5357. return this.settings.cache
  5358. .get("WITCH_HUNT")
  5359. .then((values) => values || []);
  5360. }
  5361.  
  5362. /**
  5363. * 获取猎巫
  5364. * @param {Number} id 猎巫 ID
  5365. */
  5366. async get(id) {
  5367. // 获取列表
  5368. const list = await this.list;
  5369.  
  5370. // 如果存在,则返回信息
  5371. if (list[id]) {
  5372. return list[id];
  5373. }
  5374.  
  5375. return null;
  5376. }
  5377.  
  5378. /**
  5379. * 添加猎巫
  5380. * @param {Number} fid 版面 ID
  5381. * @param {String} label 标签
  5382. * @param {String} filterMode 过滤模式
  5383. * @param {Number} filterLevel 过滤等级: 0 - 仅标记; 1 - 标记并过滤
  5384. */
  5385. async add(fid, label, filterMode, filterLevel) {
  5386. // FID 只能是数字
  5387. fid = parseInt(fid, 10);
  5388.  
  5389. // 获取列表
  5390. const list = await this.list;
  5391.  
  5392. // 如果版面 ID 已存在,则提示错误
  5393. if (Object.keys(list).includes(fid)) {
  5394. alert("已有相同版面ID");
  5395. return;
  5396. }
  5397.  
  5398. // 请求版面信息
  5399. const info = await this.api.getForumInfo(fid);
  5400.  
  5401. // 如果版面不存在,则提示错误
  5402. if (info === null) {
  5403. alert("版面ID有误");
  5404. return;
  5405. }
  5406.  
  5407. // 计算标记颜色
  5408. const color = Tools.generateColor(info.name);
  5409.  
  5410. // 写入猎巫信息
  5411. list[fid] = {
  5412. fid,
  5413. name: info.name,
  5414. label,
  5415. color,
  5416. filterMode,
  5417. filterLevel,
  5418. };
  5419.  
  5420. // 保存数据
  5421. this.settings.cache.put("WITCH_HUNT", list);
  5422.  
  5423. // 重新过滤
  5424. this.reFilter(true);
  5425.  
  5426. // 返回添加的猎巫
  5427. return list[fid];
  5428. }
  5429.  
  5430. /**
  5431. * 编辑猎巫
  5432. * @param {Number} fid 版面 ID
  5433. * @param {*} values 猎巫信息
  5434. */
  5435. async update(fid, values) {
  5436. // 获取列表
  5437. const list = await this.list;
  5438.  
  5439. // 如果不存在则跳过
  5440. if (Object.hasOwn(list, fid) === false) {
  5441. return null;
  5442. }
  5443.  
  5444. // 获取猎巫
  5445. const entity = list[fid];
  5446.  
  5447. // 更新猎巫
  5448. Object.assign(entity, values);
  5449.  
  5450. // 保存数据
  5451. this.settings.cache.put("WITCH_HUNT", list);
  5452.  
  5453. // 重新过滤,更新样式即可
  5454. this.reFilter(false);
  5455. }
  5456.  
  5457. /**
  5458. * 删除猎巫
  5459. * @param {Number} fid 版面 ID
  5460. */
  5461. async remove(fid) {
  5462. // 获取列表
  5463. const list = await this.list;
  5464.  
  5465. // 如果不存在则跳过
  5466. if (Object.hasOwn(list, fid) === false) {
  5467. return null;
  5468. }
  5469.  
  5470. // 获取猎巫
  5471. const entity = list[fid];
  5472.  
  5473. // 删除猎巫
  5474. delete list[fid];
  5475.  
  5476. // 保存数据
  5477. this.settings.cache.put("WITCH_HUNT", list);
  5478.  
  5479. // 重新过滤
  5480. this.reFilter(true);
  5481.  
  5482. // 返回删除的属地
  5483. return entity;
  5484. }
  5485.  
  5486. /**
  5487. * 格式化版面
  5488. * @param {Number} fid 版面 ID
  5489. * @param {String} name 版面名称
  5490. */
  5491. formatForum(fid, name) {
  5492. const { ui } = this;
  5493.  
  5494. return ui.createElement("A", `[${name}]`, {
  5495. className: "b nobr",
  5496. href: `/thread.php?fid=${fid}`,
  5497. });
  5498. }
  5499.  
  5500. /**
  5501. * 格式化标签
  5502. * @param {String} name 标签名称
  5503. * @param {String} name 标签颜色
  5504. */
  5505. formatLabel(name, color) {
  5506. const { ui } = this;
  5507.  
  5508. return ui.createElement("B", name, {
  5509. className: "block_txt nobr",
  5510. style: `background: ${color}; color: #FFF; margin: 0.1em 0.2em;`,
  5511. });
  5512. }
  5513.  
  5514. /**
  5515. * 表格列
  5516. * @returns {Array} 表格列集合
  5517. */
  5518. columns() {
  5519. return [
  5520. { label: "版面", width: 200 },
  5521. { label: "标签" },
  5522. { label: "启用过滤", center: true, width: 1 },
  5523. { label: "过滤模式", center: true, width: 1 },
  5524. { label: "操作", width: 1 },
  5525. ];
  5526. }
  5527.  
  5528. /**
  5529. * 表格项
  5530. * @param {*} item 标记信息
  5531. * @returns {Array} 表格项集合
  5532. */
  5533. column(item) {
  5534. const { ui } = this;
  5535. const { table } = this.views;
  5536. const { fid, name, label, color, filterMode, filterLevel } = item;
  5537.  
  5538. // 版面
  5539. const forum = this.formatForum(fid, name);
  5540.  
  5541. // 标签
  5542. const labelElement = this.formatLabel(label, color);
  5543.  
  5544. // 启用过滤
  5545. const switchLevel = ui.createElement("INPUT", [], {
  5546. type: "checkbox",
  5547. checked: filterLevel > 0,
  5548. });
  5549.  
  5550. // 切换过滤模式
  5551. const switchMode = ui.createButton(
  5552. filterMode || this.settings.filterModes[0],
  5553. () => {
  5554. const newMode = this.settings.switchModeByName(switchMode.innerText);
  5555.  
  5556. switchMode.innerText = newMode;
  5557. }
  5558. );
  5559.  
  5560. // 操作
  5561. const buttons = (() => {
  5562. const save = ui.createButton("保存", () => {
  5563. this.update(fid, {
  5564. filterMode: switchMode.innerText,
  5565. filterLevel: switchLevel.checked ? 1 : 0,
  5566. });
  5567. });
  5568.  
  5569. const remove = ui.createButton("删除", (e) => {
  5570. ui.confirm().then(async () => {
  5571. await this.remove(fid);
  5572.  
  5573. table.remove(e);
  5574. });
  5575. });
  5576.  
  5577. return ui.createButtonGroup(save, remove);
  5578. })();
  5579.  
  5580. return [forum, labelElement, switchLevel, switchMode, buttons];
  5581. }
  5582.  
  5583. /**
  5584. * 初始化组件
  5585. */
  5586. initComponents() {
  5587. super.initComponents();
  5588.  
  5589. const { ui } = this;
  5590. const { tabs, content } = ui.views;
  5591.  
  5592. const table = ui.createTable(this.columns());
  5593.  
  5594. const tips = ui.createElement("DIV", TIPS.hunter, {
  5595. className: "silver",
  5596. });
  5597.  
  5598. const tab = ui.createTab(
  5599. tabs,
  5600. this.constructor.label,
  5601. this.constructor.order,
  5602. {
  5603. onclick: () => {
  5604. this.render(content);
  5605. },
  5606. }
  5607. );
  5608.  
  5609. Object.assign(this.views, {
  5610. tab,
  5611. table,
  5612. });
  5613.  
  5614. this.views.container.appendChild(table);
  5615. this.views.container.appendChild(tips);
  5616. }
  5617.  
  5618. /**
  5619. * 渲染
  5620. * @param {HTMLElement} container 容器
  5621. */
  5622. render(container) {
  5623. super.render(container);
  5624.  
  5625. const { table } = this.views;
  5626.  
  5627. if (table) {
  5628. const { add, clear } = table;
  5629.  
  5630. clear();
  5631.  
  5632. this.list.then((values) => {
  5633. Object.values(values).forEach((item) => {
  5634. const column = this.column(item);
  5635.  
  5636. add(...column);
  5637. });
  5638.  
  5639. this.renderNewLine();
  5640. });
  5641. }
  5642. }
  5643.  
  5644. /**
  5645. * 渲染新行
  5646. */
  5647. renderNewLine() {
  5648. const { ui } = this;
  5649. const { table } = this.views;
  5650.  
  5651. // 版面 ID
  5652. const forumInput = ui.createElement("INPUT", [], {
  5653. type: "text",
  5654. });
  5655.  
  5656. const forumInputWrapper = ui.createElement("DIV", forumInput, {
  5657. className: "filter-input-wrapper",
  5658. });
  5659.  
  5660. // 标签
  5661. const labelInput = ui.createElement("INPUT", [], {
  5662. type: "text",
  5663. });
  5664.  
  5665. const labelInputWrapper = ui.createElement("DIV", labelInput, {
  5666. className: "filter-input-wrapper",
  5667. });
  5668.  
  5669. // 启用过滤
  5670. const switchLevel = ui.createElement("INPUT", [], {
  5671. type: "checkbox",
  5672. });
  5673.  
  5674. // 切换过滤模式
  5675. const switchMode = ui.createButton(this.settings.filterModes[0], () => {
  5676. const newMode = this.settings.switchModeByName(switchMode.innerText);
  5677.  
  5678. switchMode.innerText = newMode;
  5679. });
  5680.  
  5681. // 操作
  5682. const buttons = (() => {
  5683. const save = ui.createButton("添加", async (e) => {
  5684. const entity = await this.add(
  5685. forumInput.value,
  5686. labelInput.value,
  5687. switchMode.innerText,
  5688. switchLevel.checked ? 1 : 0
  5689. );
  5690.  
  5691. table.update(e, ...this.column(entity));
  5692.  
  5693. this.renderNewLine();
  5694. });
  5695.  
  5696. return ui.createButtonGroup(save);
  5697. })();
  5698.  
  5699. // 添加至列表
  5700. table.add(
  5701. forumInputWrapper,
  5702. labelInputWrapper,
  5703. switchLevel,
  5704. switchMode,
  5705. buttons
  5706. );
  5707. }
  5708.  
  5709. /**
  5710. * 过滤
  5711. * @param {*} item 绑定的 nFilter
  5712. * @param {*} result 过滤结果
  5713. */
  5714. async filter(item, result) {
  5715. // 获取当前猎巫结果
  5716. const hunter = item.hunter || [];
  5717.  
  5718. // 如果没有猎巫结果,则跳过
  5719. if (hunter.length === 0) {
  5720. return;
  5721. }
  5722.  
  5723. // 获取列表
  5724. const items = await this.list;
  5725.  
  5726. // 筛选出匹配的猎巫
  5727. const list = Object.values(items).filter(({ fid }) =>
  5728. hunter.includes(fid)
  5729. );
  5730.  
  5731. // 取最高的过滤模式
  5732. // 低于当前的过滤模式则跳过
  5733. let max = result.mode;
  5734. let res = null;
  5735.  
  5736. for (const entity of list) {
  5737. const { filterLevel, filterMode } = entity;
  5738.  
  5739. // 仅标记
  5740. if (filterLevel === 0) {
  5741. continue;
  5742. }
  5743.  
  5744. // 获取过滤模式
  5745. const mode = this.settings.getModeByName(filterMode);
  5746.  
  5747. if (mode <= max) {
  5748. continue;
  5749. }
  5750.  
  5751. max = mode;
  5752. res = entity;
  5753. }
  5754.  
  5755. // 没有匹配的则跳过
  5756. if (res === null) {
  5757. return;
  5758. }
  5759.  
  5760. // 更新过滤模式和原因
  5761. result.mode = max;
  5762. result.reason = `猎巫: ${res.label}`;
  5763. }
  5764.  
  5765. /**
  5766. * 通知
  5767. * @param {*} item 绑定的 nFilter
  5768. */
  5769. async notify(item) {
  5770. const { uid, tags } = item;
  5771.  
  5772. // 如果没有 tags 组件则跳过
  5773. if (tags === null) {
  5774. return;
  5775. }
  5776.  
  5777. // 如果是匿名,隐藏组件
  5778. if (uid <= 0) {
  5779. tags.style.display = "none";
  5780. return;
  5781. }
  5782.  
  5783. // 删除旧标签
  5784. [...tags.querySelectorAll("[fid]")].forEach((item) => {
  5785. tags.removeChild(item);
  5786. });
  5787.  
  5788. // 如果没有请求,开始请求
  5789. if (Object.hasOwn(item, "hunter") === false) {
  5790. this.execute(item);
  5791. return;
  5792. }
  5793.  
  5794. // 获取当前猎巫结果
  5795. const hunter = item.hunter;
  5796.  
  5797. // 如果没有猎巫结果,则跳过
  5798. if (hunter.length === 0) {
  5799. return;
  5800. }
  5801.  
  5802. // 格式化标签
  5803. const items = await Promise.all(
  5804. hunter.map(async (fid) => {
  5805. const item = await this.get(fid);
  5806.  
  5807. if (item) {
  5808. const element = this.formatLabel(item.label, item.color);
  5809.  
  5810. element.setAttribute("fid", fid);
  5811.  
  5812. return element;
  5813. }
  5814.  
  5815. return null;
  5816. })
  5817. );
  5818.  
  5819. // 加入组件
  5820. items.forEach((item) => {
  5821. if (item) {
  5822. tags.appendChild(item);
  5823. }
  5824. });
  5825. }
  5826.  
  5827. /**
  5828. * 重新过滤
  5829. * @param {Boolean} clear 是否清除缓存
  5830. */
  5831. reFilter(clear) {
  5832. // 清除缓存
  5833. if (clear) {
  5834. this.cache = {};
  5835. }
  5836.  
  5837. // 重新过滤
  5838. this.data.forEach((item) => {
  5839. // 不需要清除缓存的话,只要重新加载标记
  5840. if (clear === false) {
  5841. item.hunter = [];
  5842. }
  5843.  
  5844. // 重新猎巫
  5845. this.execute(item);
  5846. });
  5847. }
  5848.  
  5849. /**
  5850. * 猎巫
  5851. * @param {*} item 绑定的 nFilter
  5852. */
  5853. async execute(item) {
  5854. const { uid } = item;
  5855. const { api, cache, queue, list } = this;
  5856.  
  5857. // 如果是匿名,则跳过
  5858. if (uid <= 0) {
  5859. return;
  5860. }
  5861.  
  5862. // 初始化猎巫结果,用于标识正在猎巫
  5863. item.hunter = item.hunter || [];
  5864.  
  5865. // 获取列表
  5866. const items = await list;
  5867.  
  5868. // 没有设置且没有旧数据,直接跳过
  5869. if (items.length === 0 && item.hunter.length === 0) {
  5870. return;
  5871. }
  5872.  
  5873. // 重新过滤
  5874. const reload = (newValue) => {
  5875. const isEqual = newValue.sort().join() === item.hunter.sort().join();
  5876.  
  5877. if (isEqual) {
  5878. return;
  5879. }
  5880.  
  5881. item.hunter = newValue;
  5882. item.execute();
  5883. };
  5884.  
  5885. // 创建任务
  5886. const task = async () => {
  5887. // 如果缓存里没有记录,请求数据并写入缓存
  5888. if (Object.hasOwn(cache, uid) === false) {
  5889. cache[uid] = [];
  5890.  
  5891. await Promise.all(
  5892. Object.keys(items).map(async (fid) => {
  5893. // 转换为数字格式
  5894. const id = parseInt(fid, 10);
  5895.  
  5896. // 当前版面发言记录
  5897. const result = await api.getForumPosted(id, uid);
  5898.  
  5899. // 写入当前设置
  5900. if (result) {
  5901. cache[uid].push(id);
  5902. }
  5903. })
  5904. );
  5905. }
  5906.  
  5907. // 重新过滤
  5908. reload(cache[uid]);
  5909.  
  5910. // 将当前任务移出队列
  5911. queue.shift();
  5912.  
  5913. // 如果还有任务,继续执行
  5914. if (queue.length > 0) {
  5915. queue[0]();
  5916. }
  5917. };
  5918.  
  5919. // 队列里已经有任务
  5920. const isRunning = queue.length > 0;
  5921.  
  5922. // 加入队列
  5923. queue.push(task);
  5924.  
  5925. // 如果没有正在执行的任务,则立即执行
  5926. if (isRunning === false) {
  5927. task();
  5928. }
  5929. }
  5930. }
  5931.  
  5932. /**
  5933. * 杂项模块
  5934. */
  5935. class MiscModule extends Module {
  5936. /**
  5937. * 模块名称
  5938. */
  5939. static name = "misc";
  5940.  
  5941. /**
  5942. * 模块标签
  5943. */
  5944. static label = "杂项";
  5945.  
  5946. /**
  5947. * 顺序
  5948. */
  5949. static order = 100;
  5950.  
  5951. /**
  5952. * 请求缓存
  5953. */
  5954. cache = {
  5955. topicNums: {},
  5956. };
  5957.  
  5958. /**
  5959. * 获取用户信息(从页面上)
  5960. * @param {*} item 绑定的 nFilter
  5961. */
  5962. getUserInfo(item) {
  5963. const { uid } = item;
  5964.  
  5965. // 如果是匿名直接跳过
  5966. if (uid <= 0) {
  5967. return;
  5968. }
  5969.  
  5970. // 回复页面可以直接获取到用户信息和声望
  5971. if (commonui.userInfo) {
  5972. // 取得用户信息
  5973. const userInfo = commonui.userInfo.users[uid];
  5974.  
  5975. // 绑定用户信息和声望
  5976. if (userInfo) {
  5977. item.userInfo = userInfo;
  5978. item.username = userInfo.username;
  5979.  
  5980. item.reputation = (() => {
  5981. const reputations = commonui.userInfo.reputations;
  5982.  
  5983. if (reputations) {
  5984. for (let fid in reputations) {
  5985. return reputations[fid][uid] || 0;
  5986. }
  5987. }
  5988.  
  5989. return NaN;
  5990. })();
  5991. }
  5992. }
  5993. }
  5994.  
  5995. /**
  5996. * 获取帖子数据
  5997. * @param {*} item 绑定的 nFilter
  5998. */
  5999. async getPostInfo(item) {
  6000. const { tid, pid } = item;
  6001.  
  6002. // 请求帖子数据
  6003. const { subject, content, userInfo, reputation } =
  6004. await this.api.getPostInfo(tid, pid);
  6005.  
  6006. // 绑定用户信息和声望
  6007. if (userInfo) {
  6008. item.userInfo = userInfo;
  6009. item.username = userInfo.username;
  6010. item.reputation = reputation;
  6011. }
  6012.  
  6013. // 绑定标题和内容
  6014. item.subject = subject;
  6015. item.content = content;
  6016. }
  6017.  
  6018. /**
  6019. * 获取主题数量
  6020. * @param {*} item 绑定的 nFilter
  6021. */
  6022. async getTopicNum(item) {
  6023. const { uid } = item;
  6024.  
  6025. // 如果是匿名直接跳过
  6026. if (uid <= 0) {
  6027. return;
  6028. }
  6029.  
  6030. // 如果已有缓存,直接返回
  6031. if (Object.hasOwn(this.cache.topicNums, uid)) {
  6032. return this.cache.topicNums[uid];
  6033. }
  6034.  
  6035. // 请求数量
  6036. const number = await this.api.getTopicNum(uid);
  6037.  
  6038. // 写入缓存
  6039. this.cache.topicNums[uid] = number;
  6040.  
  6041. // 返回结果
  6042. return number;
  6043. }
  6044.  
  6045. /**
  6046. * 初始化,增加设置
  6047. */
  6048. initComponents() {
  6049. super.initComponents();
  6050.  
  6051. const { settings, ui } = this;
  6052. const { add } = ui.views.settings;
  6053.  
  6054. // 小号过滤(注册时间)
  6055. {
  6056. const input = ui.createElement("INPUT", [], {
  6057. type: "text",
  6058. value: settings.filterRegdateLimit / 86400000,
  6059. maxLength: 4,
  6060. style: "width: 48px;",
  6061. });
  6062.  
  6063. const button = ui.createButton("确认", () => {
  6064. const newValue = parseInt(input.value, 10) || 0;
  6065.  
  6066. if (newValue < 0) {
  6067. return;
  6068. }
  6069.  
  6070. settings.filterRegdateLimit = newValue * 86400000;
  6071.  
  6072. this.reFilter();
  6073. });
  6074.  
  6075. const element = ui.createElement("DIV", [
  6076. "隐藏注册时间小于",
  6077. input,
  6078. "天的用户",
  6079. button,
  6080. ]);
  6081.  
  6082. add(this.constructor.order + 0, element);
  6083. }
  6084.  
  6085. // 小号过滤(发帖数)
  6086. {
  6087. const input = ui.createElement("INPUT", [], {
  6088. type: "text",
  6089. value: settings.filterPostnumLimit,
  6090. maxLength: 5,
  6091. style: "width: 48px;",
  6092. });
  6093.  
  6094. const button = ui.createButton("确认", () => {
  6095. const newValue = parseInt(input.value, 10) || 0;
  6096.  
  6097. if (newValue < 0) {
  6098. return;
  6099. }
  6100.  
  6101. settings.filterPostnumLimit = newValue;
  6102.  
  6103. this.reFilter();
  6104. });
  6105.  
  6106. const element = ui.createElement("DIV", [
  6107. "隐藏发帖数量小于",
  6108. input,
  6109. "贴的用户",
  6110. button,
  6111. ]);
  6112.  
  6113. add(this.constructor.order + 1, element);
  6114. }
  6115.  
  6116. // 流量号过滤(主题比例)
  6117. {
  6118. const input = ui.createElement("INPUT", [], {
  6119. type: "text",
  6120. value: settings.filterTopicRateLimit,
  6121. maxLength: 3,
  6122. style: "width: 48px;",
  6123. });
  6124.  
  6125. const button = ui.createButton("确认", () => {
  6126. const newValue = parseInt(input.value, 10) || 100;
  6127.  
  6128. if (newValue <= 0 || newValue > 100) {
  6129. return;
  6130. }
  6131.  
  6132. settings.filterTopicRateLimit = newValue;
  6133.  
  6134. this.reFilter();
  6135. });
  6136.  
  6137. const element = ui.createElement("DIV", [
  6138. "隐藏发帖比例大于",
  6139. input,
  6140. "%的用户",
  6141. button,
  6142. ]);
  6143.  
  6144. add(this.constructor.order + 2, element);
  6145. }
  6146.  
  6147. // 声望过滤
  6148. {
  6149. const input = ui.createElement("INPUT", [], {
  6150. type: "text",
  6151. value: settings.filterReputationLimit || "",
  6152. maxLength: 4,
  6153. style: "width: 48px;",
  6154. });
  6155.  
  6156. const button = ui.createButton("确认", () => {
  6157. const newValue = parseInt(input.value, 10);
  6158.  
  6159. settings.filterReputationLimit = newValue;
  6160.  
  6161. this.reFilter();
  6162. });
  6163.  
  6164. const element = ui.createElement("DIV", [
  6165. "隐藏版面声望低于",
  6166. input,
  6167. "点的用户",
  6168. button,
  6169. ]);
  6170.  
  6171. add(this.constructor.order + 3, element);
  6172. }
  6173.  
  6174. // 匿名过滤
  6175. {
  6176. const input = ui.createElement("INPUT", [], {
  6177. type: "checkbox",
  6178. checked: settings.filterAnonymous,
  6179. });
  6180.  
  6181. const label = ui.createElement("LABEL", ["隐藏匿名的用户", input], {
  6182. style: "display: flex;",
  6183. });
  6184.  
  6185. const element = ui.createElement("DIV", label);
  6186.  
  6187. input.onchange = () => {
  6188. settings.filterAnonymous = input.checked;
  6189.  
  6190. this.reFilter();
  6191. };
  6192.  
  6193. add(this.constructor.order + 4, element);
  6194. }
  6195. }
  6196.  
  6197. /**
  6198. * 过滤
  6199. * @param {*} item 绑定的 nFilter
  6200. * @param {*} result 过滤结果
  6201. */
  6202. async filter(item, result) {
  6203. // 获取隐藏模式下标
  6204. const mode = this.settings.getModeByName("隐藏");
  6205.  
  6206. // 如果当前模式不低于隐藏模式,则跳过
  6207. if (result.mode >= mode) {
  6208. return;
  6209. }
  6210.  
  6211. // 匿名过滤
  6212. await this.filterByAnonymous(item, result);
  6213.  
  6214. // 注册时间过滤
  6215. await this.filterByRegdate(item, result);
  6216.  
  6217. // 发帖数量过滤
  6218. await this.filterByPostnum(item, result);
  6219.  
  6220. // 发帖比例过滤
  6221. await this.filterByTopicRate(item, result);
  6222.  
  6223. // 版面声望过滤
  6224. await this.filterByReputation(item, result);
  6225. }
  6226.  
  6227. /**
  6228. * 根据匿名过滤
  6229. * @param {*} item 绑定的 nFilter
  6230. * @param {*} result 过滤结果
  6231. */
  6232. async filterByAnonymous(item, result) {
  6233. const { uid } = item;
  6234.  
  6235. // 如果不是匿名,则跳过
  6236. if (uid > 0) {
  6237. return;
  6238. }
  6239.  
  6240. // 获取隐藏模式下标
  6241. const mode = this.settings.getModeByName("隐藏");
  6242.  
  6243. // 如果当前模式不低于隐藏模式,则跳过
  6244. if (result.mode >= mode) {
  6245. return;
  6246. }
  6247.  
  6248. // 获取过滤匿名设置
  6249. const filterAnonymous = this.settings.filterAnonymous;
  6250.  
  6251. if (filterAnonymous) {
  6252. // 更新过滤模式和原因
  6253. result.mode = mode;
  6254. result.reason = "匿名";
  6255. }
  6256. }
  6257.  
  6258. /**
  6259. * 根据注册时间过滤
  6260. * @param {*} item 绑定的 nFilter
  6261. * @param {*} result 过滤结果
  6262. */
  6263. async filterByRegdate(item, result) {
  6264. const { uid } = item;
  6265.  
  6266. // 如果是匿名,则跳过
  6267. if (uid <= 0) {
  6268. return;
  6269. }
  6270.  
  6271. // 获取隐藏模式下标
  6272. const mode = this.settings.getModeByName("隐藏");
  6273.  
  6274. // 如果当前模式不低于隐藏模式,则跳过
  6275. if (result.mode >= mode) {
  6276. return;
  6277. }
  6278.  
  6279. // 获取注册时间限制
  6280. const filterRegdateLimit = this.settings.filterRegdateLimit;
  6281.  
  6282. // 未启用则跳过
  6283. if (filterRegdateLimit <= 0) {
  6284. return;
  6285. }
  6286.  
  6287. // 没有用户信息,优先从页面上获取
  6288. if (item.userInfo === undefined) {
  6289. this.getUserInfo(item);
  6290. }
  6291.  
  6292. // 没有再从接口获取
  6293. if (item.userInfo === undefined) {
  6294. await this.getPostInfo(item);
  6295. }
  6296.  
  6297. // 获取注册时间
  6298. const { regdate } = item.userInfo || {};
  6299.  
  6300. // 获取失败则跳过
  6301. if (regdate === undefined) {
  6302. return;
  6303. }
  6304.  
  6305. // 转换时间格式,泥潭接口只精确到秒
  6306. const date = new Date(regdate * 1000);
  6307.  
  6308. // 判断是否符合条件
  6309. if (Date.now() - date > filterRegdateLimit) {
  6310. return;
  6311. }
  6312.  
  6313. // 更新过滤模式和原因
  6314. result.mode = mode;
  6315. result.reason = `注册时间: ${date.toLocaleDateString()}`;
  6316. }
  6317.  
  6318. /**
  6319. * 根据发帖数量过滤
  6320. * @param {*} item 绑定的 nFilter
  6321. * @param {*} result 过滤结果
  6322. */
  6323. async filterByPostnum(item, result) {
  6324. const { uid } = item;
  6325.  
  6326. // 如果是匿名,则跳过
  6327. if (uid <= 0) {
  6328. return;
  6329. }
  6330.  
  6331. // 获取隐藏模式下标
  6332. const mode = this.settings.getModeByName("隐藏");
  6333.  
  6334. // 如果当前模式不低于隐藏模式,则跳过
  6335. if (result.mode >= mode) {
  6336. return;
  6337. }
  6338.  
  6339. // 获取发帖数量限制
  6340. const filterPostnumLimit = this.settings.filterPostnumLimit;
  6341.  
  6342. // 未启用则跳过
  6343. if (filterPostnumLimit <= 0) {
  6344. return;
  6345. }
  6346.  
  6347. // 没有用户信息,优先从页面上获取
  6348. if (item.userInfo === undefined) {
  6349. this.getUserInfo(item);
  6350. }
  6351.  
  6352. // 没有再从接口获取
  6353. if (item.userInfo === undefined) {
  6354. await this.getPostInfo(item);
  6355. }
  6356.  
  6357. // 获取发帖数量
  6358. const { postnum } = item.userInfo || {};
  6359.  
  6360. // 获取失败则跳过
  6361. if (postnum === undefined) {
  6362. return;
  6363. }
  6364.  
  6365. // 判断是否符合条件
  6366. if (postnum >= filterPostnumLimit) {
  6367. return;
  6368. }
  6369.  
  6370. // 更新过滤模式和原因
  6371. result.mode = mode;
  6372. result.reason = `发帖数量: ${postnum}`;
  6373. }
  6374.  
  6375. /**
  6376. * 根据发帖比例过滤
  6377. * @param {*} item 绑定的 nFilter
  6378. * @param {*} result 过滤结果
  6379. */
  6380. async filterByTopicRate(item, result) {
  6381. const { uid } = item;
  6382.  
  6383. // 如果是匿名,则跳过
  6384. if (uid <= 0) {
  6385. return;
  6386. }
  6387.  
  6388. // 获取隐藏模式下标
  6389. const mode = this.settings.getModeByName("隐藏");
  6390.  
  6391. // 如果当前模式不低于隐藏模式,则跳过
  6392. if (result.mode >= mode) {
  6393. return;
  6394. }
  6395.  
  6396. // 获取发帖比例限制
  6397. const filterTopicRateLimit = this.settings.filterTopicRateLimit;
  6398.  
  6399. // 未启用则跳过
  6400. if (filterTopicRateLimit <= 0 || filterTopicRateLimit >= 100) {
  6401. return;
  6402. }
  6403.  
  6404. // 没有用户信息,优先从页面上获取
  6405. if (item.userInfo === undefined) {
  6406. this.getUserInfo(item);
  6407. }
  6408.  
  6409. // 没有再从接口获取
  6410. if (item.userInfo === undefined) {
  6411. await this.getPostInfo(item);
  6412. }
  6413.  
  6414. // 获取发帖数量
  6415. const { postnum } = item.userInfo || {};
  6416.  
  6417. // 获取失败则跳过
  6418. if (postnum === undefined) {
  6419. return;
  6420. }
  6421.  
  6422. // 获取主题数量
  6423. const topicNum = await this.getTopicNum(item);
  6424.  
  6425. // 计算发帖比例
  6426. const topicRate = Math.ceil((topicNum / postnum) * 100);
  6427.  
  6428. // 判断是否符合条件
  6429. if (topicRate < filterTopicRateLimit) {
  6430. return;
  6431. }
  6432.  
  6433. // 更新过滤模式和原因
  6434. result.mode = mode;
  6435. result.reason = `发帖比例: ${topicRate}% (${topicNum}/${postnum})`;
  6436. }
  6437.  
  6438. /**
  6439. * 根据版面声望过滤
  6440. * @param {*} item 绑定的 nFilter
  6441. * @param {*} result 过滤结果
  6442. */
  6443. async filterByReputation(item, result) {
  6444. const { uid } = item;
  6445.  
  6446. // 如果是匿名,则跳过
  6447. if (uid <= 0) {
  6448. return;
  6449. }
  6450.  
  6451. // 获取隐藏模式下标
  6452. const mode = this.settings.getModeByName("隐藏");
  6453.  
  6454. // 如果当前模式不低于隐藏模式,则跳过
  6455. if (result.mode >= mode) {
  6456. return;
  6457. }
  6458.  
  6459. // 获取版面声望限制
  6460. const filterReputationLimit = this.settings.filterReputationLimit;
  6461.  
  6462. // 未启用则跳过
  6463. if (Number.isNaN(filterReputationLimit)) {
  6464. return;
  6465. }
  6466.  
  6467. // 没有声望信息,优先从页面上获取
  6468. if (item.reputation === undefined) {
  6469. this.getUserInfo(item);
  6470. }
  6471.  
  6472. // 没有再从接口获取
  6473. if (item.reputation === undefined) {
  6474. await this.getPostInfo(item);
  6475. }
  6476.  
  6477. // 获取版面声望
  6478. const reputation = item.reputation || 0;
  6479.  
  6480. // 判断是否符合条件
  6481. if (reputation >= filterReputationLimit) {
  6482. return;
  6483. }
  6484.  
  6485. // 更新过滤模式和原因
  6486. result.mode = mode;
  6487. result.reason = `版面声望: ${reputation}`;
  6488. }
  6489.  
  6490. /**
  6491. * 重新过滤
  6492. */
  6493. reFilter() {
  6494. this.data.forEach((item) => {
  6495. item.execute();
  6496. });
  6497. }
  6498. }
  6499.  
  6500. /**
  6501. * 设置模块
  6502. */
  6503. class SettingsModule extends Module {
  6504. /**
  6505. * 模块名称
  6506. */
  6507. static name = "settings";
  6508.  
  6509. /**
  6510. * 顺序
  6511. */
  6512. static order = 0;
  6513.  
  6514. /**
  6515. * 创建实例
  6516. * @param {Settings} settings 设置
  6517. * @param {API} api API
  6518. * @param {UI} ui UI
  6519. * @param {Array} data 过滤列表
  6520. * @returns {Module | null} 成功后返回模块实例
  6521. */
  6522. static create(settings, api, ui, data) {
  6523. // 读取设置里的模块列表
  6524. const modules = settings.modules;
  6525.  
  6526. // 如果不包含自己,加入列表中,因为设置模块是必须的
  6527. if (modules.includes(this.name) === false) {
  6528. settings.modules = [...modules, this.name];
  6529. }
  6530.  
  6531. // 创建实例
  6532. return super.create(settings, api, ui, data);
  6533. }
  6534.  
  6535. /**
  6536. * 初始化,增加设置
  6537. */
  6538. initComponents() {
  6539. super.initComponents();
  6540.  
  6541. const { settings, ui } = this;
  6542. const { add } = ui.views.settings;
  6543.  
  6544. // 前置过滤
  6545. {
  6546. const input = ui.createElement("INPUT", [], {
  6547. type: "checkbox",
  6548. });
  6549.  
  6550. const label = ui.createElement("LABEL", ["前置过滤", input], {
  6551. style: "display: flex;",
  6552. });
  6553.  
  6554. settings.preFilterEnabled.then((checked) => {
  6555. input.checked = checked;
  6556. input.onchange = () => {
  6557. settings.preFilterEnabled = !checked;
  6558. };
  6559. });
  6560.  
  6561. add(this.constructor.order + 0, label);
  6562. }
  6563.  
  6564. // 模块选择
  6565. {
  6566. const modules = [
  6567. ListModule,
  6568. UserModule,
  6569. TagModule,
  6570. KeywordModule,
  6571. LocationModule,
  6572. HunterModule,
  6573. MiscModule,
  6574. ];
  6575.  
  6576. const items = modules.map((item) => {
  6577. const input = ui.createElement("INPUT", [], {
  6578. type: "checkbox",
  6579. value: item.name,
  6580. checked: settings.modules.includes(item.name),
  6581. onchange: () => {
  6582. const checked = input.checked;
  6583.  
  6584. modules.map((m, index) => {
  6585. const isDepend = checked
  6586. ? item.depends.find((i) => i.name === m.name)
  6587. : m.depends.find((i) => i.name === item.name);
  6588.  
  6589. if (isDepend) {
  6590. const element = items[index].querySelector("INPUT");
  6591.  
  6592. if (element) {
  6593. element.checked = checked;
  6594. }
  6595. }
  6596. });
  6597. },
  6598. });
  6599.  
  6600. const label = ui.createElement("LABEL", [item.label, input], {
  6601. style: "display: flex; margin-right: 10px;",
  6602. });
  6603.  
  6604. return label;
  6605. });
  6606.  
  6607. const button = ui.createButton("确认", () => {
  6608. const checked = group.querySelectorAll("INPUT:checked");
  6609. const values = [...checked].map((item) => item.value);
  6610.  
  6611. settings.modules = values;
  6612.  
  6613. location.reload();
  6614. });
  6615.  
  6616. const group = ui.createElement("DIV", [...items, button], {
  6617. style: "display: flex;",
  6618. });
  6619.  
  6620. const label = ui.createElement("LABEL", "启用模块");
  6621.  
  6622. add(this.constructor.order + 1, label, group);
  6623. }
  6624.  
  6625. // 默认过滤模式
  6626. {
  6627. const modes = ["标记", "遮罩", "隐藏"].map((item) => {
  6628. const input = ui.createElement("INPUT", [], {
  6629. type: "radio",
  6630. name: "defaultFilterMode",
  6631. value: item,
  6632. checked: settings.defaultFilterMode === item,
  6633. onchange: () => {
  6634. settings.defaultFilterMode = item;
  6635.  
  6636. this.reFilter();
  6637. },
  6638. });
  6639.  
  6640. const label = ui.createElement("LABEL", [item, input], {
  6641. style: "display: flex; margin-right: 10px;",
  6642. });
  6643.  
  6644. return label;
  6645. });
  6646.  
  6647. const group = ui.createElement("DIV", modes, {
  6648. style: "display: flex;",
  6649. });
  6650.  
  6651. const label = ui.createElement("LABEL", "默认过滤模式");
  6652.  
  6653. const tips = ui.createElement("DIV", TIPS.filterMode, {
  6654. className: "silver",
  6655. });
  6656.  
  6657. add(this.constructor.order + 2, label, group, tips);
  6658. }
  6659. }
  6660.  
  6661. /**
  6662. * 重新过滤
  6663. */
  6664. reFilter() {
  6665. // 目前仅在修改默认过滤模式时重新过滤
  6666. this.data.forEach((item) => {
  6667. // 如果过滤模式是继承,则重新过滤
  6668. if (item.filterMode === "继承") {
  6669. item.execute();
  6670. }
  6671.  
  6672. // 如果有引用,也重新过滤
  6673. if (Object.values(item.quotes || {}).includes("继承")) {
  6674. item.execute();
  6675. return;
  6676. }
  6677. });
  6678. }
  6679. }
  6680.  
  6681. /**
  6682. * 增强的列表模块,增加了用户作为附加模块
  6683. */
  6684. class ListEnhancedModule extends ListModule {
  6685. /**
  6686. * 模块名称
  6687. */
  6688. static name = "list";
  6689.  
  6690. /**
  6691. * 附加模块
  6692. */
  6693. static addons = [UserModule];
  6694.  
  6695. /**
  6696. * 附加的用户模块
  6697. * @returns {UserModule} 用户模块
  6698. */
  6699. get userModule() {
  6700. return this.addons[UserModule.name];
  6701. }
  6702.  
  6703. /**
  6704. * 表格列
  6705. * @returns {Array} 表格列集合
  6706. */
  6707. columns() {
  6708. const hasAddon = this.hasAddon(UserModule);
  6709.  
  6710. if (hasAddon === false) {
  6711. return super.columns();
  6712. }
  6713.  
  6714. return [
  6715. { label: "用户", width: 1 },
  6716. { label: "内容", ellipsis: true },
  6717. { label: "过滤模式", center: true, width: 1 },
  6718. { label: "原因", width: 1 },
  6719. { label: "操作", width: 1 },
  6720. ];
  6721. }
  6722.  
  6723. /**
  6724. * 表格项
  6725. * @param {*} item 绑定的 nFilter
  6726. * @returns {Array} 表格项集合
  6727. */
  6728. column(item) {
  6729. const column = super.column(item);
  6730.  
  6731. const hasAddon = this.hasAddon(UserModule);
  6732.  
  6733. if (hasAddon === false) {
  6734. return column;
  6735. }
  6736.  
  6737. const { ui } = this;
  6738. const { table } = this.views;
  6739. const { uid, username } = item;
  6740.  
  6741. const user = this.userModule.format(uid, username);
  6742.  
  6743. const buttons = (() => {
  6744. if (uid <= 0) {
  6745. return null;
  6746. }
  6747.  
  6748. const block = ui.createButton("屏蔽", (e) => {
  6749. this.userModule.renderDetails(uid, username, (type) => {
  6750. // 删除失效数据,等待重新过滤
  6751. table.remove(e);
  6752.  
  6753. // 如果是新增,不会因为用户重新过滤,需要主动触发
  6754. if (type === "ADD") {
  6755. this.userModule.reFilter(uid);
  6756. }
  6757. });
  6758. });
  6759.  
  6760. return ui.createButtonGroup(block);
  6761. })();
  6762.  
  6763. return [user, ...column, buttons];
  6764. }
  6765. }
  6766.  
  6767. /**
  6768. * 增强的用户模块,增加了标记作为附加模块
  6769. */
  6770. class UserEnhancedModule extends UserModule {
  6771. /**
  6772. * 模块名称
  6773. */
  6774. static name = "user";
  6775.  
  6776. /**
  6777. * 附加模块
  6778. */
  6779. static addons = [TagModule];
  6780.  
  6781. /**
  6782. * 附加的标记模块
  6783. * @returns {TagModule} 标记模块
  6784. */
  6785. get tagModule() {
  6786. return this.addons[TagModule.name];
  6787. }
  6788.  
  6789. /**
  6790. * 表格列
  6791. * @returns {Array} 表格列集合
  6792. */
  6793. columns() {
  6794. const hasAddon = this.hasAddon(TagModule);
  6795.  
  6796. if (hasAddon === false) {
  6797. return super.columns();
  6798. }
  6799.  
  6800. return [
  6801. { label: "昵称", width: 1 },
  6802. { label: "标记" },
  6803. { label: "过滤模式", center: true, width: 1 },
  6804. { label: "操作", width: 1 },
  6805. ];
  6806. }
  6807.  
  6808. /**
  6809. * 表格项
  6810. * @param {*} item 用户信息
  6811. * @returns {Array} 表格项集合
  6812. */
  6813. column(item) {
  6814. const column = super.column(item);
  6815.  
  6816. const hasAddon = this.hasAddon(TagModule);
  6817.  
  6818. if (hasAddon === false) {
  6819. return column;
  6820. }
  6821.  
  6822. const { ui } = this;
  6823. const { table } = this.views;
  6824. const { id, name } = item;
  6825.  
  6826. const tags = ui.createElement(
  6827. "DIV",
  6828. item.tags.map((id) => this.tagModule.format(id))
  6829. );
  6830.  
  6831. const newColumn = [...column];
  6832.  
  6833. newColumn.splice(1, 0, tags);
  6834.  
  6835. const buttons = column[column.length - 1];
  6836.  
  6837. const update = ui.createButton("编辑", (e) => {
  6838. this.renderDetails(id, name, (type, newValue) => {
  6839. if (type === "UPDATE") {
  6840. table.update(e, ...this.column(newValue));
  6841. }
  6842.  
  6843. if (type === "REMOVE") {
  6844. table.remove(e);
  6845. }
  6846. });
  6847. });
  6848.  
  6849. buttons.insertBefore(update, buttons.firstChild);
  6850.  
  6851. return newColumn;
  6852. }
  6853.  
  6854. /**
  6855. * 渲染详情
  6856. * @param {Number} uid 用户 ID
  6857. * @param {String | undefined} name 用户名称
  6858. * @param {Function} callback 回调函数
  6859. */
  6860. renderDetails(uid, name, callback = () => {}) {
  6861. const hasAddon = this.hasAddon(TagModule);
  6862.  
  6863. if (hasAddon === false) {
  6864. return super.renderDetails(uid, name, callback);
  6865. }
  6866.  
  6867. const { ui, settings } = this;
  6868.  
  6869. // 只允许同时存在一个详情页
  6870. if (this.views.details) {
  6871. if (this.views.details.parentNode) {
  6872. this.views.details.parentNode.removeChild(this.views.details);
  6873. }
  6874. }
  6875.  
  6876. // 获取用户信息
  6877. const user = this.get(uid);
  6878.  
  6879. if (user) {
  6880. name = user.name;
  6881. }
  6882.  
  6883. // TODO 需要优化
  6884.  
  6885. const title =
  6886. (user ? "编辑" : "添加") + `用户 - ${name ? name : "#" + uid}`;
  6887.  
  6888. const table = ui.createTable([]);
  6889.  
  6890. {
  6891. const size = Math.floor((screen.width * 0.8) / 200);
  6892.  
  6893. const items = Object.values(this.tagModule.list).map(({ id }) => {
  6894. const checked = user && user.tags.includes(id) ? "checked" : "";
  6895.  
  6896. return `
  6897. <td class="c1">
  6898. <label for="s-tag-${id}" style="display: block; cursor: pointer;">
  6899. ${this.tagModule.format(id).outerHTML}
  6900. </label>
  6901. </td>
  6902. <td class="c2" width="1">
  6903. <input id="s-tag-${id}" type="checkbox" value="${id}" ${checked}/>
  6904. </td>
  6905. `;
  6906. });
  6907.  
  6908. const rows = [...new Array(Math.ceil(items.length / size))].map(
  6909. (_, index) => `
  6910. <tr class="row${(index % 2) + 1}">
  6911. ${items.slice(size * index, size * (index + 1)).join("")}
  6912. </tr>
  6913. `
  6914. );
  6915.  
  6916. table.querySelector("TBODY").innerHTML = rows.join("");
  6917. }
  6918.  
  6919. const input = ui.createElement("INPUT", [], {
  6920. type: "text",
  6921. placeholder: TIPS.addTags,
  6922. style: "width: -webkit-fill-available;",
  6923. });
  6924.  
  6925. const inputWrapper = ui.createElement("DIV", input, {
  6926. style: "margin-top: 10px;",
  6927. });
  6928.  
  6929. const filterMode = user ? user.filterMode : settings.filterModes[0];
  6930.  
  6931. const switchMode = ui.createButton(filterMode, () => {
  6932. const newMode = settings.switchModeByName(switchMode.innerText);
  6933.  
  6934. switchMode.innerText = newMode;
  6935. });
  6936.  
  6937. const buttons = ui.createElement(
  6938. "DIV",
  6939. (() => {
  6940. const remove = user
  6941. ? ui.createButton("删除", () => {
  6942. ui.confirm().then(() => {
  6943. this.remove(uid);
  6944.  
  6945. this.views.details._.hide();
  6946.  
  6947. callback("REMOVE");
  6948. });
  6949. })
  6950. : null;
  6951.  
  6952. const save = ui.createButton("保存", () => {
  6953. const checked = [...table.querySelectorAll("INPUT:checked")].map(
  6954. (input) => parseInt(input.value, 10)
  6955. );
  6956.  
  6957. const newTags = input.value
  6958. .split("|")
  6959. .filter((item) => item.length)
  6960. .map((item) => this.tagModule.add(item))
  6961. .filter((tag) => tag !== null)
  6962. .map((tag) => tag.id);
  6963.  
  6964. const tags = [...new Set([...checked, ...newTags])].sort();
  6965.  
  6966. if (user === null) {
  6967. const entity = this.add(uid, {
  6968. id: uid,
  6969. name,
  6970. tags,
  6971. filterMode: switchMode.innerText,
  6972. });
  6973.  
  6974. this.views.details._.hide();
  6975.  
  6976. callback("ADD", entity);
  6977. } else {
  6978. const entity = this.update(uid, {
  6979. name,
  6980. tags,
  6981. filterMode: switchMode.innerText,
  6982. });
  6983.  
  6984. this.views.details._.hide();
  6985.  
  6986. callback("UPDATE", entity);
  6987. }
  6988. });
  6989.  
  6990. return ui.createButtonGroup(remove, save);
  6991. })(),
  6992. {
  6993. className: "right_",
  6994. }
  6995. );
  6996.  
  6997. const actions = ui.createElement(
  6998. "DIV",
  6999. [ui.createElement("SPAN", "过滤模式:"), switchMode, buttons],
  7000. {
  7001. style: "margin-top: 10px;",
  7002. }
  7003. );
  7004.  
  7005. const tips = ui.createElement("DIV", TIPS.filterMode, {
  7006. className: "silver",
  7007. style: "margin-top: 10px;",
  7008. });
  7009.  
  7010. const content = ui.createElement(
  7011. "DIV",
  7012. [table, inputWrapper, actions, tips],
  7013. {
  7014. style: "width: 80vw",
  7015. }
  7016. );
  7017.  
  7018. // 创建弹出框
  7019. this.views.details = ui.createDialog(null, title, content);
  7020. }
  7021. }
  7022.  
  7023. /**
  7024. * 处理 topicArg 模块
  7025. * @param {Filter} filter 过滤器
  7026. * @param {*} value commonui.topicArg
  7027. */
  7028. const handleTopicModule = async (filter, value) => {
  7029. // 绑定主题模块
  7030. topicModule = value;
  7031.  
  7032. // 是否启用前置过滤
  7033. const preFilterEnabled = await filter.settings.preFilterEnabled;
  7034.  
  7035. // 前置过滤
  7036. // 先直接隐藏,等过滤完毕后再放出来
  7037. const beforeGet = (...args) => {
  7038. if (preFilterEnabled) {
  7039. // 主题标题
  7040. const title = document.getElementById(args[1]);
  7041.  
  7042. // 主题容器
  7043. const container = title.closest("tr");
  7044.  
  7045. // 隐藏元素
  7046. container.style.display = "none";
  7047. }
  7048.  
  7049. return args;
  7050. };
  7051.  
  7052. // 过滤
  7053. const afterGet = (_, args) => {
  7054. // 主题 ID
  7055. const tid = args[8];
  7056.  
  7057. // 找到对应数据
  7058. const data = topicModule.data.find((item) => item[8] === tid);
  7059.  
  7060. // 开始过滤
  7061. if (data) {
  7062. filter.filterTopic(data);
  7063. }
  7064. };
  7065.  
  7066. // 如果已经有数据,则直接过滤
  7067. Object.values(topicModule.data).forEach(filter.filterTopic);
  7068.  
  7069. // 拦截 add 函数,这是泥潭的主题添加事件
  7070. Tools.interceptProperty(topicModule, "add", {
  7071. beforeGet,
  7072. afterGet,
  7073. });
  7074. };
  7075.  
  7076. /**
  7077. * 处理 postArg 模块
  7078. * @param {Filter} filter 过滤器
  7079. * @param {*} value commonui.postArg
  7080. */
  7081. const handleReplyModule = async (filter, value) => {
  7082. // 绑定回复模块
  7083. replyModule = value;
  7084.  
  7085. // 是否启用前置过滤
  7086. const preFilterEnabled = await filter.settings.preFilterEnabled;
  7087.  
  7088. // 前置过滤
  7089. // 先直接隐藏,等过滤完毕后再放出来
  7090. const beforeGet = (...args) => {
  7091. if (preFilterEnabled) {
  7092. // 楼层号
  7093. const index = args[0];
  7094.  
  7095. // 判断是否是楼层
  7096. const isFloor = typeof index === "number";
  7097.  
  7098. // 评论额外标签
  7099. const prefix = isFloor ? "" : "comment";
  7100.  
  7101. // 用户容器
  7102. const uInfoC = document.querySelector(`#${prefix}posterinfo${index}`);
  7103.  
  7104. // 回复容器
  7105. const container = isFloor
  7106. ? uInfoC.closest("tr")
  7107. : uInfoC.closest(".comment_c");
  7108.  
  7109. // 隐藏元素
  7110. container.style.display = "none";
  7111. }
  7112.  
  7113. return args;
  7114. };
  7115.  
  7116. // 过滤
  7117. const afterGet = (_, args) => {
  7118. // 楼层号
  7119. const index = args[0];
  7120.  
  7121. // 找到对应数据
  7122. const data = replyModule.data[index];
  7123.  
  7124. // 开始过滤
  7125. if (data) {
  7126. filter.filterReply(data);
  7127. }
  7128. };
  7129.  
  7130. // 如果已经有数据,则直接过滤
  7131. Object.values(replyModule.data).forEach(filter.filterReply);
  7132.  
  7133. // 拦截 proc 函数,这是泥潭的回复添加事件
  7134. Tools.interceptProperty(replyModule, "proc", {
  7135. beforeGet,
  7136. afterGet,
  7137. });
  7138. };
  7139.  
  7140. /**
  7141. * 处理 commonui 模块
  7142. * @param {Filter} filter 过滤器
  7143. * @param {*} value commonui
  7144. */
  7145. const handleCommonui = (filter, value) => {
  7146. // 绑定主模块
  7147. commonui = value;
  7148.  
  7149. // 拦截 mainMenu 模块,UI 需要在 init 后加载
  7150. Tools.interceptProperty(commonui, "mainMenu", {
  7151. afterSet: (value) => {
  7152. Tools.interceptProperty(value, "init", {
  7153. afterGet: () => {
  7154. filter.ui.render();
  7155. },
  7156. afterSet: () => {
  7157. filter.ui.render();
  7158. },
  7159. });
  7160. },
  7161. });
  7162.  
  7163. // 拦截 topicArg 模块,这是泥潭的主题入口
  7164. Tools.interceptProperty(commonui, "topicArg", {
  7165. afterSet: (value) => {
  7166. handleTopicModule(filter, value);
  7167. },
  7168. });
  7169.  
  7170. // 拦截 postArg 模块,这是泥潭的回复入口
  7171. Tools.interceptProperty(commonui, "postArg", {
  7172. afterSet: (value) => {
  7173. handleReplyModule(filter, value);
  7174. },
  7175. });
  7176. };
  7177.  
  7178. /**
  7179. * 注册脚本菜单
  7180. * @param {Settings} settings 设置
  7181. */
  7182. const registerMenu = async (settings) => {
  7183. // 修改 UA
  7184. {
  7185. const userAgent = await settings.userAgent;
  7186.  
  7187. GM_registerMenuCommand(`修改UA${userAgent}`, () => {
  7188. const value = prompt("修改UA", userAgent);
  7189.  
  7190. if (value) {
  7191. settings.userAgent = value;
  7192. }
  7193. });
  7194. }
  7195.  
  7196. // 前置过滤
  7197. {
  7198. const enabled = await settings.preFilterEnabled;
  7199.  
  7200. GM_registerMenuCommand(`前置过滤:${enabled ? "是" : "否"}`, () => {
  7201. settings.preFilterEnabled = !enabled;
  7202. });
  7203. }
  7204. };
  7205.  
  7206. // 主函数
  7207. (async () => {
  7208. // 初始化缓存、设置
  7209. const cache = new Cache(API.modules);
  7210. const settings = new Settings(cache);
  7211.  
  7212. // 读取设置
  7213. await settings.load();
  7214.  
  7215. // 初始化 API、UI
  7216. const api = new API(cache, settings);
  7217. const ui = new UI(settings, api);
  7218.  
  7219. // 初始化过滤器
  7220. const filter = new Filter(settings, api, ui);
  7221.  
  7222. // 加载模块
  7223. filter.initModules(
  7224. SettingsModule,
  7225. ListEnhancedModule,
  7226. UserEnhancedModule,
  7227. TagModule,
  7228. KeywordModule,
  7229. LocationModule,
  7230. HunterModule,
  7231. MiscModule
  7232. );
  7233.  
  7234. // 注册脚本菜单
  7235. registerMenu(settings);
  7236.  
  7237. // 处理 commonui 模块
  7238. if (unsafeWindow.commonui) {
  7239. handleCommonui(filter, unsafeWindow.commonui);
  7240. return;
  7241. }
  7242.  
  7243. Tools.interceptProperty(unsafeWindow, "commonui", {
  7244. afterSet: (value) => {
  7245. handleCommonui(filter, value);
  7246. },
  7247. });
  7248. })();
  7249. })();