1. 1 : /* global log -- eslint */
  2. 2 :
  3. 3 : /**
  4. 4 : * Extract essential parameters and functions from the Ingress Intel site's minified JavaScript.
  5. 5 : * Necessary due to Niantic's minification and obfuscation of their Intel Map code.
  6. 6 : *
  7. 7 : * @module extract_niantic_parameters
  8. 8 : */
  9. 9 :
  10. 10 : /**
  11. 11 : * Main function to extract required parameters from the Ingress Intel site's JavaScript.
  12. 12 : * Throws an error and shows a dialog if it fails to find the necessary parameters.
  13. 13 : *
  14. 14 : * @function extractFromStock
  15. 15 : */
  16. 16 :
  17. 17 : window.extractFromStock = function () {
  18. 18 : window.niantic_params = {};
  19. 19 :
  20. 20 : // extract the former nemesis.dashboard.config.CURRENT_VERSION from the code
  21. 21 : var reVersion = new RegExp('"X-CSRFToken".*[a-z].v="([a-f0-9]{40})";');
  22. 22 :
  23. 23 : var minified = new RegExp('^[a-zA-Z$][a-zA-Z$0-9]?$');
  24. 24 :
  25. 25 : for (var topLevel in window) {
  26. 26 : if (minified.test(topLevel)) {
  27. 27 : // a minified object - check for minified prototype entries
  28. 28 :
  29. 29 : var topObject = window[topLevel];
  30. 30 : if (topObject && topObject.prototype) {
  31. 31 : // the object has a prototype - iterate through the properties of that
  32. 32 : for (var secLevel in topObject.prototype) {
  33. 33 : if (minified.test(secLevel)) {
  34. 34 : // looks like we've found an object of the format "XX.prototype.YY"...
  35. 35 : var item = topObject.prototype[secLevel];
  36. 36 :
  37. 37 : if (item && typeof item === 'function') {
  38. 38 : // a function - test it against the relevant regular expressions
  39. 39 : var funcStr = item.toString();
  40. 40 :
  41. 41 : var match = reVersion.exec(funcStr);
  42. 42 : if (match) {
  43. 43 : log.log('Found former CURRENT_VERSION in ' + topLevel + '.prototype.' + secLevel);
  44. 44 : window.niantic_params.CURRENT_VERSION = match[1];
  45. 45 : }
  46. 46 : }
  47. 47 : }
  48. 48 : }
  49. 49 : } // end 'if .prototype'
  50. 50 :
  51. 51 : if (topObject && Array.isArray && Array.isArray(topObject)) {
  52. 52 : // find all non-zero length arrays containing just numbers
  53. 53 : if (topObject.length > 0) {
  54. 54 : var justInts = true;
  55. 55 : for (var i = 0; i < topObject.length; i++) {
  56. 56 : if (typeof topObject[i] !== 'number' || topObject[i] !== parseInt(topObject[i])) {
  57. 57 : justInts = false;
  58. 58 : break;
  59. 59 : }
  60. 60 : }
  61. 61 : if (justInts) {
  62. 62 : // current lengths are: 17: ZOOM_TO_LEVEL, 14: TILES_PER_EDGE
  63. 63 : // however, slightly longer or shorter are a possibility in the future
  64. 64 :
  65. 65 : if (topObject.length >= 12 && topObject.length <= 18) {
  66. 66 : // a reasonable array length for tile parameters
  67. 67 : // need to find two types:
  68. 68 : // a. portal level limits. decreasing numbers, starting at 8
  69. 69 : // b. tiles per edge. increasing numbers. current max is 36000, 9000 was the previous value - 18000 is a likely possibility too
  70. 70 :
  71. 71 : if (topObject[0] === 8) {
  72. 72 : // check for tile levels
  73. 73 : var decreasing = true;
  74. 74 : for (let i = 1; i < topObject.length; i++) {
  75. 75 : if (topObject[i - 1] < topObject[i]) {
  76. 76 : decreasing = false;
  77. 77 : break;
  78. 78 : }
  79. 79 : }
  80. 80 : if (decreasing) {
  81. 81 : window.niantic_params.ZOOM_TO_LEVEL = topObject;
  82. 82 : }
  83. 83 : } // end if (topObject[0] == 8)
  84. 84 :
  85. 85 : // 2015-06-25 - changed to top value of 64000, then to 32000 - allow for them to restore it just in case
  86. 86 : if (topObject[topObject.length - 1] >= 9000 && topObject[topObject.length - 1] <= 64000) {
  87. 87 : var increasing = true;
  88. 88 : for (let i = 1; i < topObject.length; i++) {
  89. 89 : if (topObject[i - 1] > topObject[i]) {
  90. 90 : increasing = false;
  91. 91 : break;
  92. 92 : }
  93. 93 : }
  94. 94 : if (increasing) {
  95. 95 : window.niantic_params.TILES_PER_EDGE = topObject;
  96. 96 : }
  97. 97 : } // end if (topObject[topObject.length-1] == 9000) {
  98. 98 : }
  99. 99 : }
  100. 100 : }
  101. 101 : }
  102. 102 : }
  103. 103 : }
  104. 104 :
  105. 105 : if (window.niantic_params.CURRENT_VERSION === undefined) {
  106. 106 : window.dialog({
  107. 107 : title: 'IITC Broken',
  108. 108 : html:
  109. 109 : '<p>IITC failed to extract the required parameters from the intel site</p>' +
  110. 110 : '<p>This can happen after Niantic update the standard intel site. A fix will be needed from the IITC developers.</p>',
  111. 111 : });
  112. 112 :
  113. 113 : log.log('Discovered parameters');
  114. 114 : log.log(JSON.stringify(window.niantic_params, null, 2));
  115. 115 :
  116. 116 : throw new Error('Error: IITC failed to extract CURRENT_VERSION string - cannot continue');
  117. 117 : }
  118. 118 : };