
在浏览帖子时经常遇到某站的视频 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(); })();