Last active
November 29, 2017 16:33
-
-
Save tameemsafi/69dff5b356bd3ad20fdeb04a47a14469 to your computer and use it in GitHub Desktop.
Replace old feedback URL params with new params
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
(function(document) { | |
// Object to store new query params, | |
// the current ones will be replaced, | |
// new ones will be added to the url | |
var newParams = { | |
'recall-ui': '', | |
'lambda-return-code': '', | |
'recall-outcome': '', | |
'recall-outcome-detail': '', | |
'recall-last-update-date': '' | |
}; | |
// Get the feedback link from DOM | |
var feedbackSurveyLink = document.querySelector('a.appendClientID'); | |
// Check if feedback survey link exists on page | |
if( !feedbackSurveyLink ) return; | |
// Get the current feedback link url | |
var feedbackURL = feedbackSurveyLink.href; | |
// Check if feedback has a link | |
if( !feedbackURL ) return; | |
// Create new feedback url from the current url and the new params | |
var newFeedbackURL = replaceURLQueryParams(feedbackURL, newParams); | |
// Check if new feedback url was created successfully | |
if( !newFeedbackURL ) return; | |
// Replace current feedback url with new one | |
feedbackSurveyLink.href = newFeedbackURL; | |
function replaceURLQueryParams(currentURL, newParams) { | |
// Seperate current url | |
// delimited by '?' which is start | |
// of the query params | |
var seperatedURL = currentURL.split('?'); | |
// Check if URL has query params | |
if( !seperatedURL || !seperatedURL[1] ) return; | |
// Seperate params delimited by '&' | |
var seperatedParams = seperatedURL[1].split('&'); | |
// Check if there is any params | |
// and that it is an array | |
if( !seperatedParams || !Array.isArray(seperatedParams) ) return; | |
// Variables for later use | |
var params = {}; | |
var paramsArray = []; | |
var paramsString = ''; | |
var newURL = ''; | |
// Add/replace all params keys/values | |
for(var i = 0; i < seperatedParams.length; i++) { | |
var currentParam = seperatedParams[i]; | |
// Seperate param delimited by '=' | |
var paramKeyValue = currentParam.split('='); | |
// Check if key/value exist and it is an array | |
if( !paramKeyValue || !Array.isArray(paramKeyValue) ) continue; | |
// Get both key and value from, | |
// seperated string | |
var key = paramKeyValue[0]; | |
var value = paramKeyValue[1]; | |
// Check if key and value exist | |
if( !key || key == undefined || !value || value == undefined || value == 'undefined' ) continue; | |
// Add key/value to the params array | |
params[key] = value; | |
} | |
// Replace all params key/value with new ones | |
for(var key in newParams) { | |
// Get new param value | |
var value = newParams[key]; | |
// Check if value exists | |
if( !value || value == undefined || value == 'undefined' ) continue; | |
// Replace '-' with '_' in key | |
var urlSafeKey = key.replace(/-/g, '_'); | |
// Check if key exists | |
if( !urlSafeKey ) continue; | |
// Replace key with value in params | |
params[urlSafeKey] = value; | |
} | |
// Create an array from params, | |
// and url encode the values | |
for(var key in params) { | |
if (params.hasOwnProperty(key)) { | |
var value = params[key]; | |
// Check if value exists | |
if( !value ) continue; | |
// Url encode both key and value, join them with '=' and then add to new array, | |
// which will be used to create a query string | |
var urlEncodedKey = encodeURIComponent(key); | |
var urlEncodedValue = encodeURIComponent(value); | |
// Replace all '%20' and '%2B' values in param with a '+' symbol | |
var valueWithPlusSeperatedString = urlEncodedValue.replace(/(%2B|%20)/g, '+'); | |
// Push the current key/value param to an array as as string, | |
// seperated with a equal sign | |
paramsArray.push(urlEncodedKey + "=" + valueWithPlusSeperatedString); | |
} | |
} | |
// Create query string from params array | |
paramsString = paramsArray.join("&"); | |
// Create new url based on old seperated url, | |
// delimited with '&' | |
newURL = seperatedURL[0] + '?' + paramsString; | |
// Return the new url, | |
// with updated query string params | |
return newURL; | |
} | |
})(document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment