强制所有顽固跳转的链接都在当前标签页打开
当前为
// ==UserScript==
// @name 禁止打开新标签
// @description 强制所有顽固跳转的链接都在当前标签页打开
// @version 1.0
// @author WJ
// @match *://*/*
// @license MIT
// @grant none
// @run-at document-end
// @namespace https://greasyfork.org/users/914996
// ==/UserScript==
(() => {
'use strict';
// 1. 修正 base 设置
(document.querySelector('base') || document.head.appendChild(
document.createElement('base'))).target = '_self';
// 2. 处理所有现有链接
document.querySelectorAll('a').forEach(a => a.target = '_self');
// 3. 监听动态内容
new MutationObserver(records =>
records.forEach(r => r.addedNodes.forEach(n => {
if (n.nodeType === 1) {
if (n.tagName === 'A') n.target = '_self';
n.querySelectorAll?.('a').forEach(a => a.target = '_self');
}
}))
).observe(document, { childList: true, subtree: true });
// 4. 拦截 window.open
window.open = (u, _, __) => (u && (location.href = u), window);
})();