Greasy Fork 还支持 简体中文。

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-22 提交的版本,檢視 最新版本

  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.2
  7. // @include *
  8. // @run-at document-start
  9. // @grant GM_openInTab
  10. // ==/UserScript==
  11.  
  12. var suppressing, clickedElement;
  13. window.addEventListener('mousedown', function(e) {
  14. clickedElement = e.target;
  15. }, true);
  16.  
  17. window.addEventListener('mouseup', function(e) {
  18. if (e.button > 1 || e.target != clickedElement)
  19. return;
  20. var link = e.target.closest('a');
  21. if (!link || (link.getAttribute('href') || '').match(/^(javascript|#|$)/))
  22. return;
  23. var b = e.button, c = e.ctrlKey, a = e.altKey, s = e.shiftKey, m = e.metaKey;
  24. if (!a && link.target != '_blank') {
  25. var observer = new MutationObserver(function(mutations) {
  26. if (link.target == '_blank') {
  27. link.removeAttribute('target');
  28. console.log('[Open links in current tab] prevented dynamic target=_blank for', link.href);
  29. }
  30. });
  31. observer.observe(link, {attributes:true, attributeFilter:['target'], characterData:true});
  32.  
  33. var _open = unsafeWindow.open;
  34. var timeout = setTimeout(function() {
  35. unsafeWindow.open = _open;
  36. observer.disconnect();
  37. }, 50);
  38. unsafeWindow.open = exportFunction(function(url, name, features) {
  39. if (!features) {
  40. console.log('[Open links in current tab] prevented window.open for', url);
  41. location.href = link.href;
  42. } else
  43. _open(url, name, features);
  44. unsafeWindow.open = _open;
  45. clearTimeout(timeout);
  46. }, unsafeWindow);
  47. return;
  48. }
  49. if (!b && !c && !s && !m && (link.href.replace(/#.*/, '') != location.href.replace(/#.*/, '') || a))
  50. location.href = link.href;
  51. else if (b == 1 || c && !a && !m)
  52. GM_openInTab(link.href, !s);
  53. else if (window.chrome && !b && s && !c && !a && !m)
  54. link.cloneNode().dispatchEvent(new MouseEvent('click', {shiftKey: true}));
  55. else
  56. return;
  57. suppressing = true;
  58. prevent(e);
  59. }, true);
  60.  
  61. window.addEventListener('click', prevent, true);
  62. window.addEventListener('auxclick', prevent, true);
  63.  
  64. function prevent(e) {
  65. if (!suppressing)
  66. return;
  67. e.preventDefault();
  68. e.stopPropagation();
  69. e.stopImmediatePropagation();
  70. setTimeout(function() {
  71. suppressing = false;
  72. }, 50);
  73. }