NGA Filter

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

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

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址