Force Ctrl-click or Middle-click into a new tab

Force Ctrl-click or Middle-click into a new tab to load it in the background

当前为 2025-05-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Force Ctrl-click or Middle-click into a new tab
  3. // @description Force Ctrl-click or Middle-click into a new tab to load it in the background
  4. // @include *
  5. // @namespace 3xploiton3.scripts
  6. // @author 3xploiton3
  7. // @version 1.0.1
  8. // @license MIT License
  9. // @grant GM_openInTab
  10. // @run-at document-start
  11. // ==/UserScript==
  12.  
  13. var suppressing = false, clickedElement;
  14.  
  15. window.addEventListener('mousedown', function (e) {
  16. clickedElement = e.target;
  17. }, true);
  18.  
  19. window.addEventListener('mouseup', function (e) {
  20. if (e.target !== clickedElement) return;
  21.  
  22. const link = e.target.closest('a');
  23. if (!link ||
  24. (link.getAttribute('href') || '').match(/^(javascript:|#|$)/) ||
  25. link.href.replace(/#.*/, '') === location.href.replace(/#.*/, '')
  26. ) {
  27. return;
  28. }
  29.  
  30. // Middle click (button === 1) OR Ctrl + Left click
  31. if (e.button === 1 || (e.button === 0 && e.ctrlKey)) {
  32. GM_openInTab(link.href, {
  33. active: false,
  34. setParent: true,
  35. insert: true,
  36. });
  37. suppressing = true;
  38. setTimeout(() => {
  39. window.dispatchEvent(new MouseEvent('mouseup', { bubbles: true }));
  40. });
  41. prevent(e);
  42. }
  43. // Left click without Ctrl – do nothing, allow default behavior (normal navigation)
  44. }, true);
  45.  
  46. window.addEventListener('click', prevent, true);
  47. window.addEventListener('auxclick', prevent, true);
  48.  
  49. function prevent(e) {
  50. if (!suppressing) return;
  51. e.preventDefault();
  52. e.stopPropagation();
  53. e.stopImmediatePropagation();
  54. setTimeout(() => {
  55. suppressing = false;
  56. }, 100);
  57. }