-2

is there a way to find all variables with a specific value in JS?

I know there is a variable holding value integer 374646 but I can not find it in sources (large codebase).

I think one can implement a solution by recursively checking all variables returned by this but I am not a JS developer

update: the original question was confusing I think.

is there any way that I can find variables by values in a browser debugger (Chrome/Firefox)?

mhrsalehi
  • 1,124
  • 1
  • 12
  • 29
  • 1
    this is for debugging – mhrsalehi Aug 22 '20 at 11:25
  • 3
    What's the budget allocated? /s – Adam Azad Aug 22 '20 at 11:25
  • "I think one can implement a solution by recursively checking all variables returned by this" — You can't. `this` doesn't return variables. – Quentin Aug 22 '20 at 11:26
  • I mean in the debugger console of browsers – mhrsalehi Aug 22 '20 at 11:27
  • maybe you can find the answer in here: https://stackoverflow.com/questions/17276206/list-all-js-global-variables-used-by-site-not-all-defined, my question wasn't answered, but still lots of answers are about YOUR problem – Flash Thunder Aug 22 '20 at 11:33
  • Your edit asking *"is there any way that I can find variables by values in a browser debugger (Chrome/Firefox)?"* is completely different from your original question, which was clearly about using **code** to do it (*"I think one can implement a solution by recursively checking all variables returned by `this`"*). – T.J. Crowder Aug 22 '20 at 11:34
  • yes I agree, I should mention debugger in the title. thank you for comment – mhrsalehi Aug 22 '20 at 12:28

2 Answers2

3

I think one can implement a solution by recursively checking all variables returned by this but I am not a JS developer

No, you can't. Properties are not variables (except in one special case: a certain class of global variables are properties of the global object).

Unless your variable is a global, and it's the kind of global that's available as a property on the global object, you can't find it. There is no way to get a list of other kinds of variables.

For example, there is no way for code at global scope to find a in the following:

const handle = (() => {
    const a = 374646;
    return setInterval(() => {
        console.log(a);
    }, 500);
})();
setTimeout(() => clearInterval(handle), 4000);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
-1

Please note that it's dangerous and do not access the sites, that you don't know with that approach!!! All your data may get stolen, your girlfriend may get r@$%ped, your grandma may die, your dog may bite you in the crotch, all the glaciers in the world may melt, you can get herpes or even start a global nuclear war! Use at own risk! A very high risk if you don't know what running a browser without cross-domain policies means!

Run your browser with cross-domain policies disabled (for example Chromium / Chrome with --disable-web-security param, note : Kill all chrome instances before running command or it won't work), then in the console copy-paste this function:

function globalSearch(startObject, value) {
    var stack = [[startObject,'']];
    var searched = [];
    var found = false;

    var isArray = function(test) {
        return Object.prototype.toString.call( test ) === '[object Array]';
    }

    while(stack.length) {
        var fromStack = stack.pop();
        var obj = fromStack[0];
        var address = fromStack[1];

        if( typeof obj == typeof value && obj == value) {
            var found = address;
            break;
        }else if(typeof obj == "object" && searched.indexOf(obj) == -1){
           if ( isArray(obj) ) {
              var prefix = '[';
              var postfix = ']';
           }else {
              var prefix = '.';
              var postfix = '';
           }
           for( i in obj ) {
              stack.push( [ obj[i], address + prefix + i + postfix ] );
           }
           searched.push(obj);
        }
    }
    return found == '' ? true : found;
}

After that you can search variable by values with:

globalSearch(window,value);

Once again! Remember that it's dangerous and do not access the sites, that you don't know with that approach!!! Disclaimer: If something will happen, you didn't even see that post, I didn't write it! I don't know what StackOverflow is! Never been here.

Flash Thunder
  • 11,672
  • 8
  • 47
  • 91