ScriptAgent tracks all executed scripts, defines internal breakpoints, and interfaces with the remote debugger.
function _onFrameNavigated(event, res) {
// Clear maps when navigating to a new page, but not if an iframe was loaded
if (!res.frame.parentId) {
_reset();
}
}
Initialize the agent
function load() {
_reset();
_load = new $.Deferred();
var enableResult = new $.Deferred();
Inspector.Debugger.enable().done(function () {
Inspector.Debugger.setPauseOnExceptions("uncaught").done(function () {
enableResult.resolve();
});
});
Inspector.Page.on("frameNavigated.ScriptAgent", _onFrameNavigated);
DOMAgent.on("getDocument.ScriptAgent", _onGetDocument);
Inspector.Debugger
.on("scriptParsed.ScriptAgent", _onScriptParsed)
.on("scriptFailedToParse.ScriptAgent", _onScriptFailedToParse)
.on("paused.ScriptAgent", _onPaused);
Inspector.DOM.on("childNodeInserted.ScriptAgent", _onChildNodeInserted);
return $.when(_load.promise(), enableResult.promise());
}
Get the script information for a given url
function scriptForURL(url) {
return _urlToScript[url];
}
// DOMAgent Event: Document root loaded
function _onGetDocument(event, res) {
Inspector.DOMDebugger.setDOMBreakpoint(res.root.nodeId, "subtree-modified");
_load.resolve();
}
// WebInspector Event: DOM.childNodeInserted
function _onChildNodeInserted(event, res) {
// res = {parentNodeId, previousNodeId, node}
if (_insertTrace) {
var node = DOMAgent.nodeWithId(res.node.nodeId);
node.trace = _insertTrace;
_insertTrace = undefined;
}
}
// TODO: Strip off query/hash strings from URL (see CSSAgent._canonicalize())
// WebInspector Event: Debugger.scriptParsed
function _onScriptParsed(event, res) {
// res = {scriptId, url, startLine, startColumn, endLine, endColumn, isContentScript, sourceMapURL}
_idToScript[res.scriptId] = res;
_urlToScript[res.url] = res;
}
// WebInspector Event: Debugger.scriptFailedToParse
function _onScriptFailedToParse(event, res) {
// res = {url, scriptSource, startLine, errorLine, errorMessage}
}
// WebInspector Event: Debugger.paused
function _onPaused(event, res) {
// res = {callFrames, reason, data}
switch (res.reason) {
// Exception
case "exception":
Inspector.Debugger.resume();
// var callFrame = res.callFrames[0];
// var script = scriptWithId(callFrame.location.scriptId);
break;
// DOMBreakpoint
case "DOM":
Inspector.Debugger.resume();
if (res.data.type === "subtree-modified" && res.data.insertion === true) {
_insertTrace = res.callFrames;
}
break;
}
}
function _reset() {
_urlToScript = {};
_idToScript = {};
}
Get the script information for a given url
function scriptWithId(url) {
return _idToScript[url];
}
// TODO: Strip off query/hash strings from URL (see CSSAgent._canonicalize())
Clean up
function unload() {
_reset();
Inspector.Page.off(".ScriptAgent");
DOMAgent.off(".ScriptAgent");
Inspector.Debugger.off(".ScriptAgent");
Inspector.DOM.off(".ScriptAgent");
}
// Export public functions
exports.scriptWithId = scriptWithId;
exports.scriptForURL = scriptForURL;
exports.load = load;
exports.unload = unload;
});