Index: lib/child/frameScript.js |
diff --git a/lib/child/frameScript.js b/lib/child/frameScript.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5c173a2dfcd1ef0b8610586f7a667ca7ca02daf2 |
--- /dev/null |
+++ b/lib/child/frameScript.js |
@@ -0,0 +1,105 @@ |
+"use strict"; |
Wladimir Palant
2016/03/15 10:07:10
We put "use strict" after the license header.
sergei
2016/03/16 14:44:23
Done.
|
+/* |
+ * This Source Code is subject to the terms of the Mozilla Public License |
+ * version 2.0 (the "License"). You can obtain a copy of the License at |
+ * http://mozilla.org/MPL/2.0/. |
+ */ |
+ |
+/** |
+ * Sends exception to parent process script using "abpcrawler:reportException" |
+ * message with CPOW of the exception parameter. |
+ * @param e exception to be sent. |
+ */ |
+function reportException(e) |
+{ |
+ sendSyncMessage("abpcrawler:reportException", undefined, e); |
Wladimir Palant
2016/03/15 10:07:10
Don't send sync messages unless you absolutely can
sergei
2016/03/16 14:44:23
Acknowledged.
JIC, it was sync here because CPOW i
|
+} |
+ |
+const {classes: Cc, interfaces: Ci, utils: Cu} = Components; |
+ |
+let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
+let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); |
+ |
+/** |
+ * Waits for finishing of the page loading, calls `gatherPageInfo` and sends |
+ * gahter information using "abpcrawler:pageInfoGathered" message. |
+ * https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIWebProgressListener |
+ */ |
+let webProgressListener = |
+{ |
+ onStateChange: function(webProgress, request, flags, status) |
+ { |
+ if ((flags & Ci.nsIWebProgressListener.STATE_STOP) && (flags & Ci.nsIWebProgressListener.STATE_IS_WINDOW)) |
+ { |
+ if (request instanceof Ci.nsIHttpChannel) |
+ { |
+ let pageInfo = {headers: []}; |
+ try |
+ { |
+ pageInfo.headers.push("HTTP/x.x " + request.responseStatus + " " + request.responseStatusText); |
+ request.visitResponseHeaders((header, value) =>pageInfo.headers.push(header + ": " + value)); |
+ } |
+ catch (e) |
+ { |
+ reportException(e); |
+ } |
+ Object.assign(pageInfo, gatherPageInfo(content)); |
+ sendAsyncMessage("abpcrawler:pageInfoGathered", pageInfo); |
+ } |
+ } |
+ }, |
+ |
+ // definitions of the remaining functions see related documentation |
+ onLocationChange: function(webProgress, request, URI, flag) {}, |
+ onProgressChange: function(aWebProgress, aRequest, curSelf, maxSelf, curTot, maxTot) {}, |
+ onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage) {}, |
+ onSecurityChange: function(aWebProgress, aRequest, aState) {}, |
+ |
+ QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, Ci.nsISupportsWeakReference]) |
+}; |
+ |
+let filter = Cc["@mozilla.org/appshell/component/browser-status-filter;1"] |
+ .createInstance(Ci.nsIWebProgress); |
+filter.addProgressListener(webProgressListener, Ci.nsIWebProgress.NOTIFY_ALL); |
+ |
+let webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor) |
+ .getInterface(Ci.nsIWebProgress); |
+webProgress.addProgressListener(filter, Ci.nsIWebProgress.NOTIFY_ALL); |
+ |
+/** |
+ * Gathers information about page using DOM window. |
+ * Currently |
+ * - creates a screenshot of the page |
+ * - serializes the page source code |
+ * @param {nsIDOMWindow} wnd window to process |
+ * @return {Object} the object containing "screenshot" and "source" properties. |
+ */ |
+function gatherPageInfo(wnd) |
+{ |
+ let document = wnd.document; |
+ let result = {}; |
+ if (document.documentElement) |
+ { |
+ try |
+ { |
+ let canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "canvas"); |
+ canvas.width = document.documentElement.scrollWidth; |
+ canvas.height = document.documentElement.scrollHeight; |
+ if (canvas.width > 0 && canvas.height > 0) |
+ { |
+ let context = canvas.getContext("2d"); |
+ context.drawWindow(wnd, 0, 0, canvas.width, canvas.height, "rgb(255, 255, 255)"); |
+ result.screenshot = canvas.toDataURL("image/jpeg", 0.8); |
+ } |
+ // TODO: Capture frames as well? |
+ let serializer = new wnd.XMLSerializer(); |
+ result.source = serializer.serializeToString(document.documentElement); |
+ } |
+ catch (e) |
+ { |
+ reportException(e); |
+ result.error = "Cannot gather page info"; |
+ } |
+ } |
+ return result; |
+} |