B站推荐视频新标签页打开(修复原页面跳转问题)

点击B站右侧推荐视频时,强制在新标签页中打开,并阻止原页面跳转

// ==UserScript==
// @name         B站推荐视频新标签页打开(修复原页面跳转问题)
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  点击B站右侧推荐视频时,强制在新标签页中打开,并阻止原页面跳转
// @author       Your Name
// @match        https://www.bilibili.com/video/*
// @license      MIT
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // 监听点击事件,处理推荐视频链接
    function handleRecommendClicks(event) {
        // 获取点击的目标元素
        const target = event.target;

        // 检查点击的是否是推荐视频的链接
        const videoLink = target.closest('a[href^="/video/"]');
        if (videoLink) {
            // 阻止默认行为(在当前页面打开)
            event.preventDefault();
            event.stopPropagation(); // 阻止事件冒泡

            // 获取视频的完整链接
            const videoUrl = new URL(videoLink.href, window.location.origin).href;

            // 在新标签页中打开视频
            window.open(videoUrl, '_blank');
        }
    }

    // 监听整个文档的点击事件
    document.addEventListener('click', handleRecommendClicks, true); // 使用捕获阶段

    // 监听动态加载的内容
    const observer = new MutationObserver(function (mutations) {
        mutations.forEach(function (mutation) {
            if (mutation.addedNodes.length) {
                console.log('检测到新内容加载,重新绑定事件。');
            }
        });
    });

    // 开始观察文档的变化
    observer.observe(document.body, {
        childList: true,
        subtree: true,
    });

    console.log('B站推荐视频新标签页打开脚本已启用!');
})();

QingJ © 2025

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