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.

目前为 2015-10-12 提交的版本。查看 最新版本

  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. // @namespace wOxxOm.scripts
  5. // @author wOxxOm
  6. // @version 1.0.1
  7. // @license MIT License
  8. // @grant GM_openInTab
  9. // @run-at document-start
  10. // @require https://greasyfork.org/scripts/12228/code/setMutationHandler.js
  11. // ==/UserScript==
  12.  
  13. attachHandler([].slice.call(document.getElementsByTagName('a')));
  14.  
  15. setMutationHandler(document, 'a', function(nodes) {
  16. attachHandler(nodes);
  17. return true;
  18. });
  19.  
  20. function attachHandler(nodes) {
  21. nodes.forEach(function(node) {
  22. if (node.target != '_blank') {
  23. node.onclick = clickHandler;
  24. node.addEventListener('click', clickHandler);
  25. }
  26. });
  27. }
  28.  
  29. function clickHandler(e) {
  30. if (e.button > 1)
  31. return;
  32. e.preventDefault();
  33. e.stopPropagation();
  34. e.stopImmediatePropagation();
  35. GM_openInTab(this.href, e.button || e.ctrlKey);
  36. }