Greasy Fork

来自缓存

Greasy Fork is available in English.

当页开链(支持Alt禁用)

当前页面打开链接,按住Alt键时临时禁用此功能

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        当页开链(支持Alt禁用)
// @namespace   -
// @match       *://*/*
// @exclude-match    *://www.gamer520.com/*
// @exclude-match    *://bray.tech/*
// @grant       none
// @version     5.7
// @author      -
// @description 当前页面打开链接,按住Alt键时临时禁用此功能
// @run-at document-start
// ==/UserScript==

(() => {
    // Alt键状态跟踪
    let altPressed = false;

    // 监听Alt键按下和释放
    document.addEventListener('keydown', e => {
        if (e.key === 'Alt') {
            altPressed = true;
        }
    });

    document.addEventListener('keyup', e => {
        if (e.key === 'Alt') {
            altPressed = false;
        }
    });

    // 窗口失焦时重置Alt状态
    window.addEventListener('blur', () => {
        altPressed = false;
    });

const shouldExcludeElement = (target) => {
    const EXCLUDE_SELECTORS = `
        [href^="javascript"]
        #ks
        .bpx-player-ending-content
        .carousel-wrap
        #sb_form
        .swiper-wrapper
        .win-wapper
        [class="hidden xl:flex space-x-4 items-center"]
        [class="flex items-start justify-between"]
        [role="button"]
        [class="pager__btn pager__btn__prev"]
        [class="pager__btn pager__btn__next"]
        [id="download_tab_cont"]
        [class^="SourceListItem__name"]
        [class^="file-tree-btn"]
        [id="send_sms_code"]
        [class="comment-reply-link"]
        [class="download-buttons-container"]
        [id="rdgenconseemore"]
        [class="tf_btn-area__Nyc0o"]
        [class="pc_btn-item__byhSS"]

    `.trim().split('\n').map(s => s.trim()).filter(Boolean);

    return EXCLUDE_SELECTORS.some(selector => target.closest(selector));
};

    // 修改点击事件监听器,添加Alt键检查
    document.addEventListener('click', function(event) {
        // 如果Alt键被按下,阻止事件传播并返回(允许默认行为)
        if (altPressed) {
            event.preventDefault();
            event.stopPropagation();
            return;
        }

        // 使用Event.composedPath()获取精确目标(2025推荐)
        const preciseTarget = event.composedPath()[0];

        if (shouldExcludeElement(preciseTarget)) return true;

        // 动态节点溯源(兼容Shadow DOM)
        let node = preciseTarget;
        while (node && node.tagName !== 'A') {
            node = node.parentElement || node.host; // 处理Web Components场景
        }

        // 增强型链接处理
        if (node?.tagName === 'A') {
            // 最新安全策略(2025-04)
            event.stopImmediatePropagation(); // 防止其他监听器干扰
            event.preventDefault();

            // 异步跳转避免阻塞(2025性能优化方案)
            requestAnimationFrame(() => {
                window.location.assign(node.href); // 替代直接href赋值
            });
        }
    }, true);

    // 修改window.open函数,添加Alt键检查
    const originalWindowOpen = window.open;
    window.open = function(url, target, features, replace) {
        // 如果Alt键被按下,调用原始window.open函数
        if (altPressed) {
            return originalWindowOpen.call(window, url, target, features, replace);
        }
        // 否则在当前页面打开
        if (url) {
            window.location.assign(url);
        }
        return null;
    };

    // 监控表单并修改target属性
    new MutationObserver(_ => {
        document.querySelectorAll('form').forEach(f => {
            if (!altPressed) { // 只在Alt键未按下时修改
                f.target = '_self';
            }
        });
    }).observe(document, { childList: 1, subtree: 1 });

})();