感谢GPT吧
更新一版从 移除元素改为 去除原本的逻辑直接不弹窗直接刷新页面。
- 移除元素
// ==UserScript==// @name nodeseek Block "全部标记已读成功" (mscAlert)// @namespace tm-block-mscalert// @version 1.0.0// @description 拦截“全部标记已读成功”// @match *://nodeseek.com/notification*// @match *://www.nodeseek.com/notification*// @run-at document-start// @grant none// ==/UserScript==(function () { 'use strict'; const BLOCK_TEXTS = ['全部标记已读成功']; try { let _mscAlert; Object.defineProperty(window, 'mscAlert', { configurable: true, enumerable: true, get() { return _mscAlert; }, set(fn) { if (typeof fn === 'function') { const wrapped = function (msg, ...rest) { try { if (typeof msg === 'string' && BLOCK_TEXTS.some(t => msg.includes(t))) { return; } } catch (_) {} return fn.apply(this, [msg, ...rest]); }; wrapped.__wrapped_by_tm = true; _mscAlert = wrapped; } else { _mscAlert = fn; } } }); } catch (_) { } const wrapIfPresent = () => { const m = window.mscAlert; if (typeof m === 'function' && !m.__wrapped_by_tm) { const original = m; const wrapped = function (msg, ...rest) { if (typeof msg === 'string' && BLOCK_TEXTS.some(t => msg.includes(t))) return; return original.apply(this, [msg, ...rest]); }; wrapped.__wrapped_by_tm = true; window.mscAlert = wrapped; } }; const timer = setInterval(wrapIfPresent, 50); window.addEventListener('load', () => { wrapIfPresent(); clearInterval(timer); }); const tryRemoveNode = (root) => { const nodes = []; if (root instanceof HTMLElement) { if (root.matches('.msc-content')) nodes.push(root); nodes.push(...root.querySelectorAll('.msc-content')); } for (const el of nodes) { const title = el.querySelector('.msc-title'); if (title && BLOCK_TEXTS.some(t => (title.textContent || '').includes(t))) { el.remove(); } } }; const mo = new MutationObserver(muts => { for (const m of muts) { for (const n of m.addedNodes) tryRemoveNode(n); } }); mo.observe(document.documentElement, { childList: true, subtree: true });})();- 直接修改原本的逻辑
// ==UserScript==// @name NodeSeek 通知「全部标为已读」去除弹框// @namespace https://nodeseek.com/// @version 1.1.0// @description 拦截“全部标记已读成功”// @match *://nodeseek.com/notification*// @match *://www.nodeseek.com/notification*// @run-at document-idle// @grant none// ==/UserScript==(function () { 'use strict'; // 当前路由对应的 API function getApiForRoute() { const h = location.hash || ''; if (h.startsWith('#/reply')) return '/api/notification/reply-to-me/markViewed?all=true'; if (h.startsWith('#/message')) return '/api/notification/message/markViewed?all=true'; if (h.startsWith('#/atMe')) return '/api/notification/at-me/markViewed?all=true'; return null; // 其他子页不处理 } const normalize = s => (s || '').trim().replace(/\s+/g, ''); // 劫持并替换按钮(用 clone 去掉原来的所有监听) function hookButtons(root = document) { const api = getApiForRoute(); if (!api) return; const allBtns = root.querySelectorAll('button.btn'); allBtns.forEach(btn => { if (btn.dataset.nsHooked === '1') return; if (normalize(btn.textContent) !== '全部标为已读') return; const clone = btn.cloneNode(true); clone.dataset.nsHooked = '1'; clone.addEventListener('click', async (e) => { e.preventDefault(); e.stopPropagation(); // 简单的进行中状态 const oldText = clone.textContent; clone.disabled = true; clone.textContent = '处理中…'; try { const res = await fetch(api, { method: 'POST', credentials: 'include', // 带上 cookie headers: { 'Accept': 'application/json, text/plain, */*' } }); if (!res.ok) throw new Error('HTTP ' + res.status); // 成功 -> 刷新 location.reload(); } catch (err) { console.error('[NodeSeek hook] 标记失败:', err); clone.disabled = false; clone.textContent = '失败,重试?'; alert('标记失败:' + err.message); // 失败就还原按钮文案 setTimeout(() => { if (!clone.disabled) clone.textContent = oldText; }, 1500); } }, true); btn.replaceWith(clone); }); } // 初始尝试 hookButtons(); // 监听 hash 路由变化(SPA) window.addEventListener('hashchange', () => setTimeout(hookButtons, 0)); // 监听 DOM 变化(Vue 重新渲染时重新劫持) const mo = new MutationObserver((mutations) => { for (const m of mutations) { if (m.addedNodes && m.addedNodes.length) { hookButtons(document); break; } } }); mo.observe(document.documentElement, { childList: true, subtree: true });})();
评论 (0)