Last active
January 8, 2018 03:31
-
-
Save fent/5c4bd5c51f7874e1a078 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name offsite links new tab | |
// @namespace fent | |
// @version 0.1.1 | |
// @description Opens offsite links in a new tab. | |
// @include /https?:\/\// | |
// ==/UserScript== | |
var originalHost = window.location.host; | |
document.body.addEventListener('click', function(e) { | |
var target = e.target; | |
if (target.tagName === 'A' && target.target !== '_blank' && | |
(target.protocol === 'https:' || target.protocol === 'http:') && | |
target.host !== originalHost) { | |
// Setting this property instead of using `GM_openInTab()` works | |
// better for links that are handled via Javascript. | |
// If `e.preventDefault()` is called on this event, a tab won't | |
// be opened incorrectly. | |
var oldTarget = target.target; | |
target.setAttribute('target', '_blank'); | |
setTimeout(function() { | |
target.setAttribute('target', oldTarget); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment