Changeset 10520
- Timestamp:
- 12/16/2020 07:30:13 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wp-themes.com/public_html/wp-content/mu-plugins/pub/open-links-in-new-window.php
r10510 r10520 3 3 /** 4 4 * Plugin Name: Open external links in new window/tab. 5 * Description: Injects some JS to cause all external links to open in a new tab, to work around X-Frame-Options (and presenting something as WordPress.org that isn't).5 * Description: Injects some JS to WordPress.org links to open in a new tab, and prevent navigation to other hostnames breaking preview iframes. 6 6 */ 7 7 8 8 add_action( 'wp_footer', function() { 9 9 echo '<script> 10 ( function( base) {10 ( function() { 11 11 var links = document.getElementsByTagName( "a" ); 12 12 for ( var i = 0; i < links.length; i++ ) { 13 var href = links[i].href.split( "#" )[0]; 14 if ( href && base !== href.substring( 0, base.length ) ) { 15 links[i].target = "_blank"; 13 var link = links[i], 14 url, 15 hostname; 16 17 try { 18 url = new URL( link.href, document.location.href ); 19 hostname = url.hostname; 20 if ( "mailto:" === url.protocol ) { 21 hostname = "mailto"; // not whitelisted hostname to fall through. 22 } 23 } catch( e ) { 24 // Internet Explorer and invalid links, fall back to regex. 25 if ( hostname = link.href.match( /^\s*(?:(?:https?:)?\/\/)([^/]+)(\/|$)/ ) ) { 26 hostname = hostname[0]; 27 } 16 28 } 29 30 // Self links are allowed. 31 if ( ! hostname || "wp-themes.com" === hostname ) { 32 continue; 33 } 34 35 // Links to WordPress.org should be allowed, but open in a new window. 36 if ( "wordpress.org" === hostname || ".wordpress.org" === hostname.substr(-14) ) { 37 link.target = "_blank"; 38 continue; 39 } 40 41 // The link should not be followed, but the href is kept to allow for a[href^=] based styling. 42 link.addEventListener( "click", function( e ) { 43 e.preventDefault(); 44 } ); 17 45 } 18 } )( ' . wp_json_encode( home_url( '/' ) ) . ')46 } )(); 19 47 </script>'; 20 } );48 }, 9999 );
Note: See TracChangeset
for help on using the changeset viewer.