禁止打开新标签

强制所有顽固跳转的链接都在当前标签页打开

目前為 2025-07-21 提交的版本,檢視 最新版本

// ==UserScript==
// @name         禁止打开新标签
// @description  强制所有顽固跳转的链接都在当前标签页打开
// @version      1.5
// @author       WJ
// @match        *://*/*
// @license      MIT
// @grant        none
// @namespace https://greasyfork.org/users/914996
// ==/UserScript==

(function() {
    'use strict';

    // 修正 base 设置
    (document.querySelector('base') || document.head.appendChild(document.createElement('base'))).target = '_self';

    // 处理所有 <a>
    const set = n => n.tagName === 'A' && (n.target = '_self');
    const walk = r => r.querySelectorAll?.('a')?.forEach(set);

    walk(document);
    new MutationObserver(rs => rs.forEach(r => r.addedNodes.forEach(n => {
        set(n); walk(n);
    }))).observe(document, { childList: true, subtree: true });

    // 拦截 window.open
    window.open = (u, _, __) => (u && (location.href = u), window);
})();