Created
July 17, 2009 08:25
-
-
Save bellbind/148944 to your computer and use it in GitHub Desktop.
[greasemonkey] Add target of links which have different domains from the page
This file contains 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 Add target of links which have different domains from the page | |
// @namespace add_link_target | |
// @description Add target of links which have different domains from the page | |
// @include https://* | |
// @include http://* | |
// ==/UserScript== | |
(function () { | |
var pattern = /^(?:https?|ftp):\/\/(.*?)\//; | |
var domainDoc = document.URL.match(pattern); | |
if (!domainDoc) return; | |
var addTarget = function (doc) { | |
var links = doc.getElementsByTagName("a"); | |
var domain = domainDoc[1]; | |
for (var i = 0; i < links.length; i += 1) { | |
var link = links[i]; | |
var domainUrl = link.href.match(pattern); | |
if (domainUrl && domainUrl[1] !== domain) { | |
link.target = "_blank"; | |
} | |
} | |
}; | |
addTarget(document); | |
if (window.AutoPagerize) { | |
window.AutoPagerize.addDocumentFilter(addTarget); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment