ingress-intel-total-conversion/code/extract_niantic_parameters.js
Jon Atkins 96190be481 extract TILES_PER_EDGE and ZOOM_TO_LEVEL from stock intel
Niantic have been tweaking the values after server overload during Shonin - I expect them to change again...
2015-03-28 18:27:06 +00:00

150 lines
5.4 KiB
JavaScript

// as of 2014-08-14, Niantic have returned to minifying the javascript. This means we no longer get the nemesis object
// and it's various member objects, functions, etc.
// so we need to extract some essential parameters from the code for IITC to use
window.extractFromStock = function() {
window.niantic_params = {}
window.niantic_params.botguard_method_group_flag = {};
// extract the former nemesis.dashboard.config.CURRENT_VERSION from the code
var reVersion = new RegExp('[a-z]=[a-z].getData\\(\\);[a-z].v="([a-f0-9]{40})";');
var minified = new RegExp('^[a-zA-Z$][a-zA-Z$0-9]$');
// required for botguard
var requestPrototype = (function() {
for(var topLevel in window) {
if(!window[topLevel]) continue;
// need an example for a request object
for(var property in window[topLevel]) {
try {
if(window[topLevel][property] == "getRegionScoreDetails") {
return Object.getPrototypeOf(window[topLevel]);
}
} catch(e) { // might throw SecurityError or others (noticed on top.opener, which might be cross-origin)
continue;
}
}
}
})();
for (var topLevel in window) {
if (minified.test(topLevel)) {
// a minified object - check for minified prototype entries
var topObject = window[topLevel];
if (topObject && topObject.prototype) {
// the object has a prototype - iterate through the properties of that
for (var secLevel in topObject.prototype) {
if (minified.test(secLevel)) {
// looks like we've found an object of the format "XX.prototype.YY"...
var item = topObject.prototype[secLevel];
if (item && typeof(item) == "function") {
// a function - test it against the relevant regular expressions
var funcStr = item.toString();
var match = reVersion.exec(funcStr);
if (match) {
console.log('Found former CURRENT_VERSION in '+topLevel+'.prototype.'+secLevel);
niantic_params.CURRENT_VERSION = match[1];
}
}
}
}
} //end 'if .prototype'
if (topObject && Array.isArray && Array.isArray(topObject)) {
// find all non-zero length arrays containing just numbers
if (topObject.length>0) {
var justInts = true;
for (var i=0; i<topObject.length; i++) {
if (typeof(topObject[i]) !== 'number' || topObject[i] != parseInt(topObject[i])) {
justInts = false;
break;
}
}
if (justInts) {
// current lengths are: 17: ZOOM_TO_LEVEL, 16: TILES_PER_EDGE
// however, slightly longer or shorter are a possibility in the future
if (topObject.length >= 15 && topObject.length <= 18) {
// a reasonable array length for tile parameters
// need to find two types:
// a. portal level limits. decreasing numbers, starting at 8
// b. tiles per edge. increasing numbers. current max is 9000
if (topObject[0] == 8) {
// check for tile levels
var decreasing = true;
for (var i=1; i<topObject.length; i++) {
if (topObject[i-1] < topObject[i]) {
decreasing = false;
break;
}
}
if (decreasing) {
console.log ('int array '+topLevel+' looks like ZOOM_TO_LEVEL: '+JSON.stringify(topObject));
window.niantic_params.ZOOM_TO_LEVEL = topObject;
}
} // end if (topObject[0] == 8)
if (topObject[topObject.length-1] == 9000) {
var increasing = true;
for (var i=1; i<topObject.length; i++) {
if (topObject[i-1] > topObject[i]) {
increasing = false;
break;
}
}
if (increasing) {
console.log ('int array '+topLevel+' looks like TILES_PER_EDGE: '+JSON.stringify(topObject));
window.niantic_params.TILES_PER_EDGE = topObject;
}
} //end if (topObject[topObject.length-1] == 9000) {
}
}
}
}
// finding the required method names for the botguard interface code
if(topObject && typeof topObject == "object" && Object.getPrototypeOf(topObject) == requestPrototype) {
var methodKey = Object
.keys(topObject)
.filter(function(key) { return typeof key == "string"; })[0];
for(var secLevel in topObject) {
if(typeof topObject[secLevel] == "boolean") {
window.niantic_params.botguard_method_group_flag[topObject[methodKey]] = topObject[secLevel];
}
}
}
}
}
if (niantic_params.CURRENT_VERSION === undefined || Object.keys(window.niantic_params.botguard_method_group_flag).length == 0) {
dialog({
title: 'IITC Broken',
html: '<p>IITC failed to extract the required parameters from the intel site</p>'
+'<p>This can happen after Niantic update the standard intel site. A fix will be needed from the IITC developers.</p>',
});
console.log('Discovered parameters');
console.log(JSON.stringify(window.niantic_params,null,2));
throw('Error: IITC failed to extract CURRENT_VERSION string - cannot continue');
}
}