Created
June 13, 2017 06:44
-
-
Save likwrk/41923de79d3b04d39d07a86512d79cda to your computer and use it in GitHub Desktop.
angular1 watchers counter userscript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name watchers | |
// @namespace *.dev | |
// @version 1 | |
// @grant none | |
// @run-at document-end | |
// ==/UserScript== | |
(function(){ | |
if (!window.hasOwnProperty('angular')) return; | |
window.getWatchers = function(root) { | |
root = angular.element(root || document.documentElement); | |
var watcherCount = 0; | |
function getElemWatchers(element) { | |
var isolateWatchers = getWatchersFromScope(element.data().$isolateScope); | |
var scopeWatchers = getWatchersFromScope(element.data().$scope); | |
var watchers = scopeWatchers.concat(isolateWatchers); | |
angular.forEach(element.children(), function (childElement) { | |
watchers = watchers.concat(getElemWatchers(angular.element(childElement))); | |
}); | |
return watchers; | |
} | |
function getWatchersFromScope(scope) { | |
if (scope) { | |
return scope.$$watchers || []; | |
} else { | |
return []; | |
} | |
} | |
function getWatcherExpressions (element) { | |
var watchers = getElemWatchers(element); | |
var watchlist = { | |
'_total': watchers.length | |
}; | |
angular.forEach(watchers, function (watcher) { | |
watchlist[watcher.exp] = watchlist[watcher.exp] || []; | |
watchlist[watcher.exp].push(watcher); | |
}); | |
return watchlist; | |
} | |
return getWatcherExpressions(root); | |
} | |
var wathcers = 0; | |
var showWatchersCount = function() { | |
if (wathcers === window.getWatchers()._total) return; | |
wathcers = window.getWatchers()._total; | |
console.log('watchers', wathcers); | |
} | |
document.onreadystatechange = function () { | |
if (document.readyState == "complete") { | |
showWatchersCount(); | |
setInterval(showWatchersCount, 2000); | |
} | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment