当页开链

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

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

// ==UserScript==
// @name         当页开链
// @version      4.0
// @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
// @namespace
// @namespace
// @namespace
// @namespace https://gf.qytechs.cn/users/982160
// ==/UserScript==

    (function() {
        'use strict';

    const shouldExcludeElement = (target) => {
        const EXCLUDE_SELECTORS = [
            '.nav-content',
            '.views',
            '.presentation',
            '.pay-box',      // 支付相关容器
            '[target="_self"]',
            '[role="group"]',
            '#ks',
            '.bpx-player-ending-related-item-cover',
            '#qs_searchBox',
            '.actions'
        ];
        return EXCLUDE_SELECTORS.some(selector => target.closest(selector));
    };

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

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

            if (shouldExcludeElement(target) || target.closest('form')) {
                return true;
            }

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

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

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

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

        // 协议安全名单


        // 动态白名单(2025-03-01更新)


        // 智能链接处理


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

            if (shouldExcludeElement(target) || target.closest('form')) {
                return true;
            }

            let link = target.closest('a');
            if (!link) {
                link = target;
                while (link && link.tagName !== 'A') {
                    link = link.parentElement;
                }
            }

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

            // 兼容Bing的AJAX搜索(2025-03-01新增)

        };

        // 核心初始化
        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);
    })();

QingJ © 2025

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