Last active
August 29, 2015 14:02
-
-
Save mattapayne/b51817ae45e6e5e957c3 to your computer and use it in GitHub Desktop.
Enhance specific links with incoming query string parameters
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
(function() { | |
var domains = ["stackoverflow.com", "google.ca", "abc.org"]; | |
var ready = function(a,b,c){b=document,c='addEventListener';b[c]?b[c]('DOMContentLoaded',a):window.attachEvent('onload',a)} | |
var getQueryString = function() { | |
var qs = document.location.search; | |
var full = ""; | |
var partial = ""; | |
if(qs !== null && qs !== "") { | |
full = qs; | |
partial = qs.substring(1); | |
} | |
return { | |
full: full, | |
partial: partial, | |
hasValue: function() { | |
return this.full !== "" && this.partial !== ""; | |
} | |
}; | |
}; | |
var qs = getQueryString(); | |
var hasDomain = function(href) { | |
if(href !== null && href !== "") { | |
for(var i=0; i<domains.length; i++) { | |
if(href.indexOf(domains[i]) != -1) | |
return true; | |
} | |
} | |
return false; | |
}; | |
var getLinksToProcess = function() { | |
var links = document.body.getElementsByTagName("a"); | |
var process = []; | |
for(var i=0; i<links.length; i++) { | |
if(hasDomain(links[i].href)) { | |
process.push(links[i]); | |
} | |
} | |
return process; | |
}; | |
var updatelinks = function() { | |
var links = getLinksToProcess(); | |
for(var i=0; i<links.length; i++) { | |
if(links[i].href.indexOf("?") != -1) | |
links[i].href = links[i].href + "&" + qs.partial; | |
else | |
links[i].href = links[i].href + qs.full; | |
} | |
}; | |
if(qs.hasValue()) | |
ready(updatelinks); | |
})(); | |
//Minified | |
(function(){var e=["stackoverflow.com","google.com","abc.org"];var t=function(e,t,n){t=document,n="addEventListener";t[n]?t[n]("DOMContentLoaded",e):window.attachEvent("onload",e)};var n=function(){var e=document.location.search;var t="";var n="";if(e!==null&&e!==""){t=e;n=e.substring(1)}return{full:t,partial:n,hasValue:function(){return this.full!==""&&this.partial!==""}}};var r=n();var i=function(t){if(t!==null&&t!==""){for(var n=0;n<e.length;n++){if(t.indexOf(e[n])!=-1)return true}}return false};var s=function(){var e=document.body.getElementsByTagName("a");var t=[];for(var n=0;n<e.length;n++){if(i(e[n].href)){t.push(e[n])}}return t};var o=function(){var e=s();for(var t=0;t<e.length;t++){if(e[t].href.indexOf("?")!=-1)e[t].href=e[t].href+"&"+r.partial;else e[t].href=e[t].href+r.full}};if(r.hasValue())t(o)})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment