Skip to content

Instantly share code, notes, and snippets.

@ruzz311
Created December 1, 2015 20:55
Show Gist options
  • Save ruzz311/171d09aee77b8fd3eb54 to your computer and use it in GitHub Desktop.
Save ruzz311/171d09aee77b8fd3eb54 to your computer and use it in GitHub Desktop.
A Tampermonkey script for developing angular.
// ==UserScript==
// @name angular tools
// @namespace http://madsendev.com
// @version 0.1
// @description remove ads on speedtest.net
// @author You
// @match *://*/*
// @grant none
// @run-at document-idle
// ==/UserScript==
(function (window) {
var retryCount = -1,
maxRetry = 5,
timeout = 1000,
timer;
(function initScript () {
window.clearTimeout(timer)
if (!window.angular) {
retryCount++;
if (retryCount < maxRetry) {
timer = setTimeout(initScript, timeout)
}
return;
}
// alert('done loading tools '+ !!window.angular)
setTimeout(chromeAngularTools, 3000);
})();
function chromeAngularTools() {
var ngAppElem = angular.element(document.querySelector('[ng-app],[data-ng-app]') || document);
window.$injector = ngAppElem.injector();
window.inject = $injector.invoke;
window.$rootScope = ngAppElem.scope();
// getService('auth') will create a variable `auth` assigned to the service `auth`.
window.getService = function getService(serviceName) {
inject([serviceName, function (s) {window[serviceName] = s;}]);
};
Object.defineProperty(window, '$scope', {
get: function () {
var elem = angular.element(__commandLineAPI.$0);
return elem.isolateScope() || elem.scope();
},
});
}
/**
* USAGE
*
* First copy the script and paste it in Chrome DevTools in Sources -> left pane -> Snippets.
* Then, after loading an Angular page, right click on the snippet and choose "run".
* Afterwards, you have the following available in the console:
*
* 1) $rootScope
* 2) inject(function ($q, $compile) { ...use $q and $compile here... });
* 3) click on an element in DevTools; now $scope in the console points at the element scope (isolate if one exists).
*
* Enjoy!
*/
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment