Bilibili Disable target="_blank"

禁止B站包含target="_blank"的链接在新标签页中打开

当前为 2024-12-24 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Bilibili Disable target="_blank"
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description 禁止B站包含target="_blank"的链接在新标签页中打开
  6. // @author malagebidi
  7. // @match *://*.bilibili.com/*
  8. // @license MIT
  9. // @grant none
  10. // @icon https://www.bilibili.com/favicon.ico
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // 移除所有链接的 target="_blank"
  17. document.addEventListener('click', function(e) {
  18. if (e.target.tagName === 'A') {
  19. e.target.target = '_self';
  20. }
  21. }, true);
  22.  
  23. // 或者使用 MutationObserver 动态处理
  24. const observer = new MutationObserver((mutations) => {
  25. mutations.forEach((mutation) => {
  26. if (mutation.type === 'childList') {
  27. document.querySelectorAll('a[target="_blank"]').forEach(link => {
  28. link.target = '_self';
  29. });
  30. }
  31. });
  32. });
  33.  
  34. observer.observe(document.body, {
  35. childList: true,
  36. subtree: true
  37. });
  38. })();