B站稍后再看链接替换

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

  1. // ==UserScript==
  2. // @name B站稍后再看链接替换
  3. // @namespace https://github.com/QieSen
  4. // @version 0.3
  5. // @description 将稍后再看视频链接替换为URL Scheme,调用App打开
  6. // @author QieSen
  7. // @match https://www.bilibili.com/watchlater/*
  8. // @grant none
  9. // @run-at document-end
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // 正则表达式匹配BV号
  17. const bvRegex = /BV[\w\d]{10}/;
  18.  
  19. // 替换函数
  20. function replaceHref(link) {
  21. const bv = link.href.match(bvRegex)[0];
  22. link.href = `bilibili://video/${bv}`;
  23. link.removeAttribute('target'); // 移除target属性,防止新标签页打开 by:daybreak
  24. }
  25.  
  26. // 遍历所有a标签
  27. function processLinks() {
  28. document.querySelectorAll('a').forEach(link => {
  29. if (link.href.match(bvRegex)) {
  30. replaceHref(link);
  31. }
  32. });
  33. }
  34.  
  35. // 初始处理
  36. processLinks();
  37.  
  38. // 监听DOM变化,实时替换新加载的a标签
  39. const observer = new MutationObserver(mutations => {
  40. mutations.forEach(mutation => {
  41. if (mutation.type === 'childList') {
  42. mutation.addedNodes.forEach(node => {
  43. if (node.querySelectorAll) {
  44. processLinks(node);
  45. }
  46. });
  47. }
  48. });
  49. });
  50.  
  51. observer.observe(document.body, { childList: true, subtree: true });
  52. })();

QingJ © 2025

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