@@ -82,13 +82,13 @@ fiftyoneDegreesManager = function() {
82
82
// and return the data as key value pairs. This method is needed to extract
83
83
// stored values for inclusion in the GET or POST request for situations
84
84
// where CORS will prevent them from being sent to third parties.
85
- var getFodSavedValues = function(){
85
+ var getFodSavedValues = function() {
86
86
let fodValues = {} ;
87
87
{ {#_enableCookies} }
88
88
{
89
89
let keyValuePairs = document.cookie.split(/; */);
90
90
for(let nextPair of keyValuePairs) {
91
- let firstEqualsLocation = nextPair.indexOf(' = ' );
91
+ let firstEqualsLocation = nextPair.indexOf(" = " );
92
92
let name = nextPair.substring(0, firstEqualsLocation);
93
93
if (startsWith(name, " 51D_" )){
94
94
let value = nextPair.substring(firstEqualsLocation+1);
@@ -115,17 +115,17 @@ fiftyoneDegreesManager = function() {
115
115
116
116
// Extract key value pairs from the '51D_' prefixed values and concatenates
117
117
// them to form a query string for the subsequent json refresh.
118
- var getParametersFromStorage = function(){
118
+ var getParametersFromStorage = function() {
119
119
var fodValues = getFodSavedValues();
120
120
var keyValuePairs = [];
121
121
for (var key in fodValues) {
122
122
if (fodValues.hasOwnProperty(key)) {
123
123
// 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
125
125
// 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
127
127
// Pipeline API.
128
- keyValuePairs.push(key+ " =" + encodeURIComponent(fodValues[key]));
128
+ keyValuePairs.push(key + " =" + encodeURIComponent(fodValues[key]));
129
129
}
130
130
}
131
131
return keyValuePairs;
@@ -136,21 +136,21 @@ fiftyoneDegreesManager = function() {
136
136
// returned rather than letting an exception occur.
137
137
var getFromJson = function(key, allowObjects, allowBooleans) {
138
138
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 ; }
141
141
142
- if (typeof(key) === ' string' ) {
142
+ if (typeof(key) === " string" ) {
143
143
var functions = json;
144
144
var segments = key.split(' .' );
145
145
var i = 0;
146
146
while (functions !== undefined && i < segments.length) {
147
147
functions = functions[segments[i++]];
148
148
}
149
- if (typeof( functions) === "string") {
149
+ if (typeof functions === "string") {
150
150
result = functions;
151
- } else if (allowBooleans && typeof( functions) === "boolean") {
151
+ } else if (allowBooleans && typeof functions === "boolean") {
152
152
result = functions;
153
- } else if (allowObjects && typeof functions === ' object' && functions !== null) {
153
+ } else if (allowObjects && typeof functions === " object" && functions !== null) {
154
154
result = functions;
155
155
}
156
156
}
@@ -186,7 +186,6 @@ fiftyoneDegreesManager = function() {
186
186
var executeCallback = true ;
187
187
var started = 0;
188
188
var cached = 0;
189
- var cachedResponse = undefined;
190
189
var toProcess = 0;
191
190
192
191
// If there is no cached response and there are JavaScript code snippets
@@ -198,80 +197,104 @@ fiftyoneDegreesManager = function() {
198
197
let session51DataPrefix = sessionKey + "_data_";
199
198
let sessionSetPatch = 'window.sessionStorage["' + session51DataPrefix + '$3$6"]=$4$7';
200
199
{ {/_enableCookies} }
201
-
200
+
202
201
// Execute each of the JavaScript property code snippets using the
203
202
// index of the value to access the value to avoid problems with
204
203
// JavaScript returning erroneous values.
205
204
for (var index = 0; index < jsProperties.length; index++) {
206
205
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);
209
217
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();
213
267
}
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);
260
284
}
285
+ started--;
286
+ toProcess--;
261
287
}
262
- } else {
263
- cached++;
264
288
}
265
289
}
266
290
}
267
291
}
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 ;
275
298
}
276
299
}
277
300
@@ -280,6 +303,7 @@ fiftyoneDegreesManager = function() {
280
303
failed = false ;
281
304
completed = true ;
282
305
}
306
+
283
307
if (executeCallback) {
284
308
callbackCounter = 1;
285
309
completedCallback(resolve, reject);
0 commit comments