Open links in new tab

Open links in new tab (Ctrl-click or Middle-click loads it in background), works on dynamically added content too.

当前为 2016-03-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Open links in new tab
  3. // @description Open links in new tab (Ctrl-click or Middle-click loads it in background), works on dynamically added content too.
  4. // @include *
  5. // @namespace wOxxOm.scripts
  6. // @author wOxxOm
  7. // @version 1.0.2
  8. // @license MIT License
  9. // @grant GM_openInTab
  10. // @run-at document-start
  11. // @require https://greasyfork.org/scripts/12228/code/setMutationHandler.js
  12. // ==/UserScript==
  13.  
  14. attachHandler([].slice.call(document.getElementsByTagName('a')));
  15.  
  16. setMutationHandler(document, 'a', function(nodes) {
  17. attachHandler(nodes);
  18. return true;
  19. });
  20.  
  21. function attachHandler(nodes) {
  22. nodes.forEach(function(node) {
  23. if (node.target != '_blank') {
  24. node.onclick = clickHandler;
  25. node.addEventListener('click', clickHandler);
  26. }
  27. });
  28. }
  29.  
  30. function clickHandler(e) {
  31. if (e.button > 1)
  32. return;
  33. e.preventDefault();
  34. e.stopPropagation();
  35. e.stopImmediatePropagation();
  36. GM_openInTab(this.href, e.button || e.ctrlKey);
  37. }