B站推荐视频新标签页打开(修复原页面跳转问题)

点击B站右侧推荐视频时,强制在新标签页中打开,并阻止原页面跳转

  1. // ==UserScript==
  2. // @name B站推荐视频新标签页打开(修复原页面跳转问题)
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.4
  5. // @description 点击B站右侧推荐视频时,强制在新标签页中打开,并阻止原页面跳转
  6. // @author Your Name
  7. // @match https://www.bilibili.com/video/*
  8. // @license MIT
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. // 监听点击事件,处理推荐视频链接
  16. function handleRecommendClicks(event) {
  17. // 获取点击的目标元素
  18. const target = event.target;
  19.  
  20. // 检查点击的是否是推荐视频的链接
  21. const videoLink = target.closest('a[href^="/video/"]');
  22. if (videoLink) {
  23. // 阻止默认行为(在当前页面打开)
  24. event.preventDefault();
  25. event.stopPropagation(); // 阻止事件冒泡
  26.  
  27. // 获取视频的完整链接
  28. const videoUrl = new URL(videoLink.href, window.location.origin).href;
  29.  
  30. // 在新标签页中打开视频
  31. window.open(videoUrl, '_blank');
  32. }
  33. }
  34.  
  35. // 监听整个文档的点击事件
  36. document.addEventListener('click', handleRecommendClicks, true); // 使用捕获阶段
  37.  
  38. // 监听动态加载的内容
  39. const observer = new MutationObserver(function (mutations) {
  40. mutations.forEach(function (mutation) {
  41. if (mutation.addedNodes.length) {
  42. console.log('检测到新内容加载,重新绑定事件。');
  43. }
  44. });
  45. });
  46.  
  47. // 开始观察文档的变化
  48. observer.observe(document.body, {
  49. childList: true,
  50. subtree: true,
  51. });
  52.  
  53. console.log('B站推荐视频新标签页打开脚本已启用!');
  54. })();

QingJ © 2025

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