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

QingJ © 2025

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