当页开链

全站通用型页内增强脚本(精简版)

目前为 2025-02-27 提交的版本。查看 最新版本

// ==UserScript==
// @name         当页开链
// @namespace    
// @version      3.3
// @description  全站通用型页内增强脚本(精简版)
// @author       none
// @match        *://*/*
// @grant        unsafeWindow
// @run-at       document-body
// ==/UserScript==

(function() {
    'use strict';

    // 协议安全名单
    const SAFE_PROTOCOLS = new Set([
        'http:', 'https:', 'ftp:',
        'mailto:', 'tel:', 'sms:'
    ]);

    // Shadow DOM处理器
    const processShadowRoot = (root) => {
        const stack = [root];
        while (stack.length) {
            const current = stack.pop();
            current.querySelectorAll('a').forEach(processElement);
            current.querySelectorAll('*').forEach(node => {
                node.shadowRoot && stack.push(node.shadowRoot)
            });
        }
    };

    // 元素处理逻辑
    const processElement = (element) => {
        // 协议过滤
        try {
            const href = element.href?.toLowerCase() || '';
            const protocol = new URL(href, location.href).protocol;
            if (!SAFE_PROTOCOLS.has(protocol)) return;
        } catch(e) {
            return;
        }

        // 清理链接属性
        ['target', 'onclick', 'data-target'].forEach(attr => {
            element.removeAttribute(attr);
        });
    };

    // 智能观察器
    const initObserver = () => {
        const processed = new WeakSet();
        let pending = new Set();
        let isProcessing = false;

        const batchProcess = () => {
            const temp = new Set(pending);
            pending.clear();
            temp.forEach(node => {
                if (node.nodeType === 1 && !processed.has(node)) {
                    processElement(node);
                    processed.add(node);
                    processShadowRoot(node);
                }
            });
            isProcessing = false;
        };

        new MutationObserver(mutations => {
            mutations.forEach(({ addedNodes }) => {
                addedNodes.forEach(node => {
                    if (node.nodeType === 1) {
                        pending.add(node);
                    } else if (node.nodeType === 11) {
                        node.querySelectorAll('a').forEach(a => pending.add(a));
                    }
                });
            });

            if (!isProcessing && pending.size) {
                isProcessing = true;
                (requestIdleCallback || requestAnimationFrame)(batchProcess);
            }
        }).observe(document, {
            childList: true,
            subtree: true,
            attributes: true,
            attributeFilter: ['target', 'href']
        });
    };

    // 核心初始化
    const main = () => {
        if (window.self !== window.top) return;

        // 基础策略
        if (!document.querySelector('base')) {
            document.head.prepend(
                Object.assign(document.createElement('base'), {
                    target: '_self'
                })
            );
        }

        // 初始处理
        document.querySelectorAll('a').forEach(processElement);
        processShadowRoot(document);
        initObserver();

        // 全局事件拦截
        document.addEventListener('click', e => {
            const target = e.composedPath()[0];
            target.tagName === 'A' && processElement(target);
        }, { capture: true });

        // 处理window.open
        unsafeWindow.open = function(url) {
            window.location.href = url;
        };
    };

    // 启动逻辑
    document.readyState === 'complete' ? main() :
    document.addEventListener('DOMContentLoaded', main);
})();

QingJ © 2025

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