Changeset 881 for trunk/autoquest-htmlmonitor/src/main
- Timestamp:
- 10/16/12 08:28:25 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/autoquest-htmlmonitor/src/main/js/autoquest-htmlmonitor.js
r879 r881 26 26 */ 27 27 var autoquestDestination; 28 29 /** 30 * an ID that is more or less unique for the client 31 */ 32 var autoquestClientId; 28 33 29 34 /** … … 287 292 288 293 log("reading client id"); 289 clientId = readClientId();294 clientId = getClientId(); 290 295 if ((clientId) && (clientId !== "")) { 291 296 message += "\"clientId\":\"" + clientId + "\","; … … 320 325 } 321 326 322 log("sending message");323 request. open("POST", autoquestDestination, true);327 request.open("POST", autoquestDestination, false); 328 request.setRequestHeader("Content-Type", "application/json"); 324 329 325 330 log("sending " + message); … … 368 373 369 374 /** 370 * reads the id of the client from the cookies. 371 * 372 * @returns the client id or null, if none is found in the cookies 373 */ 374 function readClientId() { 375 var cookie = document.cookie; 376 377 var expectedCookieName = getClientIdCookieName(); 378 379 var cookiename = null; 380 var startIndex = 0; 381 382 var clientId = null; 383 384 do { 385 cookie = cookie.substring(startIndex, cookie.length); 386 cookiename = cookie.substr(0, cookie.search('=')); 387 startIndex = cookie.search(';') + 1; 388 389 while (cookie.charAt(startIndex) === ' ') { 390 startIndex++; 391 } 392 } 393 while ((startIndex > 0) && (cookiename !== expectedCookieName)); 394 395 if (cookiename === expectedCookieName) { 396 clientId = cookie.substr(cookie.search('=') + 1, cookie.search(';')); 397 if (clientId === "") { 398 clientId = cookie.substr(cookie.search('=') + 1, cookie.length); 399 } 400 } 401 402 if ((!clientId) || (clientId === "") || (clientId.search(getClientIdPrefix()) !== 0)) { 403 clientId = generateClientId(); 404 storeClientId(clientId); 405 } 406 407 return clientId; 408 } 409 410 411 /** 412 * stores the provided client id in the cookies 413 * 414 * @param clientId the client id to be stored 415 */ 416 function storeClientId(clientId) { 417 if ((clientId) && (clientId !== "")) { 418 var expiry = new Date(); 419 // 10 years should be sufficient :-) 420 expiry = new Date(expiry.getTime() + 1000*60*60*24*365*10); 421 document.cookie = getClientIdCookieName() + '=' + clientId + 422 '; expires=' + expiry.toGMTString()+';'; 423 } 424 } 425 426 /** 427 * returns the name of the cookie used to store the client id 428 * 429 * @returns as described 430 */ 431 function getClientIdCookieName() { 432 return document.URL + "/quest-htmlmonitor/quest-client-id"; 433 } 434 435 /** 436 * generates a client id based on the result of {@link #getClientIdPrefix()} and the current time 437 * stamp 375 * generates a client id based on several information retrieved from the environment. The client 376 * id is not always unique 438 377 * 439 378 * @returns the client id 440 379 */ 441 function generateClientId() { 442 return getClientIdPrefix() + new Date().getTime(); 443 } 444 445 /** 446 * generates a client id prefix based on the user agent and the navigators platform. The prefix 447 * is a simple checksum of the concatenation of both strings 448 * 449 * @returns the client id prefix 450 */ 451 function getClientIdPrefix() { 452 // create something like a more or less unique checksum. It is sufficient, if it differs 453 // only often, but not always, because it is concatenated with a time stamp, which differs 454 // much more often. 455 var prefixStr = navigator.userAgent + "_" + navigator.platform; 456 var prefixId = prefixStr.length; 380 function getClientId() { 381 var clientIdStr; 382 var clientId; 457 383 var i = 0; 458 384 459 for (i = 0; i < prefixStr.length; i++) { 460 prefixId += prefixStr.charCodeAt(i); 461 } 462 463 // ensure, that a string is created and not a long. Otherwise, it can not be checked, if an 464 // existing client id starts with the client id prefix and can therefore be reused. 465 return prefixId.toString(); 385 if (!autoquestClientId) { 386 // create something like a more or less unique checksum. 387 clientIdStr = 388 navigator.appCodeName + navigator.appName + navigator.appVersion + 389 navigator.cookieEnabled + navigator.language + navigator.platform + 390 navigator.userAgent + navigator.javaEnabled() + window.location.protocol + 391 window.location.host + new Date().getTimezoneOffset(); 392 393 clientId = clientIdStr.length; 394 395 for (i = 0; i < clientIdStr.length; i++) { 396 clientId += clientIdStr.charCodeAt(i); 397 } 398 399 autoquestClientId = clientId; 400 } 401 402 return autoquestClientId; 466 403 } 467 404
Note: See TracChangeset
for help on using the changeset viewer.