当页开链

全站通用型页内增强脚本,Bing兼容,bili兼容.添加kook按钮排除

目前為 2025-03-26 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         当页开链
// @version      3.7.8
// @description  全站通用型页内增强脚本,Bing兼容,bili兼容.添加kook按钮排除
// @author       none
// @match        *://*/*
// @grant        unsafeWindow
// @run-at       document-body
// @namespace
// @exclude-match    *://www.gamer520.com/*
// @namespace
// @namespace
// @namespace
// @namespace
// @namespace
// @namespace 
// ==/UserScript==

(function() {
    'use strict';

    // 函数用于在当前标签页中打开链接
    function openLinkInCurrentTab(url) {
        window.location.href = url;
    }

    // 拦截所有点击事件
    document.addEventListener('click', function(event) {
        var target = event.target;

    if (target.closest('[class="views"]')) {
        return true; // 保留播放器原生交互
    }

          // 新增排除播放器容器逻辑(2025-03-02更新)
    if (target.closest('.presentation')) {
        return true; // 保留播放器原生交互
    }

          // 新增排除播放器容器逻辑(2025-03-02更新)
    if (target.closest('[class="pay-box"]')) {
        return true; // 保留播放器原生交互
    }

          // 新增排除播放器容器逻辑(2025-03-02更新)
    if (target.closest('[target="_self"]')) {
        return true; // 保留播放器原生交互
    }

          // 新增排除播放器容器逻辑(2025-03-02更新)
    if (target.closest('[role="group"]')) {
        return true; // 保留播放器原生交互
    }

    // 新增排除播放器容器逻辑(2025-03-02更新)
    if (target.closest('#ks')) {
        return true; // 保留播放器原生交互
    }

    // 新增排除播放器容器逻辑(2025-03-02更新)
    if (target.closest('.bpx-player-ending-related-item-cover')) {
        return true; // 保留播放器原生交互
    }
    // 新增排除bing选择弹出
    if (target.closest('#qs_searchBox')) {
        return true; // 保留播放器原生交互
    }

        // 检查点击的元素以及其父元素是否是链接
        while (target && target.tagName !== 'A') {
            target = target.parentElement;
        }

        if (target && target.tagName === 'A') {
            // 阻止默认行为,即在新标签页中打开链接
            event.preventDefault();

            // 获取链接的目标 URL
            var url = target.href;

            // 在当前标签页中打开链接
            openLinkInCurrentTab(url);
        }
    });

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

    // 动态白名单(2025-03-01更新)
    const SEARCH_ENGINE_HOSTNAMES = new Set([
        'www.bing.com', 'cn.bing.com',
        'www.google.com', 'www.baidu.com'
    ]);

    // 智能链接处理
    const processElement = (element) => {
        // 排除Bing搜索按钮
        if (element.closest('form[action="/search"]')) return;
        if (element.id === 'sb_form_go' || element.name === 'search') return;

        // 协议过滤
        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);
        });
    };

    // 增强事件监听(修复Bing搜索提交问题)
    const initEventHandlers = () => {
        // 拦截点击事件时排除表单元素
        document.addEventListener('click', function(event) {
            const target = event.composedPath()[0];
            const isSearchEngine = SEARCH_ENGINE_HOSTNAMES.has(location.hostname);

    if (target.closest('[class="views"]')) {
        return true; // 保留播放器原生交互
    }
          // 新增排除播放器容器逻辑(2025-03-02更新)
    if (target.closest('.presentation')) {
        return true; // 保留播放器原生交互
    }

          // 新增排除播放器容器逻辑(2025-03-02更新)
    if (target.closest('[class="pay-box"]')) {
        return true; // 保留播放器原生交互
    }

          // 新增排除播放器容器逻辑(2025-03-02更新)
    if (target.closest('[target="_self"]')) {
        return true; // 保留播放器原生交互
    }

          // 新增排除播放器容器逻辑(2025-03-02更新)
    if (target.closest('[role="group"]')) {
        return true; // 保留播放器原生交互
    }

    // 新增排除播放器容器逻辑(2025-03-02更新)
    if (target.closest('#ks')) {
        return true; // 保留播放器原生交互
    }

    // 新增排除播放器容器逻辑(2025-03-02更新)
    if (target.closest('.bpx-player-ending-related-item-cover')) {
        return true; // 保留播放器原生交互
    }
    // 新增排除播放器容器逻辑(2025-03-02更新)
    if (target.closest('#qs_searchBox')) {
        return true; // 保留播放器原生交互
    }

            // 白名单处理逻辑
            if (isSearchEngine) {
                // 允许Bing搜索按钮正常工作
                if (target.closest('form#sb_form') || target.id === 'sb_form_go') {
                    return true;
                }
            }

            // 常规链接处理
            let node = target;
            while (node && node.tagName !== 'A') {
                node = node.parentElement;
            }

            if (node && node.tagName === 'A') {
                event.preventDefault();
                window.location.href = node.href;
            }
        }, true);  // 使用捕获阶段

        // 兼容Bing的AJAX搜索(2025-03-01新增)
        if (SEARCH_ENGINE_HOSTNAMES.has(location.hostname)) {
            document.addEventListener('submit', function(e) {
                const form = e.target;
                if (form.action.includes('/search')) {
                    form.setAttribute('target', '_self');
                }
            });
        }
    };

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

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

        // 初始化事件处理器
        initEventHandlers();

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

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