Currently, I am in the process of troubleshooting a system that was developed by another individual. The issue at hand involves a minified library that initiates an ajax (xhr) call to an external data source upon page load and then stores the retrieved data in an object or variable. Since the call happens immediately after page load, it cannot be intercepted using a function for registering all XHR call requests. To tackle this problem, I am attempting to iterate through all variables and objects present in the browser window with the following approach:
var foundVar = false;
loopThroughObject(this);
function loopThroughObject(obj) {
if (!foundVar) {
try {
for (var name in obj) {
if (obj[name] && {}.toString.call(obj[name]) === '[object Function]') { //ensuring the object is not a function
} else {
if (name == 'searchedKey') { //identified the object with key = searchedKey
console.log(obj[name]);
foundVar = true;
} else {
setTimeout(loopThroughObject.bind(null, obj[name]), 10); //further recursion of inner objects
}
}
}
} catch (error) {}
}
}
The challenge arises when the
setTimeout(loopThroughObject.bind(null, obj[name]), 0);
section accumulates and leads to a memory problem. I attempted the straightforward loopThroughObject(obj[name])
, but encountered the "Too much recursion" error after 25000 iterations. Additionally, it appears that there may be some oversight causing the loop to navigate certain unnecessary object types. Is there anyone who can suggest a more efficient method for managing this iteration?
On a side note, I tested the code on a basic HTML page and it performed without issues:
<html>
<head>
<script>
var test = JSON.parse("{\"searchedKey\":\"12\"}");
</script>
</head>
<body>
</body>
</html>