Last active
December 30, 2018 14:25
-
-
Save robertuniqid/3b2a8c3069ace58deff0de03b66ab12b to your computer and use it in GitHub Desktop.
Automatically Add $_GET Url Params from current request to all same-domain links on the page, that are part of a whitelist. HTML & JS code is exactly the same, simply copy-paste for non-teechies.
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
<script type="text/javascript"> | |
// The URL Params you want to whitelist, it will apply them on all links on the same domain. | |
function rl_whitelisted_url_params() { | |
return [ 'example-url-param', 'example-url-param-2', 'example-url-param-3' ]; | |
} | |
function rl_get_url_param(parameterName) { | |
var result = null, | |
tmp = [], | |
items = location.search.substr(1).split("&"); | |
for (var index = 0; index < items.length; index++) { | |
tmp = items[index].split("="); | |
if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]); | |
} | |
return result; | |
} | |
function rl_set_url_param(url, param, value) { | |
param = encodeURIComponent(param); | |
var r = "([&?]|&)" + param + "\\b(?:=(?:[^&#]*))*"; | |
var a = document.createElement('a'); | |
var regex = new RegExp(r); | |
var str = param + (value ? "=" + encodeURIComponent(value) : ""); | |
a.href = url; | |
var q = a.search.replace(regex, "$1"+str); | |
if (q === a.search) { | |
a.search += (a.search ? "&" : "") + str; | |
} else { | |
a.search = q; | |
} | |
return a.href; | |
} | |
document.addEventListener('DOMContentLoaded', function () { | |
var rl_url_params_full = rl_whitelisted_url_params(), | |
rl_url_params = {}, | |
rl_links = document.getElementsByTagName('a'), | |
rl_i = 0, | |
rl_domain = window.location.protocol + '//' + window.location.host; | |
for( rl_i = 0; rl_i < rl_url_params_full.length; rl_i++ ) { | |
var param = rl_get_url_param( rl_url_params_full[rl_i] ); | |
if( param === null ) | |
continue; | |
rl_url_params[ rl_url_params_full[rl_i] ] = param; | |
} | |
// No URL Params are actually provided | |
if( Object.keys( rl_url_params ).length === 0 ) | |
return; | |
for( rl_i = 0; rl_i < rl_links.length; rl_i++) { | |
if( rl_links[rl_i].href.indexOf( rl_domain ) !== 0 ) | |
continue; | |
var href = rl_links[rl_i].href; | |
Object.keys( rl_url_params ).map(function(objectKey, index) { | |
href = rl_set_url_param( href, objectKey, rl_url_params[objectKey] ); | |
}); | |
rl_links[rl_i].href = href; | |
} | |
}); | |
</script> |
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
// The URL Params you want to whitelist, it will apply them on all links on the same domain. | |
function rl_whitelisted_url_params() { | |
return [ 'example-url-param', 'example-url-param-2', 'example-url-param-3' ]; | |
} | |
function rl_get_url_param(parameterName) { | |
var result = null, | |
tmp = [], | |
items = location.search.substr(1).split("&"); | |
for (var index = 0; index < items.length; index++) { | |
tmp = items[index].split("="); | |
if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]); | |
} | |
return result; | |
} | |
function rl_set_url_param(url, param, value) { | |
param = encodeURIComponent(param); | |
var r = "([&?]|&)" + param + "\\b(?:=(?:[^&#]*))*"; | |
var a = document.createElement('a'); | |
var regex = new RegExp(r); | |
var str = param + (value ? "=" + encodeURIComponent(value) : ""); | |
a.href = url; | |
var q = a.search.replace(regex, "$1"+str); | |
if (q === a.search) { | |
a.search += (a.search ? "&" : "") + str; | |
} else { | |
a.search = q; | |
} | |
return a.href; | |
} | |
document.addEventListener('DOMContentLoaded', function () { | |
var rl_url_params_full = rl_whitelisted_url_params(), | |
rl_url_params = {}, | |
rl_links = document.getElementsByTagName('a'), | |
rl_i = 0, | |
rl_domain = window.location.protocol + '//' + window.location.host; | |
for( rl_i = 0; rl_i < rl_url_params_full.length; rl_i++ ) { | |
var param = rl_get_url_param( rl_url_params_full[rl_i] ); | |
if( param === null ) | |
continue; | |
rl_url_params[ rl_url_params_full[rl_i] ] = param; | |
} | |
// No URL Params are actually provided | |
if( Object.keys( rl_url_params ).length === 0 ) | |
return; | |
for( rl_i = 0; rl_i < rl_links.length; rl_i++) { | |
if( rl_links[rl_i].href.indexOf( rl_domain ) !== 0 ) | |
continue; | |
var href = rl_links[rl_i].href; | |
Object.keys( rl_url_params ).map(function(objectKey, index) { | |
href = rl_set_url_param( href, objectKey, rl_url_params[objectKey] ); | |
}); | |
rl_links[rl_i].href = href; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment