Open External Links in New Tab

Opens all external links in a new tab

当前为 2024-01-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Open External Links in New Tab
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Opens all external links in a new tab
  6. // @author YourName
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Get all links in the document
  15. var links = document.getElementsByTagName('a');
  16.  
  17. // Loop through each link
  18. for (var i = 0; i < links.length; i++) {
  19. // Check if the link's hostname is different from the current page's hostname
  20. if (links[i].hostname !== window.location.hostname) {
  21. // Change the target attribute to _blank
  22. links[i].target = '_blank';
  23. }
  24. }
  25. })();