Open links in current tab

Open links in current tab regardless of _target or site's preferences. Ctrl-click: background tab, Ctrl-Shift-click: foreground tab, Shift-click: new window, Alt-click: force open in current tab

目前为 2016-09-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Open links in current tab
  3. // @author wOxxOm
  4. // @description Open links in current tab regardless of _target or site's preferences. Ctrl-click: background tab, Ctrl-Shift-click: foreground tab, Shift-click: new window, Alt-click: force open in current tab
  5. // @namespace http://target._blank.is.retarded
  6. // @version 2.0.6
  7. // @include *
  8. // @run-at document-start
  9. // @grant GM_openInTab
  10. // ==/UserScript==
  11.  
  12. var suppressing;
  13. window.addEventListener('mouseup', function(e) {
  14. if (e.button > 1)
  15. return;
  16. var link = e.target.closest('a');
  17. if (!link || (link.getAttribute('href') || '').match(/^(javascript|#|$)/))
  18. return;
  19. var b = e.button, c = e.ctrlKey, a = e.altKey, s = e.shiftKey, m = e.metaKey;
  20. if (link.target != '_blank' && !a && new URL(link.href).origin == location.origin)
  21. return;
  22. if (!b && !c && !s && !m && (link.href.replace(/#.*/, '') != location.href.replace(/#.*/, '') || a))
  23. location.href = link.href;
  24. else if (b == 1 || c && !a && !m)
  25. GM_openInTab(link.href, !s);
  26. else if (window.chrome && !b && s && !c && !a && !m)
  27. link.cloneNode().dispatchEvent(new MouseEvent('click', {shiftKey: true}));
  28. else
  29. return;
  30. suppressing = true;
  31. prevent(e);
  32. }, true);
  33.  
  34. window.addEventListener('click', prevent, true);
  35. window.addEventListener('auxclick', prevent, true);
  36.  
  37. function prevent(e) {
  38. if (!suppressing)
  39. return;
  40. e.preventDefault();
  41. e.stopPropagation();
  42. e.stopImmediatePropagation();
  43. setTimeout(function() {
  44. suppressing = false;
  45. }, 100);
  46. }