Index: sky/framework/inspector/page-agent.sky |
diff --git a/sky/framework/inspector/page-agent.sky b/sky/framework/inspector/page-agent.sky |
index 875d5719301ef3179c79eb802451230b75aa9904..4e5a2df06ad98bd315897e5c12102a3509f94ec3 100644 |
--- a/sky/framework/inspector/page-agent.sky |
+++ b/sky/framework/inspector/page-agent.sky |
@@ -33,19 +33,85 @@ Page.prototype.getResourceContent = function(params, message_id) { |
return this.delegate_.ASYNC_RESPONSE; |
}; |
+Page.prototype._treeOrderWalk = function(root, visit) { |
+ // HTML defines tree-order as a pre-order depth-first walk. |
+ var node = root; |
+ var parent_stack = []; |
+ // WTF? Why is !![] == true in this forsaken language? |
abarth-chromium
2014/11/18 23:08:42
Not sure we want this comment...
|
+ while (parent_stack.length || node) { |
+ if (node) { |
+ visit(node); |
+ if (node.nextSibling) |
+ parent_stack.push(node.nextSibling); |
+ node = node.firstChild; |
+ } else |
+ node = parent_stack.pop(); |
+ } |
+} |
+ |
+Page.prototype._getElementsByTagName = function(root, tag_name) { |
+ var elements = []; |
+ this._treeOrderWalk(root, function(node) { |
+ if (node.tagName == tag_name) |
+ elements.push(node); |
+ }); |
+ return elements; |
+} |
+ |
+// Javascript is possibly the worst language known to man. |
+// ES6 has Array.from which theoretically does this. |
+Page.prototype._arrayFromIterator = function(iterator) { |
+ var a = []; |
+ for (var e of iterator) |
+ a.push(e); |
+ return a; |
+} |
+ |
+Page.prototype._resourcesMapForSubtree = function(root_doc) { |
+ var resource_map = new Map; |
+ function _addResource(url, type, mimeType) { |
+ var absolute_url = new URL(url, document.URL); |
+ if (resource_map.has(absolute_url)) |
+ return; |
+ resource_map.set(absolute_url, { |
+ 'url': String(absolute_url), |
+ 'type': type, |
+ 'mimeType': mimeType, |
+ }) |
+ } |
+ |
+ var pending_docs = [root_doc]; |
+ while (pending_docs.length) { |
+ var doc = pending_docs.pop(); |
+ var imports = this._getElementsByTagName(doc, 'import'); |
+ imports.forEach(function(imp) { |
+ // FIXME: It's no longer possible to walk into an import. :( |
+ // pending_docs.push(imp.import); |
+ _addResource(imp.getAttribute('src'), 'Document', 'text/html'); |
+ }); |
+ var images = this._getElementsByTagName(doc, 'img'); |
+ images.forEach(function(img) { |
+ // FIXME: Can't determine the type of an image from the DOM. |
+ _addResource(img.getAttribute('src'), 'Image', 'image/unknown'); |
+ }); |
+ } |
+ return resource_map; |
+} |
+ |
Page.prototype.getResourceTree = function() { |
// Unclear if this is all needed, but if we don't return something here |
// the inspector hits an exception in WebInspector.ResourceTreeModel. |
+ var resources_iter = this._resourcesMapForSubtree(document).values(); |
return { |
"frameTree": { |
"frame": { |
"id": "1", |
"loaderId": "1", |
- "url": document.URL, |
+ "url": String(document.URL), |
"mimeType": "text/html", |
- "securityOrigin": document.URL, |
+ "securityOrigin": String(document.URL), |
}, |
- "resources": [], // FIXME |
+ "resources": this._arrayFromIterator(resources_iter), |
} |
}; |
}; |