Greasy Fork

Greasy Fork is available in English.

当前页面打开链接

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

当前为 2025-02-25 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         当前页面打开链接
// @namespace    http://greasyfork.icu/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);
})();