NGA Filter

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

当前为 2024-01-22 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name NGA Filter
  3. // @namespace https://greasyfork.org/users/263018
  4. // @version 2.2.4
  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 content = (() => {
  3411. if (pid) {
  3412. return ui.createElement("A", item.content, {
  3413. href: `/read.php?pid=${pid}&nofilter`,
  3414. });
  3415. }
  3416.  
  3417. // 如果有 TID 但没有标题,是引用,采用内容逻辑
  3418. if (item.subject.length === 0) {
  3419. return ui.createElement("A", item.content, {
  3420. href: `/read.php?tid=${tid}&nofilter`,
  3421. });
  3422. }
  3423.  
  3424. return ui.createElement("A", item.subject, {
  3425. href: `/read.php?tid=${tid}&nofilter`,
  3426. title: item.content,
  3427. className: "b nobr",
  3428. });
  3429. })();
  3430.  
  3431. return [content, filterMode, reason];
  3432. }
  3433.  
  3434. /**
  3435. * 初始化组件
  3436. */
  3437. initComponents() {
  3438. super.initComponents();
  3439.  
  3440. const { tabs, content } = this.ui.views;
  3441.  
  3442. const table = this.ui.createTable(this.columns());
  3443.  
  3444. const tab = this.ui.createTab(
  3445. tabs,
  3446. this.constructor.label,
  3447. this.constructor.order,
  3448. {
  3449. onclick: () => {
  3450. this.render(content);
  3451. },
  3452. }
  3453. );
  3454.  
  3455. Object.assign(this.views, {
  3456. tab,
  3457. table,
  3458. });
  3459.  
  3460. this.views.container.appendChild(table);
  3461. }
  3462.  
  3463. /**
  3464. * 渲染
  3465. * @param {HTMLElement} container 容器
  3466. */
  3467. render(container) {
  3468. super.render(container);
  3469.  
  3470. const { table } = this.views;
  3471.  
  3472. if (table) {
  3473. const { add, clear } = table;
  3474.  
  3475. clear();
  3476.  
  3477. const list = this.data.filter((item) => {
  3478. return (item.filterMode || "显示") !== "显示";
  3479. });
  3480.  
  3481. Object.values(list).forEach((item) => {
  3482. const column = this.column(item);
  3483.  
  3484. add(...column);
  3485. });
  3486. }
  3487. }
  3488.  
  3489. /**
  3490. * 通知
  3491. * @param {*} item 绑定的 nFilter
  3492. */
  3493. async notify() {
  3494. // 获取过滤后的数量
  3495. const count = this.data.filter((item) => {
  3496. return (item.filterMode || "显示") !== "显示";
  3497. }).length;
  3498.  
  3499. // 更新菜单文字
  3500. const { ui } = this;
  3501. const { menu } = ui;
  3502.  
  3503. if (menu === null) {
  3504. return;
  3505. }
  3506.  
  3507. if (count) {
  3508. menu.innerHTML = `${ui.constructor.label} <span class="small_colored_text_btn stxt block_txt_c0 vertmod">${count}</span>`;
  3509. } else {
  3510. menu.innerHTML = `${ui.constructor.label}`;
  3511. }
  3512.  
  3513. // 重新渲染
  3514. // TODO 应该给 table 增加一个判重的逻辑,这样只需要更新过滤后的内容即可
  3515. const { tab } = this.views;
  3516.  
  3517. if (tab.querySelector("A").className === "nobr") {
  3518. this.render(ui.views.content);
  3519. }
  3520. }
  3521. }
  3522.  
  3523. /**
  3524. * 用户模块
  3525. */
  3526. class UserModule extends Module {
  3527. /**
  3528. * 模块名称
  3529. */
  3530. static name = "user";
  3531.  
  3532. /**
  3533. * 模块标签
  3534. */
  3535. static label = "用户";
  3536.  
  3537. /**
  3538. * 顺序
  3539. */
  3540. static order = 20;
  3541.  
  3542. /**
  3543. * 获取列表
  3544. */
  3545. get list() {
  3546. return this.settings.users;
  3547. }
  3548.  
  3549. /**
  3550. * 获取用户
  3551. * @param {Number} uid 用户 ID
  3552. */
  3553. get(uid) {
  3554. // 获取列表
  3555. const list = this.list;
  3556.  
  3557. // 如果存在,则返回信息
  3558. if (list[uid]) {
  3559. return list[uid];
  3560. }
  3561.  
  3562. return null;
  3563. }
  3564.  
  3565. /**
  3566. * 添加用户
  3567. * @param {Number} uid 用户 ID
  3568. */
  3569. add(uid, values) {
  3570. // 获取列表
  3571. const list = this.list;
  3572.  
  3573. // 如果已存在,则返回信息
  3574. if (list[uid]) {
  3575. return list[uid];
  3576. }
  3577.  
  3578. // 写入用户信息
  3579. list[uid] = values;
  3580.  
  3581. // 保存数据
  3582. this.settings.users = list;
  3583.  
  3584. // 重新过滤
  3585. this.reFilter(uid);
  3586.  
  3587. // 返回添加的用户
  3588. return values;
  3589. }
  3590.  
  3591. /**
  3592. * 编辑用户
  3593. * @param {Number} uid 用户 ID
  3594. * @param {*} values 用户信息
  3595. */
  3596. update(uid, values) {
  3597. // 获取列表
  3598. const list = this.list;
  3599.  
  3600. // 如果不存在则跳过
  3601. if (Object.hasOwn(list, uid) === false) {
  3602. return null;
  3603. }
  3604.  
  3605. // 获取用户
  3606. const entity = list[uid];
  3607.  
  3608. // 更新用户
  3609. Object.assign(entity, values);
  3610.  
  3611. // 保存数据
  3612. this.settings.users = list;
  3613.  
  3614. // 重新过滤
  3615. this.reFilter(uid);
  3616.  
  3617. // 返回编辑的用户
  3618. return entity;
  3619. }
  3620.  
  3621. /**
  3622. * 删除用户
  3623. * @param {Number} uid 用户 ID
  3624. * @returns {Object | null} 删除的用户
  3625. */
  3626. remove(uid) {
  3627. // 获取列表
  3628. const list = this.list;
  3629.  
  3630. // 如果不存在则跳过
  3631. if (Object.hasOwn(list, uid) === false) {
  3632. return null;
  3633. }
  3634.  
  3635. // 获取用户
  3636. const entity = list[uid];
  3637.  
  3638. // 删除用户
  3639. delete list[uid];
  3640.  
  3641. // 保存数据
  3642. this.settings.users = list;
  3643.  
  3644. // 重新过滤
  3645. this.reFilter(uid);
  3646.  
  3647. // 返回删除的用户
  3648. return entity;
  3649. }
  3650.  
  3651. /**
  3652. * 格式化
  3653. * @param {Number} uid 用户 ID
  3654. * @param {String | undefined} name 用户名称
  3655. */
  3656. format(uid, name) {
  3657. if (uid <= 0) {
  3658. return null;
  3659. }
  3660.  
  3661. const { ui } = this;
  3662.  
  3663. const user = this.get(uid);
  3664.  
  3665. if (user) {
  3666. name = user.name;
  3667. }
  3668.  
  3669. const username = name ? "@" + name : "#" + uid;
  3670.  
  3671. return ui.createElement("A", `[${username}]`, {
  3672. className: "b nobr",
  3673. href: `/nuke.php?func=ucp&uid=${uid}`,
  3674. });
  3675. }
  3676.  
  3677. /**
  3678. * 表格列
  3679. * @returns {Array} 表格列集合
  3680. */
  3681. columns() {
  3682. return [
  3683. { label: "昵称" },
  3684. { label: "过滤模式", center: true, width: 1 },
  3685. { label: "操作", width: 1 },
  3686. ];
  3687. }
  3688.  
  3689. /**
  3690. * 表格项
  3691. * @param {*} item 用户信息
  3692. * @returns {Array} 表格项集合
  3693. */
  3694. column(item) {
  3695. const { ui } = this;
  3696. const { table } = this.views;
  3697. const { id, name, filterMode } = item;
  3698.  
  3699. // 昵称
  3700. const user = this.format(id, name);
  3701.  
  3702. // 切换过滤模式
  3703. const switchMode = ui.createButton(
  3704. filterMode || this.settings.filterModes[0],
  3705. () => {
  3706. const newMode = this.settings.switchModeByName(switchMode.innerText);
  3707.  
  3708. this.update(id, {
  3709. filterMode: newMode,
  3710. });
  3711.  
  3712. switchMode.innerText = newMode;
  3713. }
  3714. );
  3715.  
  3716. // 操作
  3717. const buttons = (() => {
  3718. const remove = ui.createButton("删除", (e) => {
  3719. ui.confirm().then(() => {
  3720. this.remove(id);
  3721.  
  3722. table.remove(e);
  3723. });
  3724. });
  3725.  
  3726. return ui.createButtonGroup(remove);
  3727. })();
  3728.  
  3729. return [user, switchMode, buttons];
  3730. }
  3731.  
  3732. /**
  3733. * 初始化组件
  3734. */
  3735. initComponents() {
  3736. super.initComponents();
  3737.  
  3738. const { ui } = this;
  3739. const { tabs, content, settings } = ui.views;
  3740. const { add } = settings;
  3741.  
  3742. const table = ui.createTable(this.columns());
  3743.  
  3744. const tab = ui.createTab(
  3745. tabs,
  3746. this.constructor.label,
  3747. this.constructor.order,
  3748. {
  3749. onclick: () => {
  3750. this.render(content);
  3751. },
  3752. }
  3753. );
  3754.  
  3755. Object.assign(this.views, {
  3756. tab,
  3757. table,
  3758. });
  3759.  
  3760. this.views.container.appendChild(table);
  3761.  
  3762. // 删除非激活中的用户
  3763. {
  3764. const list = ui.createElement("DIV", [], {
  3765. style: "white-space: normal;",
  3766. });
  3767.  
  3768. const button = ui.createButton("删除非激活中的用户", () => {
  3769. ui.confirm().then(() => {
  3770. list.innerHTML = "";
  3771.  
  3772. const users = Object.values(this.list);
  3773.  
  3774. const waitingQueue = users.map(
  3775. ({ id }) =>
  3776. () =>
  3777. this.api.getUserInfo(id).then(({ bit }) => {
  3778. const activeInfo = commonui.activeInfo(0, 0, bit);
  3779. const activeType = activeInfo[1];
  3780.  
  3781. if (["ACTIVED", "LINKED"].includes(activeType)) {
  3782. return;
  3783. }
  3784.  
  3785. list.append(this.format(id));
  3786.  
  3787. this.remove(id);
  3788. })
  3789. );
  3790.  
  3791. const queueLength = waitingQueue.length;
  3792.  
  3793. const execute = () => {
  3794. if (waitingQueue.length) {
  3795. const next = waitingQueue.shift();
  3796.  
  3797. button.disabled = true;
  3798. button.innerHTML = `删除非激活中的用户 (${
  3799. queueLength - waitingQueue.length
  3800. }/${queueLength})`;
  3801.  
  3802. next().finally(execute);
  3803. return;
  3804. }
  3805.  
  3806. button.disabled = false;
  3807. };
  3808.  
  3809. execute();
  3810. });
  3811. });
  3812.  
  3813. const element = ui.createElement("DIV", [button, list]);
  3814.  
  3815. add(this.constructor.order + 0, element);
  3816. }
  3817. }
  3818.  
  3819. /**
  3820. * 渲染
  3821. * @param {HTMLElement} container 容器
  3822. */
  3823. render(container) {
  3824. super.render(container);
  3825.  
  3826. const { table } = this.views;
  3827.  
  3828. if (table) {
  3829. const { add, clear } = table;
  3830.  
  3831. clear();
  3832.  
  3833. Object.values(this.list).forEach((item) => {
  3834. const column = this.column(item);
  3835.  
  3836. add(...column);
  3837. });
  3838. }
  3839. }
  3840.  
  3841. /**
  3842. * 渲染详情
  3843. * @param {Number} uid 用户 ID
  3844. * @param {String | undefined} name 用户名称
  3845. * @param {Function} callback 回调函数
  3846. */
  3847. renderDetails(uid, name, callback = () => {}) {
  3848. const { ui, settings } = this;
  3849.  
  3850. // 只允许同时存在一个详情页
  3851. if (this.views.details) {
  3852. if (this.views.details.parentNode) {
  3853. this.views.details.parentNode.removeChild(this.views.details);
  3854. }
  3855. }
  3856.  
  3857. // 获取用户信息
  3858. const user = this.get(uid);
  3859.  
  3860. if (user) {
  3861. name = user.name;
  3862. }
  3863.  
  3864. const title =
  3865. (user ? "编辑" : "添加") + `用户 - ${name ? name : "#" + uid}`;
  3866.  
  3867. const filterMode = user ? user.filterMode : settings.filterModes[0];
  3868.  
  3869. const switchMode = ui.createButton(filterMode, () => {
  3870. const newMode = settings.switchModeByName(switchMode.innerText);
  3871.  
  3872. switchMode.innerText = newMode;
  3873. });
  3874.  
  3875. const buttons = ui.createElement(
  3876. "DIV",
  3877. (() => {
  3878. const remove = user
  3879. ? ui.createButton("删除", () => {
  3880. ui.confirm().then(() => {
  3881. this.remove(uid);
  3882.  
  3883. this.views.details._.hide();
  3884.  
  3885. callback("REMOVE");
  3886. });
  3887. })
  3888. : null;
  3889.  
  3890. const save = ui.createButton("保存", () => {
  3891. if (user === null) {
  3892. const entity = this.add(uid, {
  3893. id: uid,
  3894. name,
  3895. tags: [],
  3896. filterMode: switchMode.innerText,
  3897. });
  3898.  
  3899. this.views.details._.hide();
  3900.  
  3901. callback("ADD", entity);
  3902. } else {
  3903. const entity = this.update(uid, {
  3904. name,
  3905. filterMode: switchMode.innerText,
  3906. });
  3907.  
  3908. this.views.details._.hide();
  3909.  
  3910. callback("UPDATE", entity);
  3911. }
  3912. });
  3913.  
  3914. return ui.createButtonGroup(remove, save);
  3915. })(),
  3916. {
  3917. className: "right_",
  3918. }
  3919. );
  3920.  
  3921. const actions = ui.createElement(
  3922. "DIV",
  3923. [ui.createElement("SPAN", "过滤模式:"), switchMode, buttons],
  3924. {
  3925. style: "margin-top: 10px;",
  3926. }
  3927. );
  3928.  
  3929. const tips = ui.createElement("DIV", TIPS.filterMode, {
  3930. className: "silver",
  3931. style: "margin-top: 10px;",
  3932. });
  3933.  
  3934. const content = ui.createElement("DIV", [actions, tips], {
  3935. style: "width: 80vw",
  3936. });
  3937.  
  3938. // 创建弹出框
  3939. this.views.details = ui.createDialog(null, title, content);
  3940. }
  3941.  
  3942. /**
  3943. * 过滤
  3944. * @param {*} item 绑定的 nFilter
  3945. * @param {*} result 过滤结果
  3946. */
  3947. async filter(item, result) {
  3948. // 获取用户信息
  3949. const user = this.get(item.uid);
  3950.  
  3951. // 没有则跳过
  3952. if (user === null) {
  3953. return;
  3954. }
  3955.  
  3956. // 获取用户过滤模式
  3957. const mode = this.settings.getModeByName(user.filterMode);
  3958.  
  3959. // 不高于当前过滤模式则跳过
  3960. if (mode <= result.mode) {
  3961. return;
  3962. }
  3963.  
  3964. // 更新过滤模式和原因
  3965. result.mode = mode;
  3966. result.reason = `用户模式: ${user.filterMode}`;
  3967. }
  3968.  
  3969. /**
  3970. * 通知
  3971. * @param {*} item 绑定的 nFilter
  3972. */
  3973. async notify(item) {
  3974. const { uid, username, action } = item;
  3975.  
  3976. // 如果没有 action 组件则跳过
  3977. if (action === null) {
  3978. return;
  3979. }
  3980.  
  3981. // 如果是匿名,隐藏组件
  3982. if (uid <= 0) {
  3983. action.style.display = "none";
  3984. return;
  3985. }
  3986.  
  3987. // 获取当前用户
  3988. const user = this.get(uid);
  3989.  
  3990. // 修改操作按钮文字
  3991. action.innerText = "屏蔽";
  3992.  
  3993. // 修改操作按钮颜色
  3994. if (user) {
  3995. action.style.background = "#CB4042";
  3996. } else {
  3997. action.style.background = "#AAA";
  3998. }
  3999.  
  4000. // 绑定事件
  4001. action.onclick = () => {
  4002. this.renderDetails(uid, username);
  4003. };
  4004. }
  4005.  
  4006. /**
  4007. * 重新过滤
  4008. * @param {Number} uid 用户 ID
  4009. */
  4010. reFilter(uid) {
  4011. this.data.forEach((item) => {
  4012. // 如果用户 ID 一致,则重新过滤
  4013. if (item.uid === uid) {
  4014. item.execute();
  4015. return;
  4016. }
  4017.  
  4018. // 如果有引用,也重新过滤
  4019. if (Object.hasOwn(item.quotes || {}, uid)) {
  4020. item.execute();
  4021. return;
  4022. }
  4023. });
  4024. }
  4025. }
  4026.  
  4027. /**
  4028. * 标记模块
  4029. */
  4030. class TagModule extends Module {
  4031. /**
  4032. * 模块名称
  4033. */
  4034. static name = "tag";
  4035.  
  4036. /**
  4037. * 模块标签
  4038. */
  4039. static label = "标记";
  4040.  
  4041. /**
  4042. * 顺序
  4043. */
  4044. static order = 30;
  4045.  
  4046. /**
  4047. * 依赖模块
  4048. */
  4049. static depends = [UserModule];
  4050.  
  4051. /**
  4052. * 依赖的用户模块
  4053. * @returns {UserModule} 用户模块
  4054. */
  4055. get userModule() {
  4056. return this.depends[UserModule.name];
  4057. }
  4058.  
  4059. /**
  4060. * 获取列表
  4061. */
  4062. get list() {
  4063. return this.settings.tags;
  4064. }
  4065.  
  4066. /**
  4067. * 获取标记
  4068. * @param {Number} id 标记 ID
  4069. * @param {String} name 标记名称
  4070. */
  4071. get({ id, name }) {
  4072. // 获取列表
  4073. const list = this.list;
  4074.  
  4075. // 通过 ID 获取标记
  4076. if (list[id]) {
  4077. return list[id];
  4078. }
  4079.  
  4080. // 通过名称获取标记
  4081. if (name) {
  4082. const tag = Object.values(list).find((item) => item.name === name);
  4083.  
  4084. if (tag) {
  4085. return tag;
  4086. }
  4087. }
  4088.  
  4089. return null;
  4090. }
  4091.  
  4092. /**
  4093. * 添加标记
  4094. * @param {String} name 标记名称
  4095. */
  4096. add(name) {
  4097. // 获取对应的标记
  4098. const tag = this.get({ name });
  4099.  
  4100. // 如果标记已存在,则返回标记信息,否则增加标记
  4101. if (tag) {
  4102. return tag;
  4103. }
  4104.  
  4105. // 获取列表
  4106. const list = this.list;
  4107.  
  4108. // ID 为最大值 + 1
  4109. const id = Math.max(...Object.keys(list), 0) + 1;
  4110.  
  4111. // 标记的颜色
  4112. const color = Tools.generateColor(name);
  4113.  
  4114. // 写入标记信息
  4115. list[id] = {
  4116. id,
  4117. name,
  4118. color,
  4119. filterMode: this.settings.filterModes[0],
  4120. };
  4121.  
  4122. // 保存数据
  4123. this.settings.tags = list;
  4124.  
  4125. // 返回添加的标记
  4126. return list[id];
  4127. }
  4128.  
  4129. /**
  4130. * 编辑标记
  4131. * @param {Number} id 标记 ID
  4132. * @param {*} values 标记信息
  4133. */
  4134. update(id, values) {
  4135. // 获取列表
  4136. const list = this.list;
  4137.  
  4138. // 如果不存在则跳过
  4139. if (Object.hasOwn(list, id) === false) {
  4140. return null;
  4141. }
  4142.  
  4143. // 获取标记
  4144. const entity = list[id];
  4145.  
  4146. // 获取相关的用户
  4147. const users = Object.values(this.userModule.list).filter((user) =>
  4148. user.tags.includes(id)
  4149. );
  4150.  
  4151. // 更新标记
  4152. Object.assign(entity, values);
  4153.  
  4154. // 保存数据
  4155. this.settings.tags = list;
  4156.  
  4157. // 重新过滤
  4158. this.reFilter(users);
  4159. }
  4160.  
  4161. /**
  4162. * 删除标记
  4163. * @param {Number} id 标记 ID
  4164. */
  4165. remove(id) {
  4166. // 获取列表
  4167. const list = this.list;
  4168.  
  4169. // 如果不存在则跳过
  4170. if (Object.hasOwn(list, id) === false) {
  4171. return null;
  4172. }
  4173.  
  4174. // 获取标记
  4175. const entity = list[id];
  4176.  
  4177. // 获取相关的用户
  4178. const users = Object.values(this.userModule.list).filter((user) =>
  4179. user.tags.includes(id)
  4180. );
  4181.  
  4182. // 删除标记
  4183. delete list[id];
  4184.  
  4185. // 删除相关的用户标记
  4186. users.forEach((user) => {
  4187. const index = user.tags.findIndex((item) => item === id);
  4188.  
  4189. if (index >= 0) {
  4190. user.tags.splice(index, 1);
  4191. }
  4192. });
  4193.  
  4194. // 保存数据
  4195. this.settings.tags = list;
  4196.  
  4197. // 重新过滤
  4198. this.reFilter(users);
  4199.  
  4200. // 返回删除的标记
  4201. return entity;
  4202. }
  4203.  
  4204. /**
  4205. * 格式化
  4206. * @param {Number} id 标记 ID
  4207. * @param {String | undefined} name 标记名称
  4208. * @param {String | undefined} name 标记颜色
  4209. */
  4210. format(id, name, color) {
  4211. const { ui } = this;
  4212.  
  4213. if (id >= 0) {
  4214. const tag = this.get({ id });
  4215.  
  4216. if (tag) {
  4217. name = tag.name;
  4218. color = tag.color;
  4219. }
  4220. }
  4221.  
  4222. if (name && color) {
  4223. return ui.createElement("B", name, {
  4224. className: "block_txt nobr",
  4225. style: `background: ${color}; color: #FFF; margin: 0.1em 0.2em;`,
  4226. });
  4227. }
  4228.  
  4229. return "";
  4230. }
  4231.  
  4232. /**
  4233. * 表格列
  4234. * @returns {Array} 表格列集合
  4235. */
  4236. columns() {
  4237. return [
  4238. { label: "标记", width: 1 },
  4239. { label: "列表" },
  4240. { label: "过滤模式", width: 1 },
  4241. { label: "操作", width: 1 },
  4242. ];
  4243. }
  4244.  
  4245. /**
  4246. * 表格项
  4247. * @param {*} item 标记信息
  4248. * @returns {Array} 表格项集合
  4249. */
  4250. column(item) {
  4251. const { ui } = this;
  4252. const { table } = this.views;
  4253. const { id, filterMode } = item;
  4254.  
  4255. // 标记
  4256. const tag = this.format(id);
  4257.  
  4258. // 用户列表
  4259. const list = Object.values(this.userModule.list)
  4260. .filter(({ tags }) => tags.includes(id))
  4261. .map(({ id }) => this.userModule.format(id));
  4262.  
  4263. const group = ui.createElement("DIV", list, {
  4264. style: "white-space: normal; display: none;",
  4265. });
  4266.  
  4267. const switchButton = ui.createButton(list.length.toString(), () => {
  4268. if (group.style.display === "none") {
  4269. group.style.removeProperty("display");
  4270. } else {
  4271. group.style.display = "none";
  4272. }
  4273. });
  4274.  
  4275. // 切换过滤模式
  4276. const switchMode = ui.createButton(
  4277. filterMode || this.settings.filterModes[0],
  4278. () => {
  4279. const newMode = this.settings.switchModeByName(switchMode.innerText);
  4280.  
  4281. this.update(id, {
  4282. filterMode: newMode,
  4283. });
  4284.  
  4285. switchMode.innerText = newMode;
  4286. }
  4287. );
  4288.  
  4289. // 操作
  4290. const buttons = (() => {
  4291. const remove = ui.createButton("删除", (e) => {
  4292. ui.confirm().then(() => {
  4293. this.remove(id);
  4294.  
  4295. table.remove(e);
  4296. });
  4297. });
  4298.  
  4299. return ui.createButtonGroup(remove);
  4300. })();
  4301.  
  4302. return [tag, [switchButton, group], switchMode, buttons];
  4303. }
  4304.  
  4305. /**
  4306. * 初始化组件
  4307. */
  4308. initComponents() {
  4309. super.initComponents();
  4310.  
  4311. const { ui } = this;
  4312. const { tabs, content, settings } = ui.views;
  4313. const { add } = settings;
  4314.  
  4315. const table = ui.createTable(this.columns());
  4316.  
  4317. const tab = ui.createTab(
  4318. tabs,
  4319. this.constructor.label,
  4320. this.constructor.order,
  4321. {
  4322. onclick: () => {
  4323. this.render(content);
  4324. },
  4325. }
  4326. );
  4327.  
  4328. Object.assign(this.views, {
  4329. tab,
  4330. table,
  4331. });
  4332.  
  4333. this.views.container.appendChild(table);
  4334.  
  4335. // 删除没有标记的用户
  4336. {
  4337. const button = ui.createButton("删除没有标记的用户", () => {
  4338. ui.confirm().then(() => {
  4339. const users = Object.values(this.userModule.list);
  4340.  
  4341. users.forEach(({ id, tags }) => {
  4342. if (tags.length > 0) {
  4343. return;
  4344. }
  4345.  
  4346. this.userModule.remove(id);
  4347. });
  4348. });
  4349. });
  4350.  
  4351. const element = ui.createElement("DIV", button);
  4352.  
  4353. add(this.constructor.order + 0, element);
  4354. }
  4355.  
  4356. // 删除没有用户的标记
  4357. {
  4358. const button = ui.createButton("删除没有用户的标记", () => {
  4359. ui.confirm().then(() => {
  4360. const items = Object.values(this.list);
  4361. const users = Object.values(this.userModule.list);
  4362.  
  4363. items.forEach(({ id }) => {
  4364. if (users.find(({ tags }) => tags.includes(id))) {
  4365. return;
  4366. }
  4367.  
  4368. this.remove(id);
  4369. });
  4370. });
  4371. });
  4372.  
  4373. const element = ui.createElement("DIV", button);
  4374.  
  4375. add(this.constructor.order + 1, element);
  4376. }
  4377. }
  4378.  
  4379. /**
  4380. * 渲染
  4381. * @param {HTMLElement} container 容器
  4382. */
  4383. render(container) {
  4384. super.render(container);
  4385.  
  4386. const { table } = this.views;
  4387.  
  4388. if (table) {
  4389. const { add, clear } = table;
  4390.  
  4391. clear();
  4392.  
  4393. Object.values(this.list).forEach((item) => {
  4394. const column = this.column(item);
  4395.  
  4396. add(...column);
  4397. });
  4398. }
  4399. }
  4400.  
  4401. /**
  4402. * 过滤
  4403. * @param {*} item 绑定的 nFilter
  4404. * @param {*} result 过滤结果
  4405. */
  4406. async filter(item, result) {
  4407. // 获取用户信息
  4408. const user = this.userModule.get(item.uid);
  4409.  
  4410. // 没有则跳过
  4411. if (user === null) {
  4412. return;
  4413. }
  4414.  
  4415. // 获取用户标记
  4416. const tags = user.tags;
  4417.  
  4418. // 取最高的过滤模式
  4419. // 低于当前的过滤模式则跳过
  4420. let max = result.mode;
  4421. let tag = null;
  4422.  
  4423. for (const id of tags) {
  4424. const entity = this.get({ id });
  4425.  
  4426. if (entity === null) {
  4427. continue;
  4428. }
  4429.  
  4430. // 获取过滤模式
  4431. const mode = this.settings.getModeByName(entity.filterMode);
  4432.  
  4433. if (mode <= max) {
  4434. continue;
  4435. }
  4436.  
  4437. max = mode;
  4438. tag = entity;
  4439. }
  4440.  
  4441. // 没有匹配的则跳过
  4442. if (tag === null) {
  4443. return;
  4444. }
  4445.  
  4446. // 更新过滤模式和原因
  4447. result.mode = max;
  4448. result.reason = `标记: ${tag.name}`;
  4449. }
  4450.  
  4451. /**
  4452. * 通知
  4453. * @param {*} item 绑定的 nFilter
  4454. */
  4455. async notify(item) {
  4456. const { uid, tags } = item;
  4457.  
  4458. // 如果没有 tags 组件则跳过
  4459. if (tags === null) {
  4460. return;
  4461. }
  4462.  
  4463. // 如果是匿名,隐藏组件
  4464. if (uid <= 0) {
  4465. tags.style.display = "none";
  4466. return;
  4467. }
  4468.  
  4469. // 删除旧标记
  4470. [...tags.querySelectorAll("[tid]")].forEach((item) => {
  4471. tags.removeChild(item);
  4472. });
  4473.  
  4474. // 获取当前用户
  4475. const user = this.userModule.get(uid);
  4476.  
  4477. // 如果没有用户,则跳过
  4478. if (user === null) {
  4479. return;
  4480. }
  4481.  
  4482. // 格式化标记
  4483. const items = user.tags.map((id) => {
  4484. const item = this.format(id);
  4485.  
  4486. if (item) {
  4487. item.setAttribute("tid", id);
  4488. }
  4489.  
  4490. return item;
  4491. });
  4492.  
  4493. // 加入组件
  4494. items.forEach((item) => {
  4495. if (item) {
  4496. tags.appendChild(item);
  4497. }
  4498. });
  4499. }
  4500.  
  4501. /**
  4502. * 重新过滤
  4503. * @param {Array} users 用户集合
  4504. */
  4505. reFilter(users) {
  4506. users.forEach((user) => {
  4507. this.userModule.reFilter(user.id);
  4508. });
  4509. }
  4510. }
  4511.  
  4512. /**
  4513. * 关键字模块
  4514. */
  4515. class KeywordModule extends Module {
  4516. /**
  4517. * 模块名称
  4518. */
  4519. static name = "keyword";
  4520.  
  4521. /**
  4522. * 模块标签
  4523. */
  4524. static label = "关键字";
  4525.  
  4526. /**
  4527. * 顺序
  4528. */
  4529. static order = 40;
  4530.  
  4531. /**
  4532. * 获取列表
  4533. */
  4534. get list() {
  4535. return this.settings.keywords;
  4536. }
  4537.  
  4538. /**
  4539. * 获取关键字
  4540. * @param {Number} id 关键字 ID
  4541. */
  4542. get(id) {
  4543. // 获取列表
  4544. const list = this.list;
  4545.  
  4546. // 如果存在,则返回信息
  4547. if (list[id]) {
  4548. return list[id];
  4549. }
  4550.  
  4551. return null;
  4552. }
  4553.  
  4554. /**
  4555. * 添加关键字
  4556. * @param {String} keyword 关键字
  4557. * @param {String} filterMode 过滤模式
  4558. * @param {Number} filterLevel 过滤等级: 0 - 仅过滤标题; 1 - 过滤标题和内容
  4559. */
  4560. add(keyword, filterMode, filterLevel) {
  4561. // 获取列表
  4562. const list = this.list;
  4563.  
  4564. // ID 为最大值 + 1
  4565. const id = Math.max(...Object.keys(list), 0) + 1;
  4566.  
  4567. // 写入关键字信息
  4568. list[id] = {
  4569. id,
  4570. keyword,
  4571. filterMode,
  4572. filterLevel,
  4573. };
  4574.  
  4575. // 保存数据
  4576. this.settings.keywords = list;
  4577.  
  4578. // 重新过滤
  4579. this.reFilter();
  4580.  
  4581. // 返回添加的关键字
  4582. return list[id];
  4583. }
  4584.  
  4585. /**
  4586. * 编辑关键字
  4587. * @param {Number} id 关键字 ID
  4588. * @param {*} values 关键字信息
  4589. */
  4590. update(id, values) {
  4591. // 获取列表
  4592. const list = this.list;
  4593.  
  4594. // 如果不存在则跳过
  4595. if (Object.hasOwn(list, id) === false) {
  4596. return null;
  4597. }
  4598.  
  4599. // 获取关键字
  4600. const entity = list[id];
  4601.  
  4602. // 更新关键字
  4603. Object.assign(entity, values);
  4604.  
  4605. // 保存数据
  4606. this.settings.keywords = list;
  4607.  
  4608. // 重新过滤
  4609. this.reFilter();
  4610. }
  4611.  
  4612. /**
  4613. * 删除关键字
  4614. * @param {Number} id 关键字 ID
  4615. */
  4616. remove(id) {
  4617. // 获取列表
  4618. const list = this.list;
  4619.  
  4620. // 如果不存在则跳过
  4621. if (Object.hasOwn(list, id) === false) {
  4622. return null;
  4623. }
  4624.  
  4625. // 获取关键字
  4626. const entity = list[id];
  4627.  
  4628. // 删除关键字
  4629. delete list[id];
  4630.  
  4631. // 保存数据
  4632. this.settings.keywords = list;
  4633.  
  4634. // 重新过滤
  4635. this.reFilter();
  4636.  
  4637. // 返回删除的关键字
  4638. return entity;
  4639. }
  4640.  
  4641. /**
  4642. * 获取帖子数据
  4643. * @param {*} item 绑定的 nFilter
  4644. */
  4645. async getPostInfo(item) {
  4646. const { tid, pid } = item;
  4647.  
  4648. // 请求帖子数据
  4649. const { subject, content, userInfo, reputation } =
  4650. await this.api.getPostInfo(tid, pid);
  4651.  
  4652. // 绑定用户信息和声望
  4653. if (userInfo) {
  4654. item.userInfo = userInfo;
  4655. item.username = userInfo.username;
  4656. item.reputation = reputation;
  4657. }
  4658.  
  4659. // 绑定标题和内容
  4660. item.subject = subject;
  4661. item.content = content;
  4662. }
  4663.  
  4664. /**
  4665. * 表格列
  4666. * @returns {Array} 表格列集合
  4667. */
  4668. columns() {
  4669. return [
  4670. { label: "关键字" },
  4671. { label: "过滤模式", center: true, width: 1 },
  4672. { label: "包括内容", center: true, width: 1 },
  4673. { label: "操作", width: 1 },
  4674. ];
  4675. }
  4676.  
  4677. /**
  4678. * 表格项
  4679. * @param {*} item 标记信息
  4680. * @returns {Array} 表格项集合
  4681. */
  4682. column(item) {
  4683. const { ui } = this;
  4684. const { table } = this.views;
  4685. const { id, keyword, filterLevel, filterMode } = item;
  4686.  
  4687. // 关键字
  4688. const input = ui.createElement("INPUT", [], {
  4689. type: "text",
  4690. value: keyword,
  4691. });
  4692.  
  4693. const inputWrapper = ui.createElement("DIV", input, {
  4694. className: "filter-input-wrapper",
  4695. });
  4696.  
  4697. // 切换过滤模式
  4698. const switchMode = ui.createButton(
  4699. filterMode || this.settings.filterModes[0],
  4700. () => {
  4701. const newMode = this.settings.switchModeByName(switchMode.innerText);
  4702.  
  4703. switchMode.innerText = newMode;
  4704. }
  4705. );
  4706.  
  4707. // 包括内容
  4708. const switchLevel = ui.createElement("INPUT", [], {
  4709. type: "checkbox",
  4710. checked: filterLevel > 0,
  4711. });
  4712.  
  4713. // 操作
  4714. const buttons = (() => {
  4715. const save = ui.createButton("保存", () => {
  4716. this.update(id, {
  4717. keyword: input.value,
  4718. filterMode: switchMode.innerText,
  4719. filterLevel: switchLevel.checked ? 1 : 0,
  4720. });
  4721. });
  4722.  
  4723. const remove = ui.createButton("删除", (e) => {
  4724. ui.confirm().then(() => {
  4725. this.remove(id);
  4726.  
  4727. table.remove(e);
  4728. });
  4729. });
  4730.  
  4731. return ui.createButtonGroup(save, remove);
  4732. })();
  4733.  
  4734. return [inputWrapper, switchMode, switchLevel, buttons];
  4735. }
  4736.  
  4737. /**
  4738. * 初始化组件
  4739. */
  4740. initComponents() {
  4741. super.initComponents();
  4742.  
  4743. const { ui } = this;
  4744. const { tabs, content } = ui.views;
  4745.  
  4746. const table = ui.createTable(this.columns());
  4747.  
  4748. const tips = ui.createElement("DIV", TIPS.keyword, {
  4749. className: "silver",
  4750. });
  4751.  
  4752. const tab = ui.createTab(
  4753. tabs,
  4754. this.constructor.label,
  4755. this.constructor.order,
  4756. {
  4757. onclick: () => {
  4758. this.render(content);
  4759. },
  4760. }
  4761. );
  4762.  
  4763. Object.assign(this.views, {
  4764. tab,
  4765. table,
  4766. });
  4767.  
  4768. this.views.container.appendChild(table);
  4769. this.views.container.appendChild(tips);
  4770. }
  4771.  
  4772. /**
  4773. * 渲染
  4774. * @param {HTMLElement} container 容器
  4775. */
  4776. render(container) {
  4777. super.render(container);
  4778.  
  4779. const { table } = this.views;
  4780.  
  4781. if (table) {
  4782. const { add, clear } = table;
  4783.  
  4784. clear();
  4785.  
  4786. Object.values(this.list).forEach((item) => {
  4787. const column = this.column(item);
  4788.  
  4789. add(...column);
  4790. });
  4791.  
  4792. this.renderNewLine();
  4793. }
  4794. }
  4795.  
  4796. /**
  4797. * 渲染新行
  4798. */
  4799. renderNewLine() {
  4800. const { ui } = this;
  4801. const { table } = this.views;
  4802.  
  4803. // 关键字
  4804. const input = ui.createElement("INPUT", [], {
  4805. type: "text",
  4806. });
  4807.  
  4808. const inputWrapper = ui.createElement("DIV", input, {
  4809. className: "filter-input-wrapper",
  4810. });
  4811.  
  4812. // 切换过滤模式
  4813. const switchMode = ui.createButton(this.settings.filterModes[0], () => {
  4814. const newMode = this.settings.switchModeByName(switchMode.innerText);
  4815.  
  4816. switchMode.innerText = newMode;
  4817. });
  4818.  
  4819. // 包括内容
  4820. const switchLevel = ui.createElement("INPUT", [], {
  4821. type: "checkbox",
  4822. });
  4823.  
  4824. // 操作
  4825. const buttons = (() => {
  4826. const save = ui.createButton("添加", (e) => {
  4827. const entity = this.add(
  4828. input.value,
  4829. switchMode.innerText,
  4830. switchLevel.checked ? 1 : 0
  4831. );
  4832.  
  4833. table.update(e, ...this.column(entity));
  4834.  
  4835. this.renderNewLine();
  4836. });
  4837.  
  4838. return ui.createButtonGroup(save);
  4839. })();
  4840.  
  4841. // 添加至列表
  4842. table.add(inputWrapper, switchMode, switchLevel, buttons);
  4843. }
  4844.  
  4845. /**
  4846. * 过滤
  4847. * @param {*} item 绑定的 nFilter
  4848. * @param {*} result 过滤结果
  4849. */
  4850. async filter(item, result) {
  4851. // 获取列表
  4852. const list = this.list;
  4853.  
  4854. // 跳过低于当前的过滤模式
  4855. const filtered = Object.values(list).filter(
  4856. (item) => this.settings.getModeByName(item.filterMode) > result.mode
  4857. );
  4858.  
  4859. // 没有则跳过
  4860. if (filtered.length === 0) {
  4861. return;
  4862. }
  4863.  
  4864. // 根据过滤模式依次判断
  4865. const sorted = Tools.sortBy(filtered, (item) =>
  4866. this.settings.getModeByName(item.filterMode)
  4867. );
  4868.  
  4869. for (let i = 0; i < sorted.length; i += 1) {
  4870. const { keyword, filterMode } = sorted[i];
  4871.  
  4872. // 过滤等级,0 为只过滤标题,1 为过滤标题和内容
  4873. const filterLevel = sorted[i].filterLevel || 0;
  4874.  
  4875. // 过滤标题
  4876. if (filterLevel >= 0) {
  4877. const { subject } = item;
  4878.  
  4879. const match = subject.match(keyword);
  4880.  
  4881. if (match) {
  4882. const mode = this.settings.getModeByName(filterMode);
  4883.  
  4884. // 更新过滤模式和原因
  4885. result.mode = mode;
  4886. result.reason = `关键字: ${match[0]}`;
  4887. return;
  4888. }
  4889. }
  4890.  
  4891. // 过滤内容
  4892. if (filterLevel >= 1) {
  4893. // 如果没有内容,则请求
  4894. if (item.content === undefined) {
  4895. await this.getPostInfo(item);
  4896. }
  4897.  
  4898. const { content } = item;
  4899.  
  4900. const match = content.match(keyword);
  4901.  
  4902. if (match) {
  4903. const mode = this.settings.getModeByName(filterMode);
  4904.  
  4905. // 更新过滤模式和原因
  4906. result.mode = mode;
  4907. result.reason = `关键字: ${match[0]}`;
  4908. return;
  4909. }
  4910. }
  4911. }
  4912. }
  4913.  
  4914. /**
  4915. * 重新过滤
  4916. */
  4917. reFilter() {
  4918. // 实际上应该根据过滤模式来筛选要过滤的部分
  4919. this.data.forEach((item) => {
  4920. item.execute();
  4921. });
  4922. }
  4923. }
  4924.  
  4925. /**
  4926. * 属地模块
  4927. */
  4928. class LocationModule extends Module {
  4929. /**
  4930. * 模块名称
  4931. */
  4932. static name = "location";
  4933.  
  4934. /**
  4935. * 模块标签
  4936. */
  4937. static label = "属地";
  4938.  
  4939. /**
  4940. * 顺序
  4941. */
  4942. static order = 50;
  4943.  
  4944. /**
  4945. * 请求缓存
  4946. */
  4947. cache = {};
  4948.  
  4949. /**
  4950. * 获取列表
  4951. */
  4952. get list() {
  4953. return this.settings.locations;
  4954. }
  4955.  
  4956. /**
  4957. * 获取属地
  4958. * @param {Number} id 属地 ID
  4959. */
  4960. get(id) {
  4961. // 获取列表
  4962. const list = this.list;
  4963.  
  4964. // 如果存在,则返回信息
  4965. if (list[id]) {
  4966. return list[id];
  4967. }
  4968.  
  4969. return null;
  4970. }
  4971.  
  4972. /**
  4973. * 添加属地
  4974. * @param {String} keyword 关键字
  4975. * @param {String} filterMode 过滤模式
  4976. */
  4977. add(keyword, filterMode) {
  4978. // 获取列表
  4979. const list = this.list;
  4980.  
  4981. // ID 为最大值 + 1
  4982. const id = Math.max(...Object.keys(list), 0) + 1;
  4983.  
  4984. // 写入属地信息
  4985. list[id] = {
  4986. id,
  4987. keyword,
  4988. filterMode,
  4989. };
  4990.  
  4991. // 保存数据
  4992. this.settings.locations = list;
  4993.  
  4994. // 重新过滤
  4995. this.reFilter();
  4996.  
  4997. // 返回添加的属地
  4998. return list[id];
  4999. }
  5000.  
  5001. /**
  5002. * 编辑属地
  5003. * @param {Number} id 属地 ID
  5004. * @param {*} values 属地信息
  5005. */
  5006. update(id, values) {
  5007. // 获取列表
  5008. const list = this.list;
  5009.  
  5010. // 如果不存在则跳过
  5011. if (Object.hasOwn(list, id) === false) {
  5012. return null;
  5013. }
  5014.  
  5015. // 获取属地
  5016. const entity = list[id];
  5017.  
  5018. // 更新属地
  5019. Object.assign(entity, values);
  5020.  
  5021. // 保存数据
  5022. this.settings.locations = list;
  5023.  
  5024. // 重新过滤
  5025. this.reFilter();
  5026. }
  5027.  
  5028. /**
  5029. * 删除属地
  5030. * @param {Number} id 属地 ID
  5031. */
  5032. remove(id) {
  5033. // 获取列表
  5034. const list = this.list;
  5035.  
  5036. // 如果不存在则跳过
  5037. if (Object.hasOwn(list, id) === false) {
  5038. return null;
  5039. }
  5040.  
  5041. // 获取属地
  5042. const entity = list[id];
  5043.  
  5044. // 删除属地
  5045. delete list[id];
  5046.  
  5047. // 保存数据
  5048. this.settings.locations = list;
  5049.  
  5050. // 重新过滤
  5051. this.reFilter();
  5052.  
  5053. // 返回删除的属地
  5054. return entity;
  5055. }
  5056.  
  5057. /**
  5058. * 获取 IP 属地
  5059. * @param {*} item 绑定的 nFilter
  5060. */
  5061. async getIpLocation(item) {
  5062. const { uid } = item;
  5063.  
  5064. // 如果是匿名直接跳过
  5065. if (uid <= 0) {
  5066. return;
  5067. }
  5068.  
  5069. // 如果已有缓存,直接返回
  5070. if (Object.hasOwn(this.cache, uid)) {
  5071. return this.cache[uid];
  5072. }
  5073.  
  5074. // 请求属地
  5075. const { ipLoc } = await this.api.getUserInfo(uid);
  5076.  
  5077. // 写入缓存
  5078. if (ipLoc) {
  5079. this.cache[uid] = ipLoc;
  5080. }
  5081.  
  5082. // 返回结果
  5083. return ipLoc;
  5084. }
  5085.  
  5086. /**
  5087. * 表格列
  5088. * @returns {Array} 表格列集合
  5089. */
  5090. columns() {
  5091. return [
  5092. { label: "关键字" },
  5093. { label: "过滤模式", center: true, width: 1 },
  5094. { label: "操作", width: 1 },
  5095. ];
  5096. }
  5097.  
  5098. /**
  5099. * 表格项
  5100. * @param {*} item 标记信息
  5101. * @returns {Array} 表格项集合
  5102. */
  5103. column(item) {
  5104. const { ui } = this;
  5105. const { table } = this.views;
  5106. const { id, keyword, filterMode } = item;
  5107.  
  5108. // 关键字
  5109. const input = ui.createElement("INPUT", [], {
  5110. type: "text",
  5111. value: keyword,
  5112. });
  5113.  
  5114. const inputWrapper = ui.createElement("DIV", input, {
  5115. className: "filter-input-wrapper",
  5116. });
  5117.  
  5118. // 切换过滤模式
  5119. const switchMode = ui.createButton(
  5120. filterMode || this.settings.filterModes[0],
  5121. () => {
  5122. const newMode = this.settings.switchModeByName(switchMode.innerText);
  5123.  
  5124. switchMode.innerText = newMode;
  5125. }
  5126. );
  5127.  
  5128. // 操作
  5129. const buttons = (() => {
  5130. const save = ui.createButton("保存", () => {
  5131. this.update(id, {
  5132. keyword: input.value,
  5133. filterMode: switchMode.innerText,
  5134. });
  5135. });
  5136.  
  5137. const remove = ui.createButton("删除", (e) => {
  5138. ui.confirm().then(() => {
  5139. this.remove(id);
  5140.  
  5141. table.remove(e);
  5142. });
  5143. });
  5144.  
  5145. return ui.createButtonGroup(save, remove);
  5146. })();
  5147.  
  5148. return [inputWrapper, switchMode, buttons];
  5149. }
  5150.  
  5151. /**
  5152. * 初始化组件
  5153. */
  5154. initComponents() {
  5155. super.initComponents();
  5156.  
  5157. const { ui } = this;
  5158. const { tabs, content } = ui.views;
  5159.  
  5160. const table = ui.createTable(this.columns());
  5161.  
  5162. const tips = ui.createElement("DIV", TIPS.keyword, {
  5163. className: "silver",
  5164. });
  5165.  
  5166. const tab = ui.createTab(
  5167. tabs,
  5168. this.constructor.label,
  5169. this.constructor.order,
  5170. {
  5171. onclick: () => {
  5172. this.render(content);
  5173. },
  5174. }
  5175. );
  5176.  
  5177. Object.assign(this.views, {
  5178. tab,
  5179. table,
  5180. });
  5181.  
  5182. this.views.container.appendChild(table);
  5183. this.views.container.appendChild(tips);
  5184. }
  5185.  
  5186. /**
  5187. * 渲染
  5188. * @param {HTMLElement} container 容器
  5189. */
  5190. render(container) {
  5191. super.render(container);
  5192.  
  5193. const { table } = this.views;
  5194.  
  5195. if (table) {
  5196. const { add, clear } = table;
  5197.  
  5198. clear();
  5199.  
  5200. Object.values(this.list).forEach((item) => {
  5201. const column = this.column(item);
  5202.  
  5203. add(...column);
  5204. });
  5205.  
  5206. this.renderNewLine();
  5207. }
  5208. }
  5209.  
  5210. /**
  5211. * 渲染新行
  5212. */
  5213. renderNewLine() {
  5214. const { ui } = this;
  5215. const { table } = this.views;
  5216.  
  5217. // 关键字
  5218. const input = ui.createElement("INPUT", [], {
  5219. type: "text",
  5220. });
  5221.  
  5222. const inputWrapper = ui.createElement("DIV", input, {
  5223. className: "filter-input-wrapper",
  5224. });
  5225.  
  5226. // 切换过滤模式
  5227. const switchMode = ui.createButton(this.settings.filterModes[0], () => {
  5228. const newMode = this.settings.switchModeByName(switchMode.innerText);
  5229.  
  5230. switchMode.innerText = newMode;
  5231. });
  5232.  
  5233. // 操作
  5234. const buttons = (() => {
  5235. const save = ui.createButton("添加", (e) => {
  5236. const entity = this.add(input.value, switchMode.innerText);
  5237.  
  5238. table.update(e, ...this.column(entity));
  5239.  
  5240. this.renderNewLine();
  5241. });
  5242.  
  5243. return ui.createButtonGroup(save);
  5244. })();
  5245.  
  5246. // 添加至列表
  5247. table.add(inputWrapper, switchMode, buttons);
  5248. }
  5249.  
  5250. /**
  5251. * 过滤
  5252. * @param {*} item 绑定的 nFilter
  5253. * @param {*} result 过滤结果
  5254. */
  5255. async filter(item, result) {
  5256. // 获取列表
  5257. const list = this.list;
  5258.  
  5259. // 跳过低于当前的过滤模式
  5260. const filtered = Object.values(list).filter(
  5261. (item) => this.settings.getModeByName(item.filterMode) > result.mode
  5262. );
  5263.  
  5264. // 没有则跳过
  5265. if (filtered.length === 0) {
  5266. return;
  5267. }
  5268.  
  5269. // 获取当前属地
  5270. const location = await this.getIpLocation(item);
  5271.  
  5272. // 请求失败则跳过
  5273. if (location === undefined) {
  5274. return;
  5275. }
  5276.  
  5277. // 根据过滤模式依次判断
  5278. const sorted = Tools.sortBy(filtered, (item) =>
  5279. this.settings.getModeByName(item.filterMode)
  5280. );
  5281.  
  5282. for (let i = 0; i < sorted.length; i += 1) {
  5283. const { keyword, filterMode } = sorted[i];
  5284.  
  5285. const match = location.match(keyword);
  5286.  
  5287. if (match) {
  5288. const mode = this.settings.getModeByName(filterMode);
  5289.  
  5290. // 更新过滤模式和原因
  5291. result.mode = mode;
  5292. result.reason = `属地: ${match[0]}`;
  5293. return;
  5294. }
  5295. }
  5296. }
  5297.  
  5298. /**
  5299. * 重新过滤
  5300. */
  5301. reFilter() {
  5302. // 实际上应该根据过滤模式来筛选要过滤的部分
  5303. this.data.forEach((item) => {
  5304. item.execute();
  5305. });
  5306. }
  5307. }
  5308.  
  5309. /**
  5310. * 猎巫模块
  5311. *
  5312. * 其实是通过 Cache 模块读取配置,而非 Settings
  5313. */
  5314. class HunterModule extends Module {
  5315. /**
  5316. * 模块名称
  5317. */
  5318. static name = "hunter";
  5319.  
  5320. /**
  5321. * 模块标签
  5322. */
  5323. static label = "猎巫";
  5324.  
  5325. /**
  5326. * 顺序
  5327. */
  5328. static order = 60;
  5329.  
  5330. /**
  5331. * 请求缓存
  5332. */
  5333. cache = {};
  5334.  
  5335. /**
  5336. * 请求队列
  5337. */
  5338. queue = [];
  5339.  
  5340. /**
  5341. * 获取列表
  5342. */
  5343. get list() {
  5344. return this.settings.cache
  5345. .get("WITCH_HUNT")
  5346. .then((values) => values || []);
  5347. }
  5348.  
  5349. /**
  5350. * 获取猎巫
  5351. * @param {Number} id 猎巫 ID
  5352. */
  5353. async get(id) {
  5354. // 获取列表
  5355. const list = await this.list;
  5356.  
  5357. // 如果存在,则返回信息
  5358. if (list[id]) {
  5359. return list[id];
  5360. }
  5361.  
  5362. return null;
  5363. }
  5364.  
  5365. /**
  5366. * 添加猎巫
  5367. * @param {Number} fid 版面 ID
  5368. * @param {String} label 标签
  5369. * @param {String} filterMode 过滤模式
  5370. * @param {Number} filterLevel 过滤等级: 0 - 仅标记; 1 - 标记并过滤
  5371. */
  5372. async add(fid, label, filterMode, filterLevel) {
  5373. // FID 只能是数字
  5374. fid = parseInt(fid, 10);
  5375.  
  5376. // 获取列表
  5377. const list = await this.list;
  5378.  
  5379. // 如果版面 ID 已存在,则提示错误
  5380. if (Object.keys(list).includes(fid)) {
  5381. alert("已有相同版面ID");
  5382. return;
  5383. }
  5384.  
  5385. // 请求版面信息
  5386. const info = await this.api.getForumInfo(fid);
  5387.  
  5388. // 如果版面不存在,则提示错误
  5389. if (info === null) {
  5390. alert("版面ID有误");
  5391. return;
  5392. }
  5393.  
  5394. // 计算标记颜色
  5395. const color = Tools.generateColor(info.name);
  5396.  
  5397. // 写入猎巫信息
  5398. list[fid] = {
  5399. fid,
  5400. name: info.name,
  5401. label,
  5402. color,
  5403. filterMode,
  5404. filterLevel,
  5405. };
  5406.  
  5407. // 保存数据
  5408. this.settings.cache.put("WITCH_HUNT", list);
  5409.  
  5410. // 重新过滤
  5411. this.reFilter(true);
  5412.  
  5413. // 返回添加的猎巫
  5414. return list[fid];
  5415. }
  5416.  
  5417. /**
  5418. * 编辑猎巫
  5419. * @param {Number} fid 版面 ID
  5420. * @param {*} values 猎巫信息
  5421. */
  5422. async update(fid, values) {
  5423. // 获取列表
  5424. const list = await this.list;
  5425.  
  5426. // 如果不存在则跳过
  5427. if (Object.hasOwn(list, fid) === false) {
  5428. return null;
  5429. }
  5430.  
  5431. // 获取猎巫
  5432. const entity = list[fid];
  5433.  
  5434. // 更新猎巫
  5435. Object.assign(entity, values);
  5436.  
  5437. // 保存数据
  5438. this.settings.cache.put("WITCH_HUNT", list);
  5439.  
  5440. // 重新过滤,更新样式即可
  5441. this.reFilter(false);
  5442. }
  5443.  
  5444. /**
  5445. * 删除猎巫
  5446. * @param {Number} fid 版面 ID
  5447. */
  5448. async remove(fid) {
  5449. // 获取列表
  5450. const list = await this.list;
  5451.  
  5452. // 如果不存在则跳过
  5453. if (Object.hasOwn(list, fid) === false) {
  5454. return null;
  5455. }
  5456.  
  5457. // 获取猎巫
  5458. const entity = list[fid];
  5459.  
  5460. // 删除猎巫
  5461. delete list[fid];
  5462.  
  5463. // 保存数据
  5464. this.settings.cache.put("WITCH_HUNT", list);
  5465.  
  5466. // 重新过滤
  5467. this.reFilter(true);
  5468.  
  5469. // 返回删除的属地
  5470. return entity;
  5471. }
  5472.  
  5473. /**
  5474. * 格式化版面
  5475. * @param {Number} fid 版面 ID
  5476. * @param {String} name 版面名称
  5477. */
  5478. formatForum(fid, name) {
  5479. const { ui } = this;
  5480.  
  5481. return ui.createElement("A", `[${name}]`, {
  5482. className: "b nobr",
  5483. href: `/thread.php?fid=${fid}`,
  5484. });
  5485. }
  5486.  
  5487. /**
  5488. * 格式化标签
  5489. * @param {String} name 标签名称
  5490. * @param {String} name 标签颜色
  5491. */
  5492. formatLabel(name, color) {
  5493. const { ui } = this;
  5494.  
  5495. return ui.createElement("B", name, {
  5496. className: "block_txt nobr",
  5497. style: `background: ${color}; color: #FFF; margin: 0.1em 0.2em;`,
  5498. });
  5499. }
  5500.  
  5501. /**
  5502. * 表格列
  5503. * @returns {Array} 表格列集合
  5504. */
  5505. columns() {
  5506. return [
  5507. { label: "版面", width: 200 },
  5508. { label: "标签" },
  5509. { label: "启用过滤", center: true, width: 1 },
  5510. { label: "过滤模式", center: true, width: 1 },
  5511. { label: "操作", width: 1 },
  5512. ];
  5513. }
  5514.  
  5515. /**
  5516. * 表格项
  5517. * @param {*} item 标记信息
  5518. * @returns {Array} 表格项集合
  5519. */
  5520. column(item) {
  5521. const { ui } = this;
  5522. const { table } = this.views;
  5523. const { fid, name, label, color, filterMode, filterLevel } = item;
  5524.  
  5525. // 版面
  5526. const forum = this.formatForum(fid, name);
  5527.  
  5528. // 标签
  5529. const labelElement = this.formatLabel(label, color);
  5530.  
  5531. // 启用过滤
  5532. const switchLevel = ui.createElement("INPUT", [], {
  5533. type: "checkbox",
  5534. checked: filterLevel > 0,
  5535. });
  5536.  
  5537. // 切换过滤模式
  5538. const switchMode = ui.createButton(
  5539. filterMode || this.settings.filterModes[0],
  5540. () => {
  5541. const newMode = this.settings.switchModeByName(switchMode.innerText);
  5542.  
  5543. switchMode.innerText = newMode;
  5544. }
  5545. );
  5546.  
  5547. // 操作
  5548. const buttons = (() => {
  5549. const save = ui.createButton("保存", () => {
  5550. this.update(fid, {
  5551. filterMode: switchMode.innerText,
  5552. filterLevel: switchLevel.checked ? 1 : 0,
  5553. });
  5554. });
  5555.  
  5556. const remove = ui.createButton("删除", (e) => {
  5557. ui.confirm().then(async () => {
  5558. await this.remove(fid);
  5559.  
  5560. table.remove(e);
  5561. });
  5562. });
  5563.  
  5564. return ui.createButtonGroup(save, remove);
  5565. })();
  5566.  
  5567. return [forum, labelElement, switchLevel, switchMode, buttons];
  5568. }
  5569.  
  5570. /**
  5571. * 初始化组件
  5572. */
  5573. initComponents() {
  5574. super.initComponents();
  5575.  
  5576. const { ui } = this;
  5577. const { tabs, content } = ui.views;
  5578.  
  5579. const table = ui.createTable(this.columns());
  5580.  
  5581. const tips = ui.createElement("DIV", TIPS.hunter, {
  5582. className: "silver",
  5583. });
  5584.  
  5585. const tab = ui.createTab(
  5586. tabs,
  5587. this.constructor.label,
  5588. this.constructor.order,
  5589. {
  5590. onclick: () => {
  5591. this.render(content);
  5592. },
  5593. }
  5594. );
  5595.  
  5596. Object.assign(this.views, {
  5597. tab,
  5598. table,
  5599. });
  5600.  
  5601. this.views.container.appendChild(table);
  5602. this.views.container.appendChild(tips);
  5603. }
  5604.  
  5605. /**
  5606. * 渲染
  5607. * @param {HTMLElement} container 容器
  5608. */
  5609. render(container) {
  5610. super.render(container);
  5611.  
  5612. const { table } = this.views;
  5613.  
  5614. if (table) {
  5615. const { add, clear } = table;
  5616.  
  5617. clear();
  5618.  
  5619. this.list.then((values) => {
  5620. Object.values(values).forEach((item) => {
  5621. const column = this.column(item);
  5622.  
  5623. add(...column);
  5624. });
  5625.  
  5626. this.renderNewLine();
  5627. });
  5628. }
  5629. }
  5630.  
  5631. /**
  5632. * 渲染新行
  5633. */
  5634. renderNewLine() {
  5635. const { ui } = this;
  5636. const { table } = this.views;
  5637.  
  5638. // 版面 ID
  5639. const forumInput = ui.createElement("INPUT", [], {
  5640. type: "text",
  5641. });
  5642.  
  5643. const forumInputWrapper = ui.createElement("DIV", forumInput, {
  5644. className: "filter-input-wrapper",
  5645. });
  5646.  
  5647. // 标签
  5648. const labelInput = ui.createElement("INPUT", [], {
  5649. type: "text",
  5650. });
  5651.  
  5652. const labelInputWrapper = ui.createElement("DIV", labelInput, {
  5653. className: "filter-input-wrapper",
  5654. });
  5655.  
  5656. // 启用过滤
  5657. const switchLevel = ui.createElement("INPUT", [], {
  5658. type: "checkbox",
  5659. });
  5660.  
  5661. // 切换过滤模式
  5662. const switchMode = ui.createButton(this.settings.filterModes[0], () => {
  5663. const newMode = this.settings.switchModeByName(switchMode.innerText);
  5664.  
  5665. switchMode.innerText = newMode;
  5666. });
  5667.  
  5668. // 操作
  5669. const buttons = (() => {
  5670. const save = ui.createButton("添加", async (e) => {
  5671. const entity = await this.add(
  5672. forumInput.value,
  5673. labelInput.value,
  5674. switchMode.innerText,
  5675. switchLevel.checked ? 1 : 0
  5676. );
  5677.  
  5678. table.update(e, ...this.column(entity));
  5679.  
  5680. this.renderNewLine();
  5681. });
  5682.  
  5683. return ui.createButtonGroup(save);
  5684. })();
  5685.  
  5686. // 添加至列表
  5687. table.add(
  5688. forumInputWrapper,
  5689. labelInputWrapper,
  5690. switchLevel,
  5691. switchMode,
  5692. buttons
  5693. );
  5694. }
  5695.  
  5696. /**
  5697. * 过滤
  5698. * @param {*} item 绑定的 nFilter
  5699. * @param {*} result 过滤结果
  5700. */
  5701. async filter(item, result) {
  5702. // 获取当前猎巫结果
  5703. const hunter = item.hunter || [];
  5704.  
  5705. // 如果没有猎巫结果,则跳过
  5706. if (hunter.length === 0) {
  5707. return;
  5708. }
  5709.  
  5710. // 获取列表
  5711. const items = await this.list;
  5712.  
  5713. // 筛选出匹配的猎巫
  5714. const list = Object.values(items).filter(({ fid }) =>
  5715. hunter.includes(fid)
  5716. );
  5717.  
  5718. // 取最高的过滤模式
  5719. // 低于当前的过滤模式则跳过
  5720. let max = result.mode;
  5721. let res = null;
  5722.  
  5723. for (const entity of list) {
  5724. const { filterLevel, filterMode } = entity;
  5725.  
  5726. // 仅标记
  5727. if (filterLevel === 0) {
  5728. continue;
  5729. }
  5730.  
  5731. // 获取过滤模式
  5732. const mode = this.settings.getModeByName(filterMode);
  5733.  
  5734. if (mode <= max) {
  5735. continue;
  5736. }
  5737.  
  5738. max = mode;
  5739. res = entity;
  5740. }
  5741.  
  5742. // 没有匹配的则跳过
  5743. if (res === null) {
  5744. return;
  5745. }
  5746.  
  5747. // 更新过滤模式和原因
  5748. result.mode = max;
  5749. result.reason = `猎巫: ${res.label}`;
  5750. }
  5751.  
  5752. /**
  5753. * 通知
  5754. * @param {*} item 绑定的 nFilter
  5755. */
  5756. async notify(item) {
  5757. const { uid, tags } = item;
  5758.  
  5759. // 如果没有 tags 组件则跳过
  5760. if (tags === null) {
  5761. return;
  5762. }
  5763.  
  5764. // 如果是匿名,隐藏组件
  5765. if (uid <= 0) {
  5766. tags.style.display = "none";
  5767. return;
  5768. }
  5769.  
  5770. // 删除旧标签
  5771. [...tags.querySelectorAll("[fid]")].forEach((item) => {
  5772. tags.removeChild(item);
  5773. });
  5774.  
  5775. // 如果没有请求,开始请求
  5776. if (Object.hasOwn(item, "hunter") === false) {
  5777. this.execute(item);
  5778. return;
  5779. }
  5780.  
  5781. // 获取当前猎巫结果
  5782. const hunter = item.hunter;
  5783.  
  5784. // 如果没有猎巫结果,则跳过
  5785. if (hunter.length === 0) {
  5786. return;
  5787. }
  5788.  
  5789. // 格式化标签
  5790. const items = await Promise.all(
  5791. hunter.map(async (fid) => {
  5792. const item = await this.get(fid);
  5793.  
  5794. if (item) {
  5795. const element = this.formatLabel(item.label, item.color);
  5796.  
  5797. element.setAttribute("fid", fid);
  5798.  
  5799. return element;
  5800. }
  5801.  
  5802. return null;
  5803. })
  5804. );
  5805.  
  5806. // 加入组件
  5807. items.forEach((item) => {
  5808. if (item) {
  5809. tags.appendChild(item);
  5810. }
  5811. });
  5812. }
  5813.  
  5814. /**
  5815. * 重新过滤
  5816. * @param {Boolean} clear 是否清除缓存
  5817. */
  5818. reFilter(clear) {
  5819. // 清除缓存
  5820. if (clear) {
  5821. this.cache = {};
  5822. }
  5823.  
  5824. // 重新过滤
  5825. this.data.forEach((item) => {
  5826. // 不需要清除缓存的话,只要重新加载标记
  5827. if (clear === false) {
  5828. item.hunter = [];
  5829. }
  5830.  
  5831. // 重新猎巫
  5832. this.execute(item);
  5833. });
  5834. }
  5835.  
  5836. /**
  5837. * 猎巫
  5838. * @param {*} item 绑定的 nFilter
  5839. */
  5840. async execute(item) {
  5841. const { uid } = item;
  5842. const { api, cache, queue, list } = this;
  5843.  
  5844. // 如果是匿名,则跳过
  5845. if (uid <= 0) {
  5846. return;
  5847. }
  5848.  
  5849. // 初始化猎巫结果,用于标识正在猎巫
  5850. item.hunter = item.hunter || [];
  5851.  
  5852. // 获取列表
  5853. const items = await list;
  5854.  
  5855. // 没有设置且没有旧数据,直接跳过
  5856. if (items.length === 0 && item.hunter.length === 0) {
  5857. return;
  5858. }
  5859.  
  5860. // 重新过滤
  5861. const reload = (newValue) => {
  5862. const isEqual = newValue.sort().join() === item.hunter.sort().join();
  5863.  
  5864. if (isEqual) {
  5865. return;
  5866. }
  5867.  
  5868. item.hunter = newValue;
  5869. item.execute();
  5870. };
  5871.  
  5872. // 创建任务
  5873. const task = async () => {
  5874. // 如果缓存里没有记录,请求数据并写入缓存
  5875. if (Object.hasOwn(cache, uid) === false) {
  5876. cache[uid] = [];
  5877.  
  5878. await Promise.all(
  5879. Object.keys(items).map(async (fid) => {
  5880. // 转换为数字格式
  5881. const id = parseInt(fid, 10);
  5882.  
  5883. // 当前版面发言记录
  5884. const result = await api.getForumPosted(id, uid);
  5885.  
  5886. // 写入当前设置
  5887. if (result) {
  5888. cache[uid].push(id);
  5889. }
  5890. })
  5891. );
  5892. }
  5893.  
  5894. // 重新过滤
  5895. reload(cache[uid]);
  5896.  
  5897. // 将当前任务移出队列
  5898. queue.shift();
  5899.  
  5900. // 如果还有任务,继续执行
  5901. if (queue.length > 0) {
  5902. queue[0]();
  5903. }
  5904. };
  5905.  
  5906. // 队列里已经有任务
  5907. const isRunning = queue.length > 0;
  5908.  
  5909. // 加入队列
  5910. queue.push(task);
  5911.  
  5912. // 如果没有正在执行的任务,则立即执行
  5913. if (isRunning === false) {
  5914. task();
  5915. }
  5916. }
  5917. }
  5918.  
  5919. /**
  5920. * 杂项模块
  5921. */
  5922. class MiscModule extends Module {
  5923. /**
  5924. * 模块名称
  5925. */
  5926. static name = "misc";
  5927.  
  5928. /**
  5929. * 模块标签
  5930. */
  5931. static label = "杂项";
  5932.  
  5933. /**
  5934. * 顺序
  5935. */
  5936. static order = 100;
  5937.  
  5938. /**
  5939. * 请求缓存
  5940. */
  5941. cache = {
  5942. topicNums: {},
  5943. };
  5944.  
  5945. /**
  5946. * 获取用户信息(从页面上)
  5947. * @param {*} item 绑定的 nFilter
  5948. */
  5949. getUserInfo(item) {
  5950. const { uid } = item;
  5951.  
  5952. // 如果是匿名直接跳过
  5953. if (uid <= 0) {
  5954. return;
  5955. }
  5956.  
  5957. // 回复页面可以直接获取到用户信息和声望
  5958. if (commonui.userInfo) {
  5959. // 取得用户信息
  5960. const userInfo = commonui.userInfo.users[uid];
  5961.  
  5962. // 绑定用户信息和声望
  5963. if (userInfo) {
  5964. item.userInfo = userInfo;
  5965. item.username = userInfo.username;
  5966.  
  5967. item.reputation = (() => {
  5968. const reputations = commonui.userInfo.reputations;
  5969.  
  5970. if (reputations) {
  5971. for (let fid in reputations) {
  5972. return reputations[fid][uid] || 0;
  5973. }
  5974. }
  5975.  
  5976. return NaN;
  5977. })();
  5978. }
  5979. }
  5980. }
  5981.  
  5982. /**
  5983. * 获取帖子数据
  5984. * @param {*} item 绑定的 nFilter
  5985. */
  5986. async getPostInfo(item) {
  5987. const { tid, pid } = item;
  5988.  
  5989. // 请求帖子数据
  5990. const { subject, content, userInfo, reputation } =
  5991. await this.api.getPostInfo(tid, pid);
  5992.  
  5993. // 绑定用户信息和声望
  5994. if (userInfo) {
  5995. item.userInfo = userInfo;
  5996. item.username = userInfo.username;
  5997. item.reputation = reputation;
  5998. }
  5999.  
  6000. // 绑定标题和内容
  6001. item.subject = subject;
  6002. item.content = content;
  6003. }
  6004.  
  6005. /**
  6006. * 获取主题数量
  6007. * @param {*} item 绑定的 nFilter
  6008. */
  6009. async getTopicNum(item) {
  6010. const { uid } = item;
  6011.  
  6012. // 如果是匿名直接跳过
  6013. if (uid <= 0) {
  6014. return;
  6015. }
  6016.  
  6017. // 如果已有缓存,直接返回
  6018. if (Object.hasOwn(this.cache.topicNums, uid)) {
  6019. return this.cache.topicNums[uid];
  6020. }
  6021.  
  6022. // 请求数量
  6023. const number = await this.api.getTopicNum(uid);
  6024.  
  6025. // 写入缓存
  6026. this.cache.topicNums[uid] = number;
  6027.  
  6028. // 返回结果
  6029. return number;
  6030. }
  6031.  
  6032. /**
  6033. * 初始化,增加设置
  6034. */
  6035. initComponents() {
  6036. super.initComponents();
  6037.  
  6038. const { settings, ui } = this;
  6039. const { add } = ui.views.settings;
  6040.  
  6041. // 小号过滤(注册时间)
  6042. {
  6043. const input = ui.createElement("INPUT", [], {
  6044. type: "text",
  6045. value: settings.filterRegdateLimit / 86400000,
  6046. maxLength: 4,
  6047. style: "width: 48px;",
  6048. });
  6049.  
  6050. const button = ui.createButton("确认", () => {
  6051. const newValue = parseInt(input.value, 10) || 0;
  6052.  
  6053. if (newValue < 0) {
  6054. return;
  6055. }
  6056.  
  6057. settings.filterRegdateLimit = newValue * 86400000;
  6058.  
  6059. this.reFilter();
  6060. });
  6061.  
  6062. const element = ui.createElement("DIV", [
  6063. "隐藏注册时间小于",
  6064. input,
  6065. "天的用户",
  6066. button,
  6067. ]);
  6068.  
  6069. add(this.constructor.order + 0, element);
  6070. }
  6071.  
  6072. // 小号过滤(发帖数)
  6073. {
  6074. const input = ui.createElement("INPUT", [], {
  6075. type: "text",
  6076. value: settings.filterPostnumLimit,
  6077. maxLength: 5,
  6078. style: "width: 48px;",
  6079. });
  6080.  
  6081. const button = ui.createButton("确认", () => {
  6082. const newValue = parseInt(input.value, 10) || 0;
  6083.  
  6084. if (newValue < 0) {
  6085. return;
  6086. }
  6087.  
  6088. settings.filterPostnumLimit = newValue;
  6089.  
  6090. this.reFilter();
  6091. });
  6092.  
  6093. const element = ui.createElement("DIV", [
  6094. "隐藏发帖数量小于",
  6095. input,
  6096. "贴的用户",
  6097. button,
  6098. ]);
  6099.  
  6100. add(this.constructor.order + 1, element);
  6101. }
  6102.  
  6103. // 流量号过滤(主题比例)
  6104. {
  6105. const input = ui.createElement("INPUT", [], {
  6106. type: "text",
  6107. value: settings.filterTopicRateLimit,
  6108. maxLength: 3,
  6109. style: "width: 48px;",
  6110. });
  6111.  
  6112. const button = ui.createButton("确认", () => {
  6113. const newValue = parseInt(input.value, 10) || 100;
  6114.  
  6115. if (newValue <= 0 || newValue > 100) {
  6116. return;
  6117. }
  6118.  
  6119. settings.filterTopicRateLimit = newValue;
  6120.  
  6121. this.reFilter();
  6122. });
  6123.  
  6124. const element = ui.createElement("DIV", [
  6125. "隐藏发帖比例大于",
  6126. input,
  6127. "%的用户",
  6128. button,
  6129. ]);
  6130.  
  6131. add(this.constructor.order + 2, element);
  6132. }
  6133.  
  6134. // 声望过滤
  6135. {
  6136. const input = ui.createElement("INPUT", [], {
  6137. type: "text",
  6138. value: settings.filterReputationLimit || "",
  6139. maxLength: 4,
  6140. style: "width: 48px;",
  6141. });
  6142.  
  6143. const button = ui.createButton("确认", () => {
  6144. const newValue = parseInt(input.value, 10);
  6145.  
  6146. settings.filterReputationLimit = newValue;
  6147.  
  6148. this.reFilter();
  6149. });
  6150.  
  6151. const element = ui.createElement("DIV", [
  6152. "隐藏版面声望低于",
  6153. input,
  6154. "点的用户",
  6155. button,
  6156. ]);
  6157.  
  6158. add(this.constructor.order + 3, element);
  6159. }
  6160.  
  6161. // 匿名过滤
  6162. {
  6163. const input = ui.createElement("INPUT", [], {
  6164. type: "checkbox",
  6165. checked: settings.filterAnonymous,
  6166. });
  6167.  
  6168. const label = ui.createElement("LABEL", ["隐藏匿名的用户", input], {
  6169. style: "display: flex;",
  6170. });
  6171.  
  6172. const element = ui.createElement("DIV", label);
  6173.  
  6174. input.onchange = () => {
  6175. settings.filterAnonymous = input.checked;
  6176.  
  6177. this.reFilter();
  6178. };
  6179.  
  6180. add(this.constructor.order + 4, element);
  6181. }
  6182. }
  6183.  
  6184. /**
  6185. * 过滤
  6186. * @param {*} item 绑定的 nFilter
  6187. * @param {*} result 过滤结果
  6188. */
  6189. async filter(item, result) {
  6190. // 获取隐藏模式下标
  6191. const mode = this.settings.getModeByName("隐藏");
  6192.  
  6193. // 如果当前模式不低于隐藏模式,则跳过
  6194. if (result.mode >= mode) {
  6195. return;
  6196. }
  6197.  
  6198. // 匿名过滤
  6199. await this.filterByAnonymous(item, result);
  6200.  
  6201. // 注册时间过滤
  6202. await this.filterByRegdate(item, result);
  6203.  
  6204. // 发帖数量过滤
  6205. await this.filterByPostnum(item, result);
  6206.  
  6207. // 发帖比例过滤
  6208. await this.filterByTopicRate(item, result);
  6209.  
  6210. // 版面声望过滤
  6211. await this.filterByReputation(item, result);
  6212. }
  6213.  
  6214. /**
  6215. * 根据匿名过滤
  6216. * @param {*} item 绑定的 nFilter
  6217. * @param {*} result 过滤结果
  6218. */
  6219. async filterByAnonymous(item, result) {
  6220. const { uid } = item;
  6221.  
  6222. // 如果不是匿名,则跳过
  6223. if (uid > 0) {
  6224. return;
  6225. }
  6226.  
  6227. // 获取隐藏模式下标
  6228. const mode = this.settings.getModeByName("隐藏");
  6229.  
  6230. // 如果当前模式不低于隐藏模式,则跳过
  6231. if (result.mode >= mode) {
  6232. return;
  6233. }
  6234.  
  6235. // 获取过滤匿名设置
  6236. const filterAnonymous = this.settings.filterAnonymous;
  6237.  
  6238. if (filterAnonymous) {
  6239. // 更新过滤模式和原因
  6240. result.mode = mode;
  6241. result.reason = "匿名";
  6242. }
  6243. }
  6244.  
  6245. /**
  6246. * 根据注册时间过滤
  6247. * @param {*} item 绑定的 nFilter
  6248. * @param {*} result 过滤结果
  6249. */
  6250. async filterByRegdate(item, result) {
  6251. const { uid } = item;
  6252.  
  6253. // 如果是匿名,则跳过
  6254. if (uid <= 0) {
  6255. return;
  6256. }
  6257.  
  6258. // 获取隐藏模式下标
  6259. const mode = this.settings.getModeByName("隐藏");
  6260.  
  6261. // 如果当前模式不低于隐藏模式,则跳过
  6262. if (result.mode >= mode) {
  6263. return;
  6264. }
  6265.  
  6266. // 获取注册时间限制
  6267. const filterRegdateLimit = this.settings.filterRegdateLimit;
  6268.  
  6269. // 未启用则跳过
  6270. if (filterRegdateLimit <= 0) {
  6271. return;
  6272. }
  6273.  
  6274. // 没有用户信息,优先从页面上获取
  6275. if (item.userInfo === undefined) {
  6276. this.getUserInfo(item);
  6277. }
  6278.  
  6279. // 没有再从接口获取
  6280. if (item.userInfo === undefined) {
  6281. await this.getPostInfo(item);
  6282. }
  6283.  
  6284. // 获取注册时间
  6285. const { regdate } = item.userInfo || {};
  6286.  
  6287. // 获取失败则跳过
  6288. if (regdate === undefined) {
  6289. return;
  6290. }
  6291.  
  6292. // 转换时间格式,泥潭接口只精确到秒
  6293. const date = new Date(regdate * 1000);
  6294.  
  6295. // 判断是否符合条件
  6296. if (Date.now() - date > filterRegdateLimit) {
  6297. return;
  6298. }
  6299.  
  6300. // 更新过滤模式和原因
  6301. result.mode = mode;
  6302. result.reason = `注册时间: ${date.toLocaleDateString()}`;
  6303. }
  6304.  
  6305. /**
  6306. * 根据发帖数量过滤
  6307. * @param {*} item 绑定的 nFilter
  6308. * @param {*} result 过滤结果
  6309. */
  6310. async filterByPostnum(item, result) {
  6311. const { uid } = item;
  6312.  
  6313. // 如果是匿名,则跳过
  6314. if (uid <= 0) {
  6315. return;
  6316. }
  6317.  
  6318. // 获取隐藏模式下标
  6319. const mode = this.settings.getModeByName("隐藏");
  6320.  
  6321. // 如果当前模式不低于隐藏模式,则跳过
  6322. if (result.mode >= mode) {
  6323. return;
  6324. }
  6325.  
  6326. // 获取发帖数量限制
  6327. const filterPostnumLimit = this.settings.filterPostnumLimit;
  6328.  
  6329. // 未启用则跳过
  6330. if (filterPostnumLimit <= 0) {
  6331. return;
  6332. }
  6333.  
  6334. // 没有用户信息,优先从页面上获取
  6335. if (item.userInfo === undefined) {
  6336. this.getUserInfo(item);
  6337. }
  6338.  
  6339. // 没有再从接口获取
  6340. if (item.userInfo === undefined) {
  6341. await this.getPostInfo(item);
  6342. }
  6343.  
  6344. // 获取发帖数量
  6345. const { postnum } = item.userInfo || {};
  6346.  
  6347. // 获取失败则跳过
  6348. if (postnum === undefined) {
  6349. return;
  6350. }
  6351.  
  6352. // 判断是否符合条件
  6353. if (postnum >= filterPostnumLimit) {
  6354. return;
  6355. }
  6356.  
  6357. // 更新过滤模式和原因
  6358. result.mode = mode;
  6359. result.reason = `发帖数量: ${postnum}`;
  6360. }
  6361.  
  6362. /**
  6363. * 根据发帖比例过滤
  6364. * @param {*} item 绑定的 nFilter
  6365. * @param {*} result 过滤结果
  6366. */
  6367. async filterByTopicRate(item, result) {
  6368. const { uid } = item;
  6369.  
  6370. // 如果是匿名,则跳过
  6371. if (uid <= 0) {
  6372. return;
  6373. }
  6374.  
  6375. // 获取隐藏模式下标
  6376. const mode = this.settings.getModeByName("隐藏");
  6377.  
  6378. // 如果当前模式不低于隐藏模式,则跳过
  6379. if (result.mode >= mode) {
  6380. return;
  6381. }
  6382.  
  6383. // 获取发帖比例限制
  6384. const filterTopicRateLimit = this.settings.filterTopicRateLimit;
  6385.  
  6386. // 未启用则跳过
  6387. if (filterTopicRateLimit <= 0 || filterTopicRateLimit >= 100) {
  6388. return;
  6389. }
  6390.  
  6391. // 没有用户信息,优先从页面上获取
  6392. if (item.userInfo === undefined) {
  6393. this.getUserInfo(item);
  6394. }
  6395.  
  6396. // 没有再从接口获取
  6397. if (item.userInfo === undefined) {
  6398. await this.getPostInfo(item);
  6399. }
  6400.  
  6401. // 获取发帖数量
  6402. const { postnum } = item.userInfo || {};
  6403.  
  6404. // 获取失败则跳过
  6405. if (postnum === undefined) {
  6406. return;
  6407. }
  6408.  
  6409. // 获取主题数量
  6410. const topicNum = await this.getTopicNum(item);
  6411.  
  6412. // 计算发帖比例
  6413. const topicRate = Math.ceil((topicNum / postnum) * 100);
  6414.  
  6415. // 判断是否符合条件
  6416. if (topicRate < filterTopicRateLimit) {
  6417. return;
  6418. }
  6419.  
  6420. // 更新过滤模式和原因
  6421. result.mode = mode;
  6422. result.reason = `发帖比例: ${topicRate}% (${topicNum}/${postnum})`;
  6423. }
  6424.  
  6425. /**
  6426. * 根据版面声望过滤
  6427. * @param {*} item 绑定的 nFilter
  6428. * @param {*} result 过滤结果
  6429. */
  6430. async filterByReputation(item, result) {
  6431. const { uid } = item;
  6432.  
  6433. // 如果是匿名,则跳过
  6434. if (uid <= 0) {
  6435. return;
  6436. }
  6437.  
  6438. // 获取隐藏模式下标
  6439. const mode = this.settings.getModeByName("隐藏");
  6440.  
  6441. // 如果当前模式不低于隐藏模式,则跳过
  6442. if (result.mode >= mode) {
  6443. return;
  6444. }
  6445.  
  6446. // 获取版面声望限制
  6447. const filterReputationLimit = this.settings.filterReputationLimit;
  6448.  
  6449. // 未启用则跳过
  6450. if (Number.isNaN(filterReputationLimit)) {
  6451. return;
  6452. }
  6453.  
  6454. // 没有声望信息,优先从页面上获取
  6455. if (item.reputation === undefined) {
  6456. this.getUserInfo(item);
  6457. }
  6458.  
  6459. // 没有再从接口获取
  6460. if (item.reputation === undefined) {
  6461. await this.getPostInfo(item);
  6462. }
  6463.  
  6464. // 获取版面声望
  6465. const reputation = item.reputation || 0;
  6466.  
  6467. // 判断是否符合条件
  6468. if (reputation >= filterReputationLimit) {
  6469. return;
  6470. }
  6471.  
  6472. // 更新过滤模式和原因
  6473. result.mode = mode;
  6474. result.reason = `版面声望: ${reputation}`;
  6475. }
  6476.  
  6477. /**
  6478. * 重新过滤
  6479. */
  6480. reFilter() {
  6481. this.data.forEach((item) => {
  6482. item.execute();
  6483. });
  6484. }
  6485. }
  6486.  
  6487. /**
  6488. * 设置模块
  6489. */
  6490. class SettingsModule extends Module {
  6491. /**
  6492. * 模块名称
  6493. */
  6494. static name = "settings";
  6495.  
  6496. /**
  6497. * 顺序
  6498. */
  6499. static order = 0;
  6500.  
  6501. /**
  6502. * 创建实例
  6503. * @param {Settings} settings 设置
  6504. * @param {API} api API
  6505. * @param {UI} ui UI
  6506. * @param {Array} data 过滤列表
  6507. * @returns {Module | null} 成功后返回模块实例
  6508. */
  6509. static create(settings, api, ui, data) {
  6510. // 读取设置里的模块列表
  6511. const modules = settings.modules;
  6512.  
  6513. // 如果不包含自己,加入列表中,因为设置模块是必须的
  6514. if (modules.includes(this.name) === false) {
  6515. settings.modules = [...modules, this.name];
  6516. }
  6517.  
  6518. // 创建实例
  6519. return super.create(settings, api, ui, data);
  6520. }
  6521.  
  6522. /**
  6523. * 初始化,增加设置
  6524. */
  6525. initComponents() {
  6526. super.initComponents();
  6527.  
  6528. const { settings, ui } = this;
  6529. const { add } = ui.views.settings;
  6530.  
  6531. // 前置过滤
  6532. {
  6533. const input = ui.createElement("INPUT", [], {
  6534. type: "checkbox",
  6535. });
  6536.  
  6537. const label = ui.createElement("LABEL", ["前置过滤", input], {
  6538. style: "display: flex;",
  6539. });
  6540.  
  6541. settings.preFilterEnabled.then((checked) => {
  6542. input.checked = checked;
  6543. input.onchange = () => {
  6544. settings.preFilterEnabled = !checked;
  6545. };
  6546. });
  6547.  
  6548. add(this.constructor.order + 0, label);
  6549. }
  6550.  
  6551. // 模块选择
  6552. {
  6553. const modules = [
  6554. ListModule,
  6555. UserModule,
  6556. TagModule,
  6557. KeywordModule,
  6558. LocationModule,
  6559. HunterModule,
  6560. MiscModule,
  6561. ];
  6562.  
  6563. const items = modules.map((item) => {
  6564. const input = ui.createElement("INPUT", [], {
  6565. type: "checkbox",
  6566. value: item.name,
  6567. checked: settings.modules.includes(item.name),
  6568. onchange: () => {
  6569. const checked = input.checked;
  6570.  
  6571. modules.map((m, index) => {
  6572. const isDepend = checked
  6573. ? item.depends.find((i) => i.name === m.name)
  6574. : m.depends.find((i) => i.name === item.name);
  6575.  
  6576. if (isDepend) {
  6577. const element = items[index].querySelector("INPUT");
  6578.  
  6579. if (element) {
  6580. element.checked = checked;
  6581. }
  6582. }
  6583. });
  6584. },
  6585. });
  6586.  
  6587. const label = ui.createElement("LABEL", [item.label, input], {
  6588. style: "display: flex; margin-right: 10px;",
  6589. });
  6590.  
  6591. return label;
  6592. });
  6593.  
  6594. const button = ui.createButton("确认", () => {
  6595. const checked = group.querySelectorAll("INPUT:checked");
  6596. const values = [...checked].map((item) => item.value);
  6597.  
  6598. settings.modules = values;
  6599.  
  6600. location.reload();
  6601. });
  6602.  
  6603. const group = ui.createElement("DIV", [...items, button], {
  6604. style: "display: flex;",
  6605. });
  6606.  
  6607. const label = ui.createElement("LABEL", "启用模块");
  6608.  
  6609. add(this.constructor.order + 1, label, group);
  6610. }
  6611.  
  6612. // 默认过滤模式
  6613. {
  6614. const modes = ["标记", "遮罩", "隐藏"].map((item) => {
  6615. const input = ui.createElement("INPUT", [], {
  6616. type: "radio",
  6617. name: "defaultFilterMode",
  6618. value: item,
  6619. checked: settings.defaultFilterMode === item,
  6620. onchange: () => {
  6621. settings.defaultFilterMode = item;
  6622.  
  6623. this.reFilter();
  6624. },
  6625. });
  6626.  
  6627. const label = ui.createElement("LABEL", [item, input], {
  6628. style: "display: flex; margin-right: 10px;",
  6629. });
  6630.  
  6631. return label;
  6632. });
  6633.  
  6634. const group = ui.createElement("DIV", modes, {
  6635. style: "display: flex;",
  6636. });
  6637.  
  6638. const label = ui.createElement("LABEL", "默认过滤模式");
  6639.  
  6640. const tips = ui.createElement("DIV", TIPS.filterMode, {
  6641. className: "silver",
  6642. });
  6643.  
  6644. add(this.constructor.order + 2, label, group, tips);
  6645. }
  6646. }
  6647.  
  6648. /**
  6649. * 重新过滤
  6650. */
  6651. reFilter() {
  6652. // 目前仅在修改默认过滤模式时重新过滤
  6653. this.data.forEach((item) => {
  6654. // 如果过滤模式是继承,则重新过滤
  6655. if (item.filterMode === "继承") {
  6656. item.execute();
  6657. }
  6658.  
  6659. // 如果有引用,也重新过滤
  6660. if (Object.values(item.quotes || {}).includes("继承")) {
  6661. item.execute();
  6662. return;
  6663. }
  6664. });
  6665. }
  6666. }
  6667.  
  6668. /**
  6669. * 增强的列表模块,增加了用户作为附加模块
  6670. */
  6671. class ListEnhancedModule extends ListModule {
  6672. /**
  6673. * 模块名称
  6674. */
  6675. static name = "list";
  6676.  
  6677. /**
  6678. * 附加模块
  6679. */
  6680. static addons = [UserModule];
  6681.  
  6682. /**
  6683. * 附加的用户模块
  6684. * @returns {UserModule} 用户模块
  6685. */
  6686. get userModule() {
  6687. return this.addons[UserModule.name];
  6688. }
  6689.  
  6690. /**
  6691. * 表格列
  6692. * @returns {Array} 表格列集合
  6693. */
  6694. columns() {
  6695. const hasAddon = this.hasAddon(UserModule);
  6696.  
  6697. if (hasAddon === false) {
  6698. return super.columns();
  6699. }
  6700.  
  6701. return [
  6702. { label: "用户", width: 1 },
  6703. { label: "内容", ellipsis: true },
  6704. { label: "过滤模式", center: true, width: 1 },
  6705. { label: "原因", width: 1 },
  6706. { label: "操作", width: 1 },
  6707. ];
  6708. }
  6709.  
  6710. /**
  6711. * 表格项
  6712. * @param {*} item 绑定的 nFilter
  6713. * @returns {Array} 表格项集合
  6714. */
  6715. column(item) {
  6716. const column = super.column(item);
  6717.  
  6718. const hasAddon = this.hasAddon(UserModule);
  6719.  
  6720. if (hasAddon === false) {
  6721. return column;
  6722. }
  6723.  
  6724. const { ui } = this;
  6725. const { table } = this.views;
  6726. const { uid, username } = item;
  6727.  
  6728. const user = this.userModule.format(uid, username);
  6729.  
  6730. const buttons = (() => {
  6731. if (uid <= 0) {
  6732. return null;
  6733. }
  6734.  
  6735. const block = ui.createButton("屏蔽", (e) => {
  6736. this.userModule.renderDetails(uid, username, (type) => {
  6737. // 删除失效数据,等待重新过滤
  6738. table.remove(e);
  6739.  
  6740. // 如果是新增,不会因为用户重新过滤,需要主动触发
  6741. if (type === "ADD") {
  6742. this.userModule.reFilter(uid);
  6743. }
  6744. });
  6745. });
  6746.  
  6747. return ui.createButtonGroup(block);
  6748. })();
  6749.  
  6750. return [user, ...column, buttons];
  6751. }
  6752. }
  6753.  
  6754. /**
  6755. * 增强的用户模块,增加了标记作为附加模块
  6756. */
  6757. class UserEnhancedModule extends UserModule {
  6758. /**
  6759. * 模块名称
  6760. */
  6761. static name = "user";
  6762.  
  6763. /**
  6764. * 附加模块
  6765. */
  6766. static addons = [TagModule];
  6767.  
  6768. /**
  6769. * 附加的标记模块
  6770. * @returns {TagModule} 标记模块
  6771. */
  6772. get tagModule() {
  6773. return this.addons[TagModule.name];
  6774. }
  6775.  
  6776. /**
  6777. * 表格列
  6778. * @returns {Array} 表格列集合
  6779. */
  6780. columns() {
  6781. const hasAddon = this.hasAddon(TagModule);
  6782.  
  6783. if (hasAddon === false) {
  6784. return super.columns();
  6785. }
  6786.  
  6787. return [
  6788. { label: "昵称", width: 1 },
  6789. { label: "标记" },
  6790. { label: "过滤模式", center: true, width: 1 },
  6791. { label: "操作", width: 1 },
  6792. ];
  6793. }
  6794.  
  6795. /**
  6796. * 表格项
  6797. * @param {*} item 用户信息
  6798. * @returns {Array} 表格项集合
  6799. */
  6800. column(item) {
  6801. const column = super.column(item);
  6802.  
  6803. const hasAddon = this.hasAddon(TagModule);
  6804.  
  6805. if (hasAddon === false) {
  6806. return column;
  6807. }
  6808.  
  6809. const { ui } = this;
  6810. const { table } = this.views;
  6811. const { id, name } = item;
  6812.  
  6813. const tags = ui.createElement(
  6814. "DIV",
  6815. item.tags.map((id) => this.tagModule.format(id))
  6816. );
  6817.  
  6818. const newColumn = [...column];
  6819.  
  6820. newColumn.splice(1, 0, tags);
  6821.  
  6822. const buttons = column[column.length - 1];
  6823.  
  6824. const update = ui.createButton("编辑", (e) => {
  6825. this.renderDetails(id, name, (type, newValue) => {
  6826. if (type === "UPDATE") {
  6827. table.update(e, ...this.column(newValue));
  6828. }
  6829.  
  6830. if (type === "REMOVE") {
  6831. table.remove(e);
  6832. }
  6833. });
  6834. });
  6835.  
  6836. buttons.insertBefore(update, buttons.firstChild);
  6837.  
  6838. return newColumn;
  6839. }
  6840.  
  6841. /**
  6842. * 渲染详情
  6843. * @param {Number} uid 用户 ID
  6844. * @param {String | undefined} name 用户名称
  6845. * @param {Function} callback 回调函数
  6846. */
  6847. renderDetails(uid, name, callback = () => {}) {
  6848. const hasAddon = this.hasAddon(TagModule);
  6849.  
  6850. if (hasAddon === false) {
  6851. return super.renderDetails(uid, name, callback);
  6852. }
  6853.  
  6854. const { ui, settings } = this;
  6855.  
  6856. // 只允许同时存在一个详情页
  6857. if (this.views.details) {
  6858. if (this.views.details.parentNode) {
  6859. this.views.details.parentNode.removeChild(this.views.details);
  6860. }
  6861. }
  6862.  
  6863. // 获取用户信息
  6864. const user = this.get(uid);
  6865.  
  6866. if (user) {
  6867. name = user.name;
  6868. }
  6869.  
  6870. // TODO 需要优化
  6871.  
  6872. const title =
  6873. (user ? "编辑" : "添加") + `用户 - ${name ? name : "#" + uid}`;
  6874.  
  6875. const table = ui.createTable([]);
  6876.  
  6877. {
  6878. const size = Math.floor((screen.width * 0.8) / 200);
  6879.  
  6880. const items = Object.values(this.tagModule.list).map(({ id }) => {
  6881. const checked = user && user.tags.includes(id) ? "checked" : "";
  6882.  
  6883. return `
  6884. <td class="c1">
  6885. <label for="s-tag-${id}" style="display: block; cursor: pointer;">
  6886. ${this.tagModule.format(id).outerHTML}
  6887. </label>
  6888. </td>
  6889. <td class="c2" width="1">
  6890. <input id="s-tag-${id}" type="checkbox" value="${id}" ${checked}/>
  6891. </td>
  6892. `;
  6893. });
  6894.  
  6895. const rows = [...new Array(Math.ceil(items.length / size))].map(
  6896. (_, index) => `
  6897. <tr class="row${(index % 2) + 1}">
  6898. ${items.slice(size * index, size * (index + 1)).join("")}
  6899. </tr>
  6900. `
  6901. );
  6902.  
  6903. table.querySelector("TBODY").innerHTML = rows.join("");
  6904. }
  6905.  
  6906. const input = ui.createElement("INPUT", [], {
  6907. type: "text",
  6908. placeholder: TIPS.addTags,
  6909. style: "width: -webkit-fill-available;",
  6910. });
  6911.  
  6912. const inputWrapper = ui.createElement("DIV", input, {
  6913. style: "margin-top: 10px;",
  6914. });
  6915.  
  6916. const filterMode = user ? user.filterMode : settings.filterModes[0];
  6917.  
  6918. const switchMode = ui.createButton(filterMode, () => {
  6919. const newMode = settings.switchModeByName(switchMode.innerText);
  6920.  
  6921. switchMode.innerText = newMode;
  6922. });
  6923.  
  6924. const buttons = ui.createElement(
  6925. "DIV",
  6926. (() => {
  6927. const remove = user
  6928. ? ui.createButton("删除", () => {
  6929. ui.confirm().then(() => {
  6930. this.remove(uid);
  6931.  
  6932. this.views.details._.hide();
  6933.  
  6934. callback("REMOVE");
  6935. });
  6936. })
  6937. : null;
  6938.  
  6939. const save = ui.createButton("保存", () => {
  6940. const checked = [...table.querySelectorAll("INPUT:checked")].map(
  6941. (input) => parseInt(input.value, 10)
  6942. );
  6943.  
  6944. const newTags = input.value
  6945. .split("|")
  6946. .filter((item) => item.length)
  6947. .map((item) => this.tagModule.add(item))
  6948. .filter((tag) => tag !== null)
  6949. .map((tag) => tag.id);
  6950.  
  6951. const tags = [...new Set([...checked, ...newTags])].sort();
  6952.  
  6953. if (user === null) {
  6954. const entity = this.add(uid, {
  6955. id: uid,
  6956. name,
  6957. tags,
  6958. filterMode: switchMode.innerText,
  6959. });
  6960.  
  6961. this.views.details._.hide();
  6962.  
  6963. callback("ADD", entity);
  6964. } else {
  6965. const entity = this.update(uid, {
  6966. name,
  6967. tags,
  6968. filterMode: switchMode.innerText,
  6969. });
  6970.  
  6971. this.views.details._.hide();
  6972.  
  6973. callback("UPDATE", entity);
  6974. }
  6975. });
  6976.  
  6977. return ui.createButtonGroup(remove, save);
  6978. })(),
  6979. {
  6980. className: "right_",
  6981. }
  6982. );
  6983.  
  6984. const actions = ui.createElement(
  6985. "DIV",
  6986. [ui.createElement("SPAN", "过滤模式:"), switchMode, buttons],
  6987. {
  6988. style: "margin-top: 10px;",
  6989. }
  6990. );
  6991.  
  6992. const tips = ui.createElement("DIV", TIPS.filterMode, {
  6993. className: "silver",
  6994. style: "margin-top: 10px;",
  6995. });
  6996.  
  6997. const content = ui.createElement(
  6998. "DIV",
  6999. [table, inputWrapper, actions, tips],
  7000. {
  7001. style: "width: 80vw",
  7002. }
  7003. );
  7004.  
  7005. // 创建弹出框
  7006. this.views.details = ui.createDialog(null, title, content);
  7007. }
  7008. }
  7009.  
  7010. /**
  7011. * 处理 topicArg 模块
  7012. * @param {Filter} filter 过滤器
  7013. * @param {*} value commonui.topicArg
  7014. */
  7015. const handleTopicModule = async (filter, value) => {
  7016. // 绑定主题模块
  7017. topicModule = value;
  7018.  
  7019. // 是否启用前置过滤
  7020. const preFilterEnabled = await filter.settings.preFilterEnabled;
  7021.  
  7022. // 前置过滤
  7023. // 先直接隐藏,等过滤完毕后再放出来
  7024. const beforeGet = (...args) => {
  7025. if (preFilterEnabled) {
  7026. // 主题标题
  7027. const title = document.getElementById(args[1]);
  7028.  
  7029. // 主题容器
  7030. const container = title.closest("tr");
  7031.  
  7032. // 隐藏元素
  7033. container.style.display = "none";
  7034. }
  7035.  
  7036. return args;
  7037. };
  7038.  
  7039. // 过滤
  7040. const afterGet = (_, args) => {
  7041. // 主题 ID
  7042. const tid = args[8];
  7043.  
  7044. // 找到对应数据
  7045. const data = topicModule.data.find((item) => item[8] === tid);
  7046.  
  7047. // 开始过滤
  7048. if (data) {
  7049. filter.filterTopic(data);
  7050. }
  7051. };
  7052.  
  7053. // 如果已经有数据,则直接过滤
  7054. Object.values(topicModule.data).forEach(filter.filterTopic);
  7055.  
  7056. // 拦截 add 函数,这是泥潭的主题添加事件
  7057. Tools.interceptProperty(topicModule, "add", {
  7058. beforeGet,
  7059. afterGet,
  7060. });
  7061. };
  7062.  
  7063. /**
  7064. * 处理 postArg 模块
  7065. * @param {Filter} filter 过滤器
  7066. * @param {*} value commonui.postArg
  7067. */
  7068. const handleReplyModule = async (filter, value) => {
  7069. // 绑定回复模块
  7070. replyModule = value;
  7071.  
  7072. // 是否启用前置过滤
  7073. const preFilterEnabled = await filter.settings.preFilterEnabled;
  7074.  
  7075. // 前置过滤
  7076. // 先直接隐藏,等过滤完毕后再放出来
  7077. const beforeGet = (...args) => {
  7078. if (preFilterEnabled) {
  7079. // 楼层号
  7080. const index = args[0];
  7081.  
  7082. // 判断是否是楼层
  7083. const isFloor = typeof index === "number";
  7084.  
  7085. // 评论额外标签
  7086. const prefix = isFloor ? "" : "comment";
  7087.  
  7088. // 用户容器
  7089. const uInfoC = document.querySelector(`#${prefix}posterinfo${index}`);
  7090.  
  7091. // 回复容器
  7092. const container = isFloor
  7093. ? uInfoC.closest("tr")
  7094. : uInfoC.closest(".comment_c");
  7095.  
  7096. // 隐藏元素
  7097. container.style.display = "none";
  7098. }
  7099.  
  7100. return args;
  7101. };
  7102.  
  7103. // 过滤
  7104. const afterGet = (_, args) => {
  7105. // 楼层号
  7106. const index = args[0];
  7107.  
  7108. // 找到对应数据
  7109. const data = replyModule.data[index];
  7110.  
  7111. // 开始过滤
  7112. if (data) {
  7113. filter.filterReply(data);
  7114. }
  7115. };
  7116.  
  7117. // 如果已经有数据,则直接过滤
  7118. Object.values(replyModule.data).forEach(filter.filterReply);
  7119.  
  7120. // 拦截 proc 函数,这是泥潭的回复添加事件
  7121. Tools.interceptProperty(replyModule, "proc", {
  7122. beforeGet,
  7123. afterGet,
  7124. });
  7125. };
  7126.  
  7127. /**
  7128. * 处理 commonui 模块
  7129. * @param {Filter} filter 过滤器
  7130. * @param {*} value commonui
  7131. */
  7132. const handleCommonui = (filter, value) => {
  7133. // 绑定主模块
  7134. commonui = value;
  7135.  
  7136. // 拦截 mainMenu 模块,UI 需要在 init 后加载
  7137. Tools.interceptProperty(commonui, "mainMenu", {
  7138. afterSet: (value) => {
  7139. Tools.interceptProperty(value, "init", {
  7140. afterGet: () => {
  7141. filter.ui.render();
  7142. },
  7143. afterSet: () => {
  7144. filter.ui.render();
  7145. },
  7146. });
  7147. },
  7148. });
  7149.  
  7150. // 拦截 topicArg 模块,这是泥潭的主题入口
  7151. Tools.interceptProperty(commonui, "topicArg", {
  7152. afterSet: (value) => {
  7153. handleTopicModule(filter, value);
  7154. },
  7155. });
  7156.  
  7157. // 拦截 postArg 模块,这是泥潭的回复入口
  7158. Tools.interceptProperty(commonui, "postArg", {
  7159. afterSet: (value) => {
  7160. handleReplyModule(filter, value);
  7161. },
  7162. });
  7163. };
  7164.  
  7165. /**
  7166. * 注册脚本菜单
  7167. * @param {Settings} settings 设置
  7168. */
  7169. const registerMenu = async (settings) => {
  7170. // 修改 UA
  7171. {
  7172. const userAgent = await settings.userAgent;
  7173.  
  7174. GM_registerMenuCommand(`修改UA${userAgent}`, () => {
  7175. const value = prompt("修改UA", userAgent);
  7176.  
  7177. if (value) {
  7178. settings.userAgent = value;
  7179. }
  7180. });
  7181. }
  7182.  
  7183. // 前置过滤
  7184. {
  7185. const enabled = await settings.preFilterEnabled;
  7186.  
  7187. GM_registerMenuCommand(`前置过滤:${enabled ? "是" : "否"}`, () => {
  7188. settings.preFilterEnabled = !enabled;
  7189. });
  7190. }
  7191. };
  7192.  
  7193. // 主函数
  7194. (async () => {
  7195. // 初始化缓存、设置
  7196. const cache = new Cache(API.modules);
  7197. const settings = new Settings(cache);
  7198.  
  7199. // 读取设置
  7200. await settings.load();
  7201.  
  7202. // 初始化 API、UI
  7203. const api = new API(cache, settings);
  7204. const ui = new UI(settings, api);
  7205.  
  7206. // 初始化过滤器
  7207. const filter = new Filter(settings, api, ui);
  7208.  
  7209. // 加载模块
  7210. filter.initModules(
  7211. SettingsModule,
  7212. ListEnhancedModule,
  7213. UserEnhancedModule,
  7214. TagModule,
  7215. KeywordModule,
  7216. LocationModule,
  7217. HunterModule,
  7218. MiscModule
  7219. );
  7220.  
  7221. // 注册脚本菜单
  7222. registerMenu(settings);
  7223.  
  7224. // 处理 commonui 模块
  7225. if (unsafeWindow.commonui) {
  7226. handleCommonui(filter, unsafeWindow.commonui);
  7227. return;
  7228. }
  7229.  
  7230. Tools.interceptProperty(unsafeWindow, "commonui", {
  7231. afterSet: (value) => {
  7232. handleCommonui(filter, value);
  7233. },
  7234. });
  7235. })();
  7236. })();