V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
icelei
V2EX  ›  分享创造

懒人福音 省去复制粘贴 B 站链接转换脚本

  •  
  •   icelei · 11 天前 · 1130 次点击

    起因

    在浏览帖子时经常遇到某站的视频 id ,想进一步看是什么视频的时候都要通过 复制→进入视频网站→粘贴并回车 这样一个过程才能完成。作为一个懒人,想直接可以点击访问,于是有了这个脚本。

    主要功能

    将页面中的某站视频 ID ( BV 号或 av 号)转换为可点击链接

    代码如下

    // ==UserScript==
    // @name         B 站链接转换器
    // @namespace    [email protected]
    // @version      0.1
    // @description  将页面中的 B 站视频 ID ( BV 号或 av 号)转换为可点击链接
    // @author       iceLei
    // @match        *://*/*
    // @grant        none
    // ==/UserScript==
    
    (function() {
        'use strict';
    
        function replaceIdsWithLinks() {
            const pattern = /(?:BV[0-9A-Za-z]{10,}|av\d+)/g;
            
            // 递归处理文本节点
            function walk(node) {
                if (node.nodeType === 3) { // 文本节点
                    const text = node.nodeValue;
                    const matches = text.match(pattern);
                    if (matches) {
                        const fragment = document.createDocumentFragment();
                        let lastIndex = 0;
                        
                        text.replace(pattern, (match, index) => {
                            // 添加匹配前的文本
                            fragment.appendChild(document.createTextNode(text.slice(lastIndex, index)));
                            
                            // 创建链接元素
                            const link = document.createElement('a');
                            link.href = `https://www.bilibili.com/video/${match}`;
                            link.textContent = match;
                            link.target = '_blank';
                            link.style.color = '#FB7299'; // B 站粉色
                            link.style.textDecoration = 'underline';
                            
                            fragment.appendChild(link);
                            lastIndex = index + match.length;
                        });
                        
                        // 添加剩余文本
                        fragment.appendChild(document.createTextNode(text.slice(lastIndex)));
                        
                        // 替换原节点
                        node.parentNode.replaceChild(fragment, node);
                    }
                } else if (node.nodeType === 1 && node.tagName !== 'A') { // 元素节点且不是链接
                    // 递归处理子节点
                    Array.from(node.childNodes).forEach(walk);
                }
            }
            
            walk(document.body);
        }
    
        // 执行替换
        replaceIdsWithLinks();
    })();
    
    1 条回复    2025-09-29 15:11:49 +08:00
    chendaye
        1
    chendaye  
       11 天前
    没处理未来元素吗?
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   2855 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 13:59 · PVG 21:59 · LAX 06:59 · JFK 09:59
    ♥ Do have faith in what you're doing.