全站替换_top为_blank

将全站所有的_top替换为_blank

当前为 2025-06-20 提交的版本,查看 最新版本

// ==UserScript==
// @name          全站替换_top为_blank
// @namespace     https://example.com/
// @description   将全站所有的_top替换为_blank
// @match         *://*/*
// @version       1.0.1
// @run-at        document-start
// @license       MIT
// ==/UserScript==


(function inject() {
	console.log("脚本已注入");
	function replaceTopTargets() {
    	console.log("[replaceTopTargets]")
        document.querySelectorAll('a[target="_top"]').forEach(el => {
            el.setAttribute('target', '_blank');
        });
    }

    function setupObserver() {
    	console.log("[setupObserver]")
        const observer = new MutationObserver(replaceTopTargets);
        observer.observe(document.body, { childList: true, subtree: true });
    }

    function safeInit() {
        // 初始处理
        replaceTopTargets();
        setupObserver();
    }

    // 页面状态判断
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', safeInit);

        // ⏱ 超时保护机制:最多等 5 秒就执行
        setTimeout(() => {
            if (document.body) {
                safeInit();
            }
        }, 5000);
    } else {
        // DOM 已构建,无需等待
        safeInit();
        window.webkit.messageHandlers.domLoaded.postMessage(location.href);
    }
})();

QingJ © 2025

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