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-21 提交的版本,查看 最新版本

  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.9
  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 (!a && link.target != '_blank') {
  21. var _open = unsafeWindow.open;
  22. var timeout = setTimeout(function() {
  23. unsafeWindow.open = _open;
  24. }, 50);
  25. unsafeWindow.open = exportFunction(function(url) {
  26. console.log('[Open links in current tab] prevented window.open for', url);
  27. location.href = link.href;
  28. unsafeWindow.open = _open;
  29. clearTimeout(timeout);
  30. }, unsafeWindow);
  31. return;
  32. }
  33. if (!b && !c && !s && !m && (link.href.replace(/#.*/, '') != location.href.replace(/#.*/, '') || a))
  34. location.href = link.href;
  35. else if (b == 1 || c && !a && !m)
  36. GM_openInTab(link.href, !s);
  37. else if (window.chrome && !b && s && !c && !a && !m)
  38. link.cloneNode().dispatchEvent(new MouseEvent('click', {shiftKey: true}));
  39. else
  40. return;
  41. suppressing = true;
  42. prevent(e);
  43. }, true);
  44.  
  45. window.addEventListener('click', prevent, true);
  46. window.addEventListener('auxclick', prevent, true);
  47.  
  48. function prevent(e) {
  49. if (!suppressing)
  50. return;
  51. e.preventDefault();
  52. e.stopPropagation();
  53. e.stopImmediatePropagation();
  54. setTimeout(function() {
  55. suppressing = false;
  56. }, 50);
  57. }