当前页面打开链接

综合处理链接新窗口打开及搜索框回车提交(排除javascript:;链接)

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

// ==UserScript==
// @name         当前页面打开链接
// @namespace    https://gf.qytechs.cn/zh-CN/scripts/497533
// @version      1.4
// @description  综合处理链接新窗口打开及搜索框回车提交(排除javascript:;链接)
// @author       YourName
// @match        *://*/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    // 判断是否应该跳过处理的函数
    const shouldSkip = (element) => {
        return element.getAttribute('href')?.toLowerCase() === 'javascript:;';
    };

    // 通用属性清理函数(2025优化版)
    const cleanAttributes = (element) => {
        if (shouldSkip(element)) return;
        element.removeAttribute('target');
        element.removeAttribute('onclick');
        element.removeAttribute('onmousedown');
    };

    // 初始清理所有链接(逆向遍历提升性能)
    document.querySelectorAll('a').forEach(link => {
        if (!shouldSkip(link)) cleanAttributes(link);
    });

    // 动态元素处理器(支持Shadow DOM)
    const handleDynamicElements = (nodes) => {
        nodes.forEach(node => {
            if (node.nodeType === 1) {
                // 处理链接元素
                if (node.matches('a') && !shouldSkip(node)) {
                    cleanAttributes(node);
                }

                // 处理表单元素(2025新增)
                if (node.matches('input[type="text"], input[type="search"], textarea')) {
                    node.addEventListener('keydown', function(e) {
                        if (e.key === 'Enter') {
                            const form = node.closest('form');
                            if (form) {
                                form.target = '_self';
                                form.removeAttribute('target');
                            }
                        }
                    });
                }
            }
        });
    };

    // MutationObserver配置(增强版)
    const observer = new MutationObserver(mutations => {
        mutations.forEach(({ addedNodes }) => handleDynamicElements(addedNodes));
    });

    observer.observe(document, {
        childList: true,
        subtree: true,
        attributeFilter: ['target', 'onclick']
    });

    // 全局点击拦截(2025升级版)
    document.addEventListener('click', function(e) {
        const target = e.composedPath()[0];
        if (target.matches('a') && !shouldSkip(target)) {
            cleanAttributes(target);
        }
    }, true);

    // 键盘事件拦截(2025新增功能)
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Enter' && !e.ctrlKey && !e.metaKey) {
            const activeElement = document.activeElement;
            if (activeElement.matches('input[type="text"], input[type="search"], textarea')) {
                // 实时处理form属性
                const form = activeElement.closest('form');
                if (form) {
                    form.target = '_self';
                    form.removeAttribute('target');
                }

                // 防御性处理异步属性添加
                requestAnimationFrame(() => {
                    if (form && form.getAttribute('target') === '_blank') {
                        form.removeAttribute('target');
                    }
                });
            }
        }
    }, true);

    // 表单提交拦截(新增防御层)
    document.addEventListener('submit', function(e) {
        e.target.target = '_self';
    }, true);
})();

QingJ © 2025

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