2014-10-22 14:43:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('batarang.inspected-app', []).
|
2014-12-11 21:06:22 +00:00
|
|
|
service('inspectedApp', ['$rootScope', '$q', inspectedAppService]);
|
2014-10-22 14:43:29 +00:00
|
|
|
|
2014-12-11 21:06:22 +00:00
|
|
|
function inspectedAppService($rootScope, $q) {
|
2014-10-22 14:43:29 +00:00
|
|
|
|
|
|
|
// TODO: maybe state should live elsewhere
|
|
|
|
var scopes = this.scopes = {},
|
|
|
|
hints = this.hints = [];
|
|
|
|
|
|
|
|
this.watch = function (scopeId, path) {
|
|
|
|
return invokeAngularHintMethod('watch', scopeId, path);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.unwatch = function (scopeId, path) {
|
|
|
|
return invokeAngularHintMethod('unwatch', scopeId, path);
|
|
|
|
};
|
|
|
|
|
2014-12-09 22:32:45 +00:00
|
|
|
this.assign = function (scopeId, path, value) {
|
|
|
|
return invokeAngularHintMethod('assign', scopeId, path, value);
|
|
|
|
};
|
|
|
|
|
2014-12-10 18:34:09 +00:00
|
|
|
this.enableInstrumentation = function (setting) {
|
|
|
|
setting = !!setting;
|
|
|
|
chrome.devtools.inspectedWindow.eval(
|
2014-12-11 21:06:22 +00:00
|
|
|
"(function () {" +
|
|
|
|
"var prev = document.cookie.indexOf('__ngDebug=true') !== -1;" +
|
|
|
|
"if (prev !== " + setting + ") {" +
|
|
|
|
"window.document.cookie = '__ngDebug=" + setting + ";';" +
|
|
|
|
"window.document.location.reload();" +
|
|
|
|
"}" +
|
|
|
|
"}())"
|
2014-12-10 18:34:09 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2014-12-11 21:06:22 +00:00
|
|
|
this.getInstrumentationStatus = function () {
|
|
|
|
return $q(function(resolve, reject) {
|
|
|
|
chrome.devtools.inspectedWindow.eval(
|
|
|
|
"document.cookie.indexOf('__ngDebug=true') !== -1", resolve);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-12-09 23:08:02 +00:00
|
|
|
/*
|
|
|
|
* sets window.$scope to the scope of the given id
|
|
|
|
*/
|
|
|
|
this.inspectScope = function (scopeId) {
|
|
|
|
return invokeAngularHintMethod('inspectScope', scopeId);
|
|
|
|
};
|
|
|
|
|
2014-12-09 22:32:45 +00:00
|
|
|
function invokeAngularHintMethod(method, scopeId, path, value) {
|
|
|
|
var args = [parseInt(scopeId, 10), path || ''].
|
|
|
|
map(JSON.stringify).
|
|
|
|
concat(value ? [value] : []).
|
|
|
|
join(',');
|
|
|
|
|
2014-10-22 14:43:29 +00:00
|
|
|
chrome.devtools.inspectedWindow.eval('angular.hint.' + method + '(' + args + ')');
|
|
|
|
}
|
|
|
|
|
|
|
|
var port = chrome.extension.connect();
|
|
|
|
port.postMessage(chrome.devtools.inspectedWindow.tabId);
|
|
|
|
port.onMessage.addListener(function(msg) {
|
|
|
|
$rootScope.$applyAsync(function () {
|
|
|
|
if (msg === 'refresh') {
|
|
|
|
onRefreshMessage();
|
2014-10-23 01:11:23 +00:00
|
|
|
} else if (typeof msg === 'string') {
|
2014-10-22 14:43:29 +00:00
|
|
|
var hint = JSON.parse(msg);
|
|
|
|
onHintMessage(hint);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
port.onDisconnect.addListener(function (a) {
|
|
|
|
console.log(a);
|
|
|
|
});
|
|
|
|
|
|
|
|
function onHintMessage(hint) {
|
|
|
|
if (hint.message) {
|
|
|
|
hints.push(hint);
|
2014-12-11 19:20:20 +00:00
|
|
|
} else if (hint.event) {
|
2014-12-15 18:42:39 +00:00
|
|
|
if (hint.event === 'scope:new') {
|
|
|
|
addNewScope(hint);
|
|
|
|
} else if (hint.id && scopes[hint.id]) {
|
|
|
|
if (hint.event === 'model:change') {
|
|
|
|
scopes[hint.id].models[hint.path] = (typeof hint.value === 'undefined') ?
|
|
|
|
undefined : JSON.parse(hint.value);
|
|
|
|
} else if (hint.event === 'scope:link') {
|
|
|
|
scopes[hint.id].descriptor = hint.descriptor;
|
2014-12-11 19:20:20 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-22 14:43:29 +00:00
|
|
|
$rootScope.$broadcast(hint.event, hint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onRefreshMessage() {
|
|
|
|
hints.length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addNewScope (hint) {
|
|
|
|
scopes[hint.child] = {
|
|
|
|
parent: hint.parent,
|
|
|
|
children: [],
|
|
|
|
models: {}
|
|
|
|
};
|
|
|
|
if (scopes[hint.parent]) {
|
|
|
|
scopes[hint.parent].children.push(hint.child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|