Skip to content

Commit 68ef908

Browse files
Merged Pull Request '#47 nightly-package-update->main : Updated packages.'
Updated packages.
2 parents f5fd265 + 190cdaf commit 68ef908

File tree

1 file changed

+98
-74
lines changed

1 file changed

+98
-74
lines changed

javascript-templates/JavaScriptResource.mustache

Lines changed: 98 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ fiftyoneDegreesManager = function() {
8282
// and return the data as key value pairs. This method is needed to extract
8383
// stored values for inclusion in the GET or POST request for situations
8484
// where CORS will prevent them from being sent to third parties.
85-
var getFodSavedValues = function(){
85+
var getFodSavedValues = function() {
8686
let fodValues = {};
8787
{{#_enableCookies}}
8888
{
8989
let keyValuePairs = document.cookie.split(/; */);
9090
for(let nextPair of keyValuePairs) {
91-
let firstEqualsLocation = nextPair.indexOf('=');
91+
let firstEqualsLocation = nextPair.indexOf("=");
9292
let name = nextPair.substring(0, firstEqualsLocation);
9393
if(startsWith(name, "51D_")){
9494
let value = nextPair.substring(firstEqualsLocation+1);
@@ -115,17 +115,17 @@ fiftyoneDegreesManager = function() {
115115

116116
// Extract key value pairs from the '51D_' prefixed values and concatenates
117117
// them to form a query string for the subsequent json refresh.
118-
var getParametersFromStorage = function(){
118+
var getParametersFromStorage = function() {
119119
var fodValues = getFodSavedValues();
120120
var keyValuePairs = [];
121121
for (var key in fodValues) {
122122
if (fodValues.hasOwnProperty(key)) {
123123
// Url encode the value.
124-
// This is done to ensure that invalid characters (e.g. = chars at the end of
124+
// This is done to ensure that invalid characters (e.g. = chars at the end of
125125
// base 64 encoded strings) reach the server intact.
126-
// The server will automatically decode the value before passing it into the
126+
// The server will automatically decode the value before passing it into the
127127
// Pipeline API.
128-
keyValuePairs.push(key+"="+encodeURIComponent(fodValues[key]));
128+
keyValuePairs.push(key + "=" + encodeURIComponent(fodValues[key]));
129129
}
130130
}
131131
return keyValuePairs;
@@ -136,21 +136,21 @@ fiftyoneDegreesManager = function() {
136136
// returned rather than letting an exception occur.
137137
var getFromJson = function(key, allowObjects, allowBooleans) {
138138
var result = undefined;
139-
if(typeof allowObjects === 'undefined') { allowObjects = false; }
140-
if(typeof allowBooleans === 'undefined') { allowBooleans = false; }
139+
if (typeof allowObjects === "undefined") { allowObjects = false; }
140+
if (typeof allowBooleans === "undefined") { allowBooleans = false; }
141141

142-
if (typeof(key) === 'string') {
142+
if (typeof(key) === "string") {
143143
var functions = json;
144144
var segments = key.split('.');
145145
var i = 0;
146146
while (functions !== undefined && i < segments.length) {
147147
functions = functions[segments[i++]];
148148
}
149-
if (typeof(functions) === "string") {
149+
if (typeof functions === "string") {
150150
result = functions;
151-
} else if (allowBooleans && typeof(functions) === "boolean") {
151+
} else if (allowBooleans && typeof functions === "boolean") {
152152
result = functions;
153-
} else if (allowObjects && typeof functions === 'object' && functions !== null) {
153+
} else if (allowObjects && typeof functions === "object" && functions !== null) {
154154
result = functions;
155155
}
156156
}
@@ -186,7 +186,6 @@ fiftyoneDegreesManager = function() {
186186
var executeCallback = true;
187187
var started = 0;
188188
var cached = 0;
189-
var cachedResponse = undefined;
190189
var toProcess = 0;
191190
192191
// If there is no cached response and there are JavaScript code snippets
@@ -198,80 +197,104 @@ fiftyoneDegreesManager = function() {
198197
let session51DataPrefix = sessionKey + "_data_";
199198
let sessionSetPatch = 'window.sessionStorage["' + session51DataPrefix + '$3$6"]=$4$7';
200199
{{/_enableCookies}}
201-
200+
202201
// Execute each of the JavaScript property code snippets using the
203202
// index of the value to access the value to avoid problems with
204203
// JavaScript returning erroneous values.
205204
for (var index = 0; index < jsProperties.length; index++) {
206205
var name = jsProperties[index];
207-
if (jsPropertiesStarted.indexOf(name) === -1) {
208-
var body = getFromJson(name);
206+
if (jsPropertiesStarted.indexOf(name) !== -1) {
207+
continue;
208+
}
209+
var body = getFromJson(name);
210+
211+
// If there is a body then this property should be processed.
212+
if (body) {
213+
toProcess++;
214+
}
215+
216+
var isCached = sessionStorage && sessionStorage.getItem(sessionKey + "_property_" + name);
209217

210-
// If there is a body then this property should be processed.
211-
if (body) {
212-
toProcess++;
218+
// If the property has already been processed then skip it.
219+
if (isCached) {
220+
cached++;
221+
continue;
222+
}
223+
224+
// Create new function bound to this instance and execute it.
225+
// This is needed to ensure the scope of the function is
226+
// associated with this instance if any members are altered or
227+
// added. Avoids global scoped variables.
228+
229+
var delay = getFromJson(name + 'delayexecution', false, true);
230+
231+
if (
232+
(ignoreDelayFlag || delay === undefined || delay === false) &&
233+
typeof body === "string" &&
234+
body.length
235+
) {
236+
var func = undefined;
237+
var searchString = '// 51D replace this comment with callback function.';
238+
completed = false;
239+
jsPropertiesStarted.push(name);
240+
started++;
241+
242+
{{^_enableCookies}}
243+
body = body.replaceAll(valueSetPrefix, sessionSetPatch);
244+
{{/_enableCookies}}
245+
246+
if (body.indexOf(searchString) !== -1){
247+
callbackCounter++;
248+
body = body.replace(/\/\/ 51D replace this comment with callback function./g, 'callbackFunc(resolveFunc, rejectFunc);');
249+
func = new Function('callbackFunc', 'resolveFunc', 'rejectFunc',
250+
"try {\n" +
251+
body + "\n" +
252+
"} catch (err) {\n" +
253+
"console.log(err);" +
254+
"}"
255+
);
256+
func(completedCallback, resolve, reject);
257+
executeCallback = false;
258+
} else {
259+
func = new Function(
260+
"try {\n" +
261+
body + "\n" +
262+
"} catch (err) {\n" +
263+
"console.log(err);" +
264+
"}"
265+
);
266+
func();
213267
}
214-
var isCached = sessionStorage && sessionStorage.getItem(sessionKey + "_property_" + name);
215-
216-
if (!isCached) {
217-
// Create new function bound to this instance and execute it.
218-
// This is needed to ensure the scope of the function is
219-
// associated with this instance if any members are altered or
220-
// added. Avoids global scoped variables.
221-
222-
var delay = getFromJson(name + 'delayexecution', false, true);
223-
224-
if ((ignoreDelayFlag || (delay === undefined || delay === false)) &&
225-
typeof body === "string" && body.length) {
226-
var func = undefined;
227-
var searchString = '// 51D replace this comment with callback function.';
228-
completed = false;
229-
jsPropertiesStarted.push(name);
230-
started++;
231-
232-
{{^_enableCookies}}
233-
body = body.replaceAll(valueSetPrefix, sessionSetPatch);
234-
{{/_enableCookies}}
235-
236-
if (body.indexOf(searchString) !== -1){
237-
callbackCounter++;
238-
body = body.replace(/\/\/ 51D replace this comment with callback function./g, 'callbackFunc(resolveFunc, rejectFunc);');
239-
func = new Function('callbackFunc', 'resolveFunc', 'rejectFunc',
240-
"try {\n" +
241-
body + "\n" +
242-
"} catch (err) {\n" +
243-
"console.log(err);" +
244-
"}"
245-
);
246-
func(completedCallback, resolve, reject);
247-
executeCallback = false;
248-
} else {
249-
func = new Function(
250-
"try {\n" +
251-
body + "\n" +
252-
"} catch (err) {\n" +
253-
"console.log(err);" +
254-
"}"
255-
);
256-
func();
257-
}
258-
if (sessionStorage) {
259-
sessionStorage.setItem(sessionKey + "_property_" + name, true)
268+
269+
if (sessionStorage) {
270+
sessionStorage.setItem(sessionKey + "_property_" + name, true)
271+
}
272+
273+
// If the property is `javascripthardwareprofile` then check if the
274+
// profile has been set. If not then remove current property from
275+
// the list of properties that have started to prevent the
276+
// 2nd request from being made.
277+
if (name === "device.javascripthardwareprofile") {
278+
var hrw = getFodSavedValues();
279+
if (hrw && !hrw["51D_ProfileIds"]) {
280+
// find and remove name from jsPropertiesStarted
281+
var propIndex = jsPropertiesStarted.indexOf(name);
282+
if (propIndex > -1) {
283+
jsPropertiesStarted.splice(index, 1);
260284
}
285+
started--;
286+
toProcess--;
261287
}
262-
} else {
263-
cached++;
264288
}
265289
}
266290
}
267291
}
268-
if(cached === toProcess || started === 0) {
269-
if (sessionStorage) {
270-
var cachedResponse = sessionStorage.getItem(sessionKey);
271-
if (cachedResponse) {
272-
loadJSON(resolve, reject, cachedResponse);
273-
executeCallback = false;
274-
}
292+
293+
if ((cached === toProcess || started === 0) && sessionStorage) {
294+
var cachedResponse = sessionStorage.getItem(sessionKey);
295+
if (cachedResponse) {
296+
loadJSON(resolve, reject, cachedResponse);
297+
executeCallback = false;
275298
}
276299
}
277300

@@ -280,6 +303,7 @@ fiftyoneDegreesManager = function() {
280303
failed = false;
281304
completed = true;
282305
}
306+
283307
if (executeCallback) {
284308
callbackCounter = 1;
285309
completedCallback(resolve, reject);

0 commit comments

Comments
 (0)