diff --git a/javascript-templates/JavaScriptResource.mustache b/javascript-templates/JavaScriptResource.mustache index 625a930..fbddd60 100644 --- a/javascript-templates/JavaScriptResource.mustache +++ b/javascript-templates/JavaScriptResource.mustache @@ -75,46 +75,59 @@ fiftyoneDegreesManager = function() { } } - // Get cookies with the '51D_' prefix that have been added to the request + // Get stored values with the '51D_' prefix that have been added to the request // and return the data as key value pairs. This method is needed to extract - // cookie values for inclusion in the GET or POST request for situations - // where CORS will prevent cookies being sent to third parties. - var getFodCookies = function(){ - var keyValuePairs = document.cookie.split(/; */); - var fodCookies = []; - for(var i = 0; i < keyValuePairs.length; i++) { - var name = keyValuePairs[i].substring(0, keyValuePairs[i].indexOf('=')); - if(startsWith(name, "51D_")){ - var value = keyValuePairs[i].substring(keyValuePairs[i].indexOf('=')+1); - fodCookies[name] = value; + // stored values for inclusion in the GET or POST request for situations + // where CORS will prevent them from being sent to third parties. + var getFodSavedValues = function(){ + let fodValues = {}; + {{#_enableCookies}} + { + let keyValuePairs = document.cookie.split(/; */); + for(let nextPair of keyValuePairs) { + let firstEqualsLocation = nextPair.indexOf('='); + let name = nextPair.substring(0, firstEqualsLocation); + if(startsWith(name, "51D_")){ + let value = nextPair.substring(firstEqualsLocation+1); + fodValues[name] = value; + } } - } - return fodCookies; + }; + {{/_enableCookies}} + {{^_enableCookies}} + { + // Collect values from session storage + let session51DataPrefix = sessionKey + "_data_"; + for(let i = 0, n = window.sessionStorage.length; i < n; ++i) { + let nextKey = window.sessionStorage.key(i); + if(startsWith(nextKey, session51DataPrefix)){ + let value = window.sessionStorage[nextKey]; + fodValues[nextKey.substring(session51DataPrefix.length)] = value; + } + } + }; + {{/_enableCookies}} + return fodValues; }; - // Extract key value pairs from the '51D_' prefixed cookies and concatenates + // Extract key value pairs from the '51D_' prefixed values and concatenates // them to form a query string for the subsequent json refresh. - var getParametersFromCookies = function(){ - var fodCookies = getFodCookies(); + var getParametersFromStorage = function(){ + var fodValues = getFodSavedValues(); var keyValuePairs = []; - for (var key in fodCookies) { - if (fodCookies.hasOwnProperty(key)) { - // Url encode the cookie value. + for (var key in fodValues) { + if (fodValues.hasOwnProperty(key)) { + // Url encode the value. // This is done to ensure that invalid characters (e.g. = chars at the end of // base 64 encoded strings) reach the server intact. // The server will automatically decode the value before passing it into the // Pipeline API. - keyValuePairs.push(key+"="+encodeURIComponent(fodCookies[key])); + keyValuePairs.push(key+"="+encodeURIComponent(fodValues[key])); } } return keyValuePairs; }; - // Delete a cookie. - function deleteCookie(name) { - document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; - } - // Fetch a value safely from the json object. If a key somewhere down the // '.' separated hierarchy of keys is not present then 'undefined' is // returned rather than letting an exception occur. @@ -177,6 +190,12 @@ fiftyoneDegreesManager = function() { // then process them and perform any call-backs required. if (jsProperties !== undefined && jsProperties.length > 0) { + {{^_enableCookies}} + let valueSetPrefix = new RegExp('document\\.cookie\\s*=\\s*(("([A-Za-z0-9_]+)\\s*=\\s*"\\s*\\+\\s*([^\\s};]+))|(`([A-Za-z0-9_]+)\\s*=\\s*\\$\\{([^}]+)\\}`))', 'g'); + let session51DataPrefix = sessionKey + "_data_"; + let sessionSetPatch = 'window.sessionStorage["' + session51DataPrefix + '$3$6"]=$4$7'; + {{/_enableCookies}} + // Execute each of the JavaScript property code snippets using the // index of the value to access the value to avoid problems with // JavaScript returning erroneous values. @@ -189,7 +208,7 @@ fiftyoneDegreesManager = function() { if (body) { toProcess++; } - var isCached = sessionStorage && sessionStorage.getItem(sessionKey + "_" + name); + var isCached = sessionStorage && sessionStorage.getItem(sessionKey + "_property_" + name); if (!isCached) { // Create new function bound to this instance and execute it. @@ -207,6 +226,10 @@ fiftyoneDegreesManager = function() { jsPropertiesStarted.push(name); started++; + {{^_enableCookies}} + body = body.replaceAll(valueSetPrefix, sessionSetPatch); + {{/_enableCookies}} + if (body.indexOf(searchString) !== -1){ callbackCounter++; body = body.replace(/\/\/ 51D replace this comment with callback function./g, 'callbackFunc(resolveFunc, rejectFunc);'); @@ -230,7 +253,7 @@ fiftyoneDegreesManager = function() { func(); } if (sessionStorage) { - sessionStorage.setItem(sessionKey + "_" + name, true) + sessionStorage.setItem(sessionKey + "_property_" + name, true) } } } else { @@ -335,14 +358,6 @@ fiftyoneDegreesManager = function() { // for 'complete' functions to fire. fireChangeFuncs(json); resolve(json); -{{^_enableCookies}} - var fodCookies = getFodCookies(); - for (var key in fodCookies) { - if (fodCookies.hasOwnProperty(key)) { - deleteCookie(key); - } - } -{{/_enableCookies}} } } @@ -353,7 +368,7 @@ fiftyoneDegreesManager = function() { // cached by browsers, the result of the POST request is stored in session // storage on a successful response. This can then be checked before making // repeat requests to the call-back URL. - // Any cookie parameters that have been set by the executed JavaScript + // Any saved value parameters that have been set by the executed JavaScript // properties are added to the list of parameters, this list is then // serialized as Form Data and sent in the POST body to the call-back URL, // refreshing the JSON data. The new JSON is then loaded if the request is @@ -362,11 +377,11 @@ fiftyoneDegreesManager = function() { var processRequest = function(resolve, reject){ loadParameters(); - // Get additional parameters from cookies in case they are not sent + // Get additional parameters in case they are not sent // by the browser. - var cookieParams = getParametersFromCookies(); - for(var cookie in cookieParams) { - var parts = cookieParams[cookie].split('='); + var savedValueParams = getParametersFromStorage(); + for(var savedValueIndex in savedValueParams) { + var parts = savedValueParams[savedValueIndex].split('='); parameters[parts[0]] = parts[1]; } @@ -419,6 +434,7 @@ fiftyoneDegreesManager = function() { // Add the HTTP header for POST form data. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + xhr.setRequestHeader('Accept', 'application/json'); xhr.onload = function () {