Skip to content

Instantly share code, notes, and snippets.

@rinukkusu
Last active January 2, 2023 07:57
Show Gist options
  • Save rinukkusu/491ab6fed4feee08e4671be0598ce011 to your computer and use it in GitHub Desktop.
Save rinukkusu/491ab6fed4feee08e4671be0598ce011 to your computer and use it in GitHub Desktop.
function walkScope(scope) {
function processLevel(scope, idCache) {
if (scope == null)
return null;
if (idCache.indexOf(scope.$id) >= 0)
return null;
idCache.push(scope.$id);
let level = {};
let curScopeObj = {id: scope.$id, scope: scope, childs: []};
curScopeObj.childs = processLevel(scope.$$childHead, idCache);
level[curScopeObj.id] = curScopeObj;
let nextScope = scope.$$nextSibling;
while (nextScope != null) {
if (idCache.indexOf(nextScope.$id) >= 0)
continue;
idCache.push(nextScope.$id);
let nextScopeObj = {id: nextScope.$id, scope: nextScope, childs: []};
nextScopeObj.childs = processLevel(nextScope, idCache);
level[nextScopeObj.id] = nextScopeObj;
nextScope = nextScope.$$nextSibling;
}
return level;
}
let idCache = [];
return processLevel(scope, idCache);
}
@rinukkusu
Copy link
Author

Usage:

const scopes = walkScope($("html").scope());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment