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