Open links in new tab

Open links in new tab. Ctrl-click or Middle-click loads it in background

  1. // ==UserScript==
  2. // @name Open links in new tab
  3. // @description Open links in new tab. Ctrl-click or Middle-click loads it in background
  4. // @include *
  5. // @namespace wOxxOm.scripts
  6. // @author wOxxOm
  7. // @version 2.0.5
  8. // @license MIT License
  9. // @grant GM_openInTab
  10. // @run-at document-start
  11. // ==/UserScript==
  12.  
  13. var suppressing, clickedElement;
  14.  
  15. window.addEventListener('mousedown', function (e) {
  16. clickedElement = e.target;
  17. }, true);
  18.  
  19. window.addEventListener('mouseup', function (e) {
  20. if (e.button > 1 || e.altKey || e.target != clickedElement)
  21. return;
  22. var 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. GM_openInTab(link.href, {
  30. active: !e.button && !e.ctrlKey,
  31. setParent: true,
  32. insert: true,
  33. });
  34. suppressing = true;
  35. setTimeout(function () {
  36. window.dispatchEvent(new MouseEvent('mouseup', {bubbles: true}));
  37. });
  38. prevent(e);
  39. }, true);
  40.  
  41. window.addEventListener('click', prevent, true);
  42. window.addEventListener('auxclick', prevent, true);
  43.  
  44. function prevent(e) {
  45. if (!suppressing)
  46. return;
  47. e.preventDefault();
  48. e.stopPropagation();
  49. e.stopImmediatePropagation();
  50. setTimeout(function () {
  51. suppressing = false;
  52. }, 100);
  53. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址