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

  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.2.6
  7. // @include *
  8. // @run-at document-start
  9. // @grant GM_openInTab
  10. // ==/UserScript==
  11.  
  12. if (window == top) {
  13. window.addEventListener('message', function(e) {
  14. // some stupid sites choke on object data
  15. if (!/^\{/.test(e.data))
  16. return;
  17. var data = tryParse(e.data);
  18. if (data.name == GM_info.script.name)
  19. navigate(data.url);
  20. });
  21. }
  22.  
  23. var suppressing, clickedElement;
  24. window.addEventListener('mousedown', function(e) {
  25. clickedElement = e.target;
  26. }, true);
  27.  
  28. window.addEventListener('mouseup', function(e) {
  29. if (e.button > 1 || e.target != clickedElement)
  30. return;
  31. var link = pierceShadow(e);
  32. if (!link || (link.getAttribute('href') || '').match(/^(javascript|#|$)/))
  33. return;
  34. var b = e.button, c = e.ctrlKey, a = e.altKey, s = e.shiftKey, m = e.metaKey;
  35. if (b == 1 || c && !a && !m)
  36. GM_openInTab(link.href, !s || b == 1);
  37. else if (window.chrome && b === 0 && s && !c && !a && !m)
  38. link.cloneNode().dispatchEvent(new MouseEvent('click', {shiftKey: true}));
  39. else if (!c && !s && !m && !a) {
  40. if (link.target == '_blank')
  41. link.target = '';
  42. blockWindowOpenAndMutations(link);
  43. return;
  44. }
  45. else
  46. return;
  47. suppressing = true;
  48. prevent(e);
  49. }, true);
  50.  
  51. window.addEventListener('click', prevent, true);
  52. window.addEventListener('auxclick', prevent, true);
  53.  
  54. function prevent(e) {
  55. if (!suppressing)
  56. return;
  57. e.preventDefault();
  58. e.stopPropagation();
  59. e.stopImmediatePropagation();
  60. setTimeout(function() {
  61. suppressing = false;
  62. }, 50);
  63. }
  64.  
  65. function blockWindowOpenAndMutations(link) {
  66. var observer = new MutationObserver(function() {
  67. if (link.target == '_blank') {
  68. link.removeAttribute('target');
  69. console.log('[Open links in current tab] prevented dynamic target=_blank for', link.href);
  70. navigate(link.href);
  71. }
  72. });
  73. observer.observe(link, {attributes:true, attributeFilter:['target'], characterData:true});
  74.  
  75. var _open = unsafeWindow.open;
  76. var timeout = setTimeout(function() {
  77. unsafeWindow.open = _open;
  78. observer.disconnect();
  79. }, 50);
  80. unsafeWindow.open = exportFunction(function(url, name, features) {
  81. if (!features) {
  82. console.log('[Open links in current tab] prevented window.open for', url);
  83. navigate(link.href);
  84. } else
  85. _open(url, name, features);
  86. unsafeWindow.open = _open;
  87. clearTimeout(timeout);
  88. }, unsafeWindow);
  89. }
  90.  
  91. function pierceShadow(e) {
  92. var el = e.target;
  93. while (el.shadowRoot)
  94. el = el.shadowRoot.elementFromPoint(e.clientX, e.clientY);
  95. return el.closest('a');
  96. }
  97.  
  98. function navigate(url) {
  99. if (window == top) {
  100. var link = document.createElement('a');
  101. link.href = url;
  102. link.dispatchEvent(new MouseEvent('click'));
  103. } else
  104. top.postMessage(JSON.stringify({name: GM_info.script.name, url: url}), '*');
  105. }
  106.  
  107. function tryParse(str) {
  108. try { return JSON.parse(str); }
  109. catch(e) {}
  110. }