Disable target="_blank"

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

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

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