Angular - search for key or value in all scopes

Imagine you have big angular page with many controllers, directives, scopes etc. It can be difficult to find if there is already information you need in some of scopes.

I wrote javsascript script which searches for value in all scopes and objects.


clear();
var options = { phrase: "Managetemplates", exact: false };
var maxRec = 6;

var scopesId = [];
var scopes = [];


var results = [];


function searchInObject(scope, ob, rec) {
  if (rec == maxRec) {
      return;    
  }
 for (var key in ob) {
    if (key.indexOf('$')==0 || key == "this") {
       continue; 
    }
  var val = ob[key];
  if (isPhraseMatched(key) || isPhraseMatched(val)) {
   addResult(key, val, ob, scope);
  }
  if (val && typeof val == "object") {
   searchInObject(scope, val, rec+1);
  }
 }
}

function isPhraseMatched(newPhrase) {
 if (!newPhrase) {
  return;
 }
 var phraseL = (""+options.phrase).toLowerCase();
 var newPhraseL = (""+newPhrase).toLowerCase();

 if (options.exact) {
  return phraseL == newPhraseL;
 } else {
   // console.log(newPhraseL);
   return newPhraseL.indexOf(phraseL) >=0;
 }
}

function addResult(key, val, parent, scope) { 
 //var keyM = (""+key).toLowerCase();
  
  var realVal = val;
  if (typeof realVal == "function") {
    realVal = "";
     var lines = (""+val).split("\n");
     var phraseL = (""+options.phrase).toLowerCase();
    lines.forEach(function(line) {
    if ( (""+line).toLowerCase().indexOf(phraseL) >=0) {
        realVal +=line.trim() + " | ";
    }
  });    
  }
  
 for (var i in results) {
  if (results[i].key == key && angular.equals(results[i].val,realVal)) {
   return;  
  }
 }

  results.push({ key: key, val: realVal, scope:scope, parent:parent });
}

function searchScopes() {
 $('*').each(function(i, el) {
   var scope = $(el).scope();
   if (scope && scopes.indexOf(scope)<0) {
  scopesId.push(scope.$id);
  scopes.push(scope);
   }
 }); 

}

searchScopes();
//console.log(scopes);
scopes.forEach(function(scope) {
  
  searchInObject(scope, scope, 1);
});

results.sort(function(a,b) {
  return a.key>b.key?1:-1;
});

console.dir(results);



How does it work? For each html element on page $('*') it gets its scope (.scope()) and searches in keys and values in this scope. It also searches recursively in object and even in functions body