Greasy Fork

Greasy Fork is available in English.

B站视频跳转

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

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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);