deterministic.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. (function () {
  3. var random_count = 0;
  4. var random_count_threshold = 25;
  5. var random_seed = 0.462;
  6. Math.random = function() {
  7. random_count++;
  8. if (random_count > random_count_threshold){
  9. random_seed += 0.1;
  10. random_count = 1;
  11. }
  12. return (random_seed % 1);
  13. };
  14. if (typeof(crypto) == 'object' &&
  15. typeof(crypto.getRandomValues) == 'function') {
  16. crypto.getRandomValues = function(arr) {
  17. var scale = Math.pow(256, arr.BYTES_PER_ELEMENT);
  18. for (var i = 0; i < arr.length; i++) {
  19. arr[i] = Math.floor(Math.random() * scale);
  20. }
  21. return arr;
  22. };
  23. }
  24. })();
  25. (function () {
  26. var date_count = 0;
  27. var date_count_threshold = 25;
  28. var orig_date = Date;
  29. // This should be replaced by web page replay by corresponding date
  30. // (usually the date when the recording was done)
  31. var time_seed = {{WPR_TIME_SEED_TIMESTAMP}};
  32. Date = function() {
  33. if (this instanceof Date) {
  34. date_count++;
  35. if (date_count > date_count_threshold){
  36. time_seed += 50;
  37. date_count = 1;
  38. }
  39. switch (arguments.length) {
  40. case 0: return new orig_date(time_seed);
  41. case 1: return new orig_date(arguments[0]);
  42. default: return new orig_date(arguments[0], arguments[1],
  43. arguments.length >= 3 ? arguments[2] : 1,
  44. arguments.length >= 4 ? arguments[3] : 0,
  45. arguments.length >= 5 ? arguments[4] : 0,
  46. arguments.length >= 6 ? arguments[5] : 0,
  47. arguments.length >= 7 ? arguments[6] : 0);
  48. }
  49. }
  50. return new Date().toString();
  51. };
  52. Date.__proto__ = orig_date;
  53. Date.prototype = orig_date.prototype;
  54. Date.prototype.constructor = Date;
  55. orig_date.now = function() {
  56. return new Date().getTime();
  57. };
  58. orig_date.prototype.getTimezoneOffset = function() {
  59. var dst2010Start = 1268560800000;
  60. var dst2010End = 1289120400000;
  61. if (this.getTime() >= dst2010Start && this.getTime() < dst2010End)
  62. return 420;
  63. return 480;
  64. };
  65. })();