Open external link in new tab

This script will open any external link in new tab. Support dynamic content

  1. // ==UserScript==
  2. // @name Open external link in new tab
  3. // @version 0.1.3
  4. // @namespace eight04.blogspot.com
  5. // @description This script will open any external link in new tab. Support dynamic content
  6. // @include http*
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. "use strict";
  11.  
  12. function getAnchor(element) {
  13. while (element && element.nodeName != "A") {
  14. element = element.parentNode;
  15. }
  16. return element;
  17. }
  18.  
  19. document.addEventListener("click", function(e){
  20. var anchor = getAnchor(e.target);
  21. if (!anchor || anchor.target || anchor.protocol == "javascript:" || e.isTrusted === false || !anchor.offsetParent || (e.isTrusted == null && !e.detail)) {
  22. return;
  23. }
  24. if (anchor.hostname != location.hostname) {
  25. anchor.target = "_blank";
  26. }
  27. });

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址