Index: lib/child/frameScript.js |
diff --git a/lib/child/frameScript.js b/lib/child/frameScript.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bc90eeb1bbd1380cff52cf6ffdd9ea0314661c12 |
--- /dev/null |
+++ b/lib/child/frameScript.js |
@@ -0,0 +1,132 @@ |
+/* |
+ * 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/. |
+ */ |
+"use strict"; |
+ |
+const {classes: Cc, interfaces: Ci, utils: Cu, Cr: results} = Components; |
+ |
+/** |
+ * @param e exception |
+ */ |
+function reportException(e) |
+{ |
+ let stack = ""; |
+ if (e && typeof e == "object" && "stack" in e) |
+ stack = e.stack + "\n"; |
+ |
+ Cu.reportError(e); |
+ dump(e + "\n" + stack + "\n"); |
+} |
+ |
+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) |
+ { |
+ // use isTopLevel to filter beacon requests out |
+ if (webProgress.isTopLevel && |
+ (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) |
+ { |
+ // Ignore if called before the response has been received (before |
+ // onStartRequest()). |
+ if (e.result == Cr.NS_ERROR_NOT_AVAILABLE) |
+ return; |
+ 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 = {errors:[]}; |
+ if (document.documentElement) |
+ { |
+ try |
+ { |
+ let canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "canvas"); |
+ // Firefox does not work with large canvas elements. |
+ // http://stackoverflow.com/questions/6081483/maximum-size-of-a-canvas-element |
+ canvas.width = document.documentElement.scrollWidth; |
+ canvas.height = document.documentElement.scrollHeight; |
+ if (canvas.width > 0 && canvas.height > 0) |
+ { |
+ if (canvas.width > 32767) |
+ { |
+ result.errors.push("Width exceeds supported limit, " + canvas.width); |
+ canvas.width = 32767; |
+ } |
+ if (canvas.height > 32767) |
+ { |
+ result.errors.push("Height exceeds supported limit, " + canvas.height); |
+ canvas.height = 32767; |
+ } |
+ if (canvas.width * canvas.height > 472907776) |
+ { |
+ result.errrors.push("Area exceeds supported limit, " + canvas.height + " * " + canvas.width); |
+ canvas.height = 472907776 / canvas.width; |
+ } |
+ 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.errors.push("Cannot gather page info"); |
+ } |
+ } |
+ return result; |
+} |