B站视频跳转

将B站站外出现的av号/bv号转换为链接方便点击跳转

当前为 2020-04-14 提交的版本,查看 最新版本

// ==UserScript==
// @name         B站视频跳转
// @namespace    http://tampermonkey.net/
// @version      1.0.0
// @description  将B站站外出现的av号/bv号转换为链接方便点击跳转
// @author       QingMu_
// @match        *://*.baidu.com/*
// @match        *://*.zhihu.com/*
// @match        *://*.saraba1st.com/*
// @match        *://*.ngabbs.com/*
// @match        *://*.nga.cn/*
// @match        *://nga.178.com/*
// @grant        none
// @license      MIT License
// ==/UserScript==
//如需添加其他站点,按上面@match的格式自行添加即可(每行一个)
const matchRules = /(?:av[0-9]+)|(?:bv[A-z0-9]{10,})/gi;
const config = {childList: true, subtree: true ,characterData:true ,attributes:true};
const DFS = {
    nodes: [],
    do (root) {
        for (let i = 0;i < root.childNodes.length;i++) {
            var node = root.childNodes[i];
            if ((node.nodeType != 8) && (node.nodeType != 3) && (node.nodeName !== 'STYLE') && (node.nodeName != 'SCRIPT') && (node.nodeName != 'A')) {
                //console.log(i,node)
                try{
                    if(node.innerText.match(matchRules) != null){
                        this.nodes.push(node);
                    }
                }catch(e){};
                this.do(node);
            }
        }
        return this.nodes;
    }
}
var observer = {
    observe(){},
    disconnect(){}
}
const update = (mutationsList)=>{
    DFS.nodes.length=0;
    mutationsList.forEach((item,index)=>{
        if(item.addedNodes.length>0){
            item.addedNodes.forEach((node,i)=>{
                try{
                    if((node.nodeType != 3) && (node.nodeName != 'A') && (node.innerText.match(matchRules) !== null)){
                        startTransaction(node)
                    }
                }catch(e){};
            });
        }
    });
}
try{
    observer = new MutationObserver(update);
}catch(e){
    console.log("[B站视频跳转]:创建MutationObserver失败,无法监听DOM节点改变,后续更新的内容将不会对av/bv号进行链接转换!")
}
const startTransaction = (checkNodes)=>{
    observer.disconnect();
    DFS.do(checkNodes);
    for(let x in DFS.nodes){
        let gc = DFS.nodes[x].children;
        if(gc.length==0){
            DFS.nodes[x].innerHTML = DFS.nodes[x].innerHTML.replace(matchRules,(videoId)=>{
                return `<a target="_blank" style="text-decoration:underline;" href="https://www.bilibili.com/video/${videoId}">${videoId}</a>`;
            });
        }else{
            let originLength = gc.length;
            for(let i=0;i<originLength;i++){
                if(gc[i].innerText.match(matchRules) != null){
                    break;
                }else if(i == gc.length-1){
                    DFS.nodes[x].innerHTML = DFS.nodes[x].innerHTML.replace(matchRules,(videoId)=>{
                        return `<a target="_blank" style="text-decoration:underline;" href="https://www.bilibili.com/video/${videoId}">${videoId}</a>`;
                    });
                }
            }
        }
    }
    observer.observe(document.body, config);
}
startTransaction(document.body);

QingJ © 2025

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