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: don't alter the link

当前为 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: don't alter the link
  5. // @namespace http://target._blank.is.retarded
  6. // @version 2.0
  7. // @include *
  8. // @run-at document-start
  9. // @grant GM_openInTab
  10. // ==/UserScript==
  11.  
  12. window.addEventListener('mousedown', function(e) {
  13. if (e.button > 1)
  14. return;
  15. var link = e.target.closest('a');
  16. if (!link)
  17. return;
  18.  
  19. var b = e.button, c = e.ctrlKey, a = e.altKey, s = e.shiftKey, m = e.metaKey;
  20. if (!b && !c && !a && !s && !m)
  21. location.href = link.href;
  22. else if (b == 1 || c && !a && !m)
  23. GM_openInTab(link.href, !s);
  24. else if (window.chrome && !b && s && !c && !a && !m)
  25. link.cloneNode().dispatchEvent(new MouseEvent('click', {shiftKey: true}));
  26. else
  27. return;
  28.  
  29. prevent(e);
  30. window.addEventListener('click', prevent, true);
  31. window.addEventListener('mouseup', prevent, true);
  32. window.addEventListener('auxclick', prevent, true);
  33.  
  34. function prevent(e) {
  35. e.preventDefault();
  36. e.stopPropagation();
  37. e.stopImmediatePropagation();
  38. if (e.type != 'mousedown')
  39. window.removeEventListener(e.type, prevent);
  40. }
  41. }, true);