Bilibili 禁止新标签页打开链接

让哔哩哔哩所有链接在当前标签页打开

目前為 2025-02-15 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Bilibili 禁止新标签页打开链接
// @name:en      Bilibili No New Tab
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  让哔哩哔哩所有链接在当前标签页打开
// @description:en Force all Bilibili links to open in the current tab instead of a new tab
// @author       ChingyuanCheng
// @license      MIT
// @match        *://www.bilibili.com/*
// @match        *://t.bilibili.com/*
// @match        *://space.bilibili.com/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    /**
     * 移除所有 target="_blank" 以确保链接在当前标签页打开。
     * Remove all target="_blank" to ensure links open in the current tab.
     */
    function modifyLinks() {
        document.querySelectorAll('a[target="_blank"]').forEach(link => {
            link.removeAttribute('target');
            link.rel = "noopener noreferrer"; // 额外的安全性 / Extra security
        });
    }

    // 初始修改 / Initial modification
    modifyLinks();

    // 监听动态加载的内容(适用于 AJAX 加载的页面)
    // Observe dynamically loaded content (for AJAX-loaded pages)
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            if (mutation.type === 'childList') {
                modifyLinks();
            }
        });
    });

    observer.observe(document.body, { childList: true, subtree: true });
})();

QingJ © 2025

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