// ==UserScript== // @name 网页变化监控 // @name:zh-CN 网页变化监控 // @name:en Page Monitor — Watch Any Part of a Page for Changes // @namespace https://github.com/yilecoding/helper-scripts // @version 2025.10.02.9 // @description 选中页面上的一块区域(价格、库存、公告、成绩、工单状态),内容变化时通知你。 // @description:zh-CN 选中页面上的一块区域(价格、库存、公告、成绩、工单状态),内容变化时通知你。 // @description:en Pick any part of a page (price, stock, notice, grades, ticket status) and get notified when it changes. // @author 壹樂(V:ita1125) // @match *://*/* // @noframes // @grant GM_getValue // @grant GM_setValue // @grant GM_deleteValue // @grant GM_listValues // @grant GM_registerMenuCommand // @grant GM_xmlhttpRequest // @grant GM_notification // @grant GM_openInTab // @connect self // @license MIT // @run-at document-idle // @homepageURL https://github.com/yile2020/helper-scripts // @supportURL https://github.com/yile2020/helper-scripts/issues // @updateURL https://raw.githubusercontent.com/yile2020/helper-scripts/main/page-monitor/page-monitor.user.js // @downloadURL https://raw.githubusercontent.com/yile2020/helper-scripts/main/page-monitor/page-monitor.user.js // ==/UserScript== // 脚本持续更新维护,欢迎 star / watch 仓库获取最新版本: // https://github.com/yile2020/helper-scripts (function () { 'use strict'; /** 日志前缀。控制台过滤框输入「网页监控」只看本脚本。 */ const TAG = '[网页监控]'; // ========================================================================= // 配置 // ========================================================================= const CONFIG = { defaultInterval: 300, // 秒。新监控项的检查间隔 minInterval: 30, // 秒。再短就是在给对方服务器施压了 maxTextLength: 4000, // 快照文字上限(存 GM,历史里每条存新旧两份) previewLength: 60, // 列表 / 提示里显示的文字长度 tickMs: 5000, // 调度器多久看一次「谁到点了」 requestTimeout: 20000, // 抓取超时(毫秒) historySize: 20, // 每个监控项保留最近多少条变化 diffMaxCells: 1500000, // 逐词 diff 的 LCS 表上限(词数之积),超了退化成整段替换 observeDebounce: 1000, // 本页观察:DOM 安静这么久再取文字比较(毫秒) observeMaxWait: 5000, // 本页观察:持续变化时最多拖这么久也要比较一次 leaseConfirmMs: 300, // 认领一次检查后等这么久再读回来确认,别的标签同时认领时以最后写入的为准 maxBackoff: 3600, // 秒。连续失败时间隔翻倍,最多拉长到这么久(间隔本身更长的不受影响) failNotifyAfter: 3, // 连续失败这么多次发一条通知:选择器失效了用户不开面板也该知道 }; const HOST_ID = 'page-monitor-host'; /** 本标签页的身份,认领检查时写进存储。 */ const TAB_ID = Math.random().toString(36).slice(2, 10); // ========================================================================= // 存储:每个监控项一个键(mon:) // ========================================================================= /** * 不把所有监控项存成一个数组:同一 URL 开着几个标签时,每个标签都在写自己检查的那一项, * 整个数组读改写会互相覆盖(A 标签写回的数组里带着 B 标签改之前的旧值)。 * 一项一个键,各写各的。 */ const Store = (() => { const PREFIX = 'mon:'; function all() { return GM_listValues() .filter((k) => k.startsWith(PREFIX)) .map((k) => GM_getValue(k, null)) .filter(Boolean) .sort((a, b) => a.createdAt - b.createdAt || (a.id < b.id ? -1 : 1)); } function get(id) { return GM_getValue(PREFIX + id, null); } function save(mon) { GM_setValue(PREFIX + mon.id, mon); return mon; } function patch(id, fields) { const cur = get(id); if (!cur) return null; return save({ ...cur, ...fields }); } function remove(id) { GM_deleteValue(PREFIX + id); } return { all, get, save, patch, remove }; })(); /** 监控项按「去掉 hash 的 URL」归属页面:hash 多半是锚点或前端路由的小状态。 */ function pageUrl(href = location.href) { const u = new URL(href); u.hash = ''; return u.href; } function newId() { return Date.now().toString(36) + Math.random().toString(36).slice(2, 8); } // ========================================================================= // 文本 // ========================================================================= const Text = (() => { const SKIP = new Set(['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEMPLATE', 'svg', 'SVG']); const BLOCK = /^(ADDRESS|ARTICLE|ASIDE|BLOCKQUOTE|BR|DD|DIV|DL|DT|FIELDSET|FIGCAPTION|FIGURE|FOOTER|FORM|H[1-6]|HEADER|HR|LI|MAIN|NAV|OL|P|PRE|SECTION|TABLE|TBODY|TD|TFOOT|TH|THEAD|TR|UL)$/; /** * 不用 innerText:抓取型检查拿到的是 DOMParser 解析出来的文档,没有布局, * innerText 在那里退化成 textContent(连