B站稍后再看链接替换

将稍后再看视频链接替换为URL Scheme,调用App打开

// ==UserScript==
// @name        B站稍后再看链接替换
// @namespace   https://github.com/QieSen
// @version     0.3
// @description 将稍后再看视频链接替换为URL Scheme,调用App打开
// @author      QieSen
// @match       https://www.bilibili.com/watchlater/*
// @grant       none
// @run-at      document-end
// @license     MIT
// ==/UserScript==

(function() {
    'use strict';

    // 正则表达式匹配BV号
    const bvRegex = /BV[\w\d]{10}/;

    // 替换函数
    function replaceHref(link) {
        const bv = link.href.match(bvRegex)[0];
        link.href = `bilibili://video/${bv}`;
        link.removeAttribute('target'); // 移除target属性,防止新标签页打开 by:daybreak
    }

    // 遍历所有a标签
    function processLinks() {
        document.querySelectorAll('a').forEach(link => {
            if (link.href.match(bvRegex)) {
                replaceHref(link);
            }
        });
    }

    // 初始处理
    processLinks();

    // 监听DOM变化,实时替换新加载的a标签
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            if (mutation.type === 'childList') {
                mutation.addedNodes.forEach(node => {
                    if (node.querySelectorAll) {
                        processLinks(node);
                    }
                });
            }
        });
    });

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

QingJ © 2025

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