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.2
  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.getAttribute('href') || '').match(/^(javascript|#|$)/) ||
  20. link.href.replace(/#.*/, '') == location.href.replace(/#.*/, '')
  21. )
  22. return;
  23.  
  24. GM_openInTab(link.href, e.button || e.ctrlKey);
  25. suppressing = true;
  26. prevent(e);
  27. }, true);
  28.  
  29. window.addEventListener('click', prevent, true);
  30. window.addEventListener('mouseup', prevent, true);
  31. window.addEventListener('auxclick', prevent, true);
  32.  
  33. function prevent(e) {
  34. if (!suppressing)
  35. return;
  36. e.preventDefault();
  37. e.stopPropagation();
  38. e.stopImmediatePropagation();
  39. if (e.type == 'mouseup') {
  40. setTimeout(function() {
  41. suppressing = false;
  42. }, 100);
  43. }
  44. }