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

  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. // @namespace 3xploiton3.scripts
  5. // @author 3xploiton3
  6. // @version 1.1
  7. // @license MIT License
  8. // @grant GM_openInTab
  9. // @run-at document-start
  10. // @match *://www.tokopedia.com/*
  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. }