Disable target="_blank"

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

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