Open links in new tab

Open links in new tab. Ctrl-click or Middle-click loads it in background, Alt-click opens normally

目前為 2016-09-20 提交的版本,檢視 最新版本

  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, Alt-click opens normally
  4. // @include *
  5. // @namespace wOxxOm.scripts
  6. // @author wOxxOm
  7. // @version 2.0.1
  8. // @license MIT License
  9. // @grant GM_openInTab
  10. // @run-at document-start
  11. // ==/UserScript==
  12.  
  13. var suppressing;
  14. window.addEventListener('mousedown', function(e) {
  15. if (e.button > 1 || e.altKey)
  16. return;
  17. var link = e.target.closest('a');
  18. if (!link ||
  19. !link.href ||
  20. link.href.match(/^javascript|^#/) ||
  21. link.href.replace(/#.*/, '') == location.href.replace(/#.*/, '')
  22. )
  23. return;
  24.  
  25. GM_openInTab(link.href, e.button || e.ctrlKey);
  26. suppressing = true;
  27. prevent(e);
  28. }, true);
  29.  
  30. window.addEventListener('click', prevent, true);
  31. window.addEventListener('mouseup', prevent, true);
  32. window.addEventListener('auxclick', prevent, true);
  33.  
  34. function prevent(e) {
  35. if (!suppressing)
  36. return;
  37. e.preventDefault();
  38. e.stopPropagation();
  39. e.stopImmediatePropagation();
  40. if (e.type == 'mouseup') {
  41. setTimeout(function() {
  42. suppressing = false;
  43. }, 100);
  44. }
  45. }