OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This Source Code is subject to the terms of the Mozilla Public License |
| 3 * version 2.0 (the "License"). You can obtain a copy of the License at |
| 4 * http://mozilla.org/MPL/2.0/. |
| 5 */ |
| 6 "use strict"; |
| 7 |
| 8 const {classes: Cc, interfaces: Ci, utils: Cu, Cr: results} = Components; |
| 9 |
| 10 /** |
| 11 * @param e exception |
| 12 */ |
| 13 function reportException(e) |
| 14 { |
| 15 let stack = ""; |
| 16 if (e && typeof e == "object" && "stack" in e) |
| 17 stack = e.stack + "\n"; |
| 18 |
| 19 Cu.reportError(e); |
| 20 dump(e + "\n" + stack + "\n"); |
| 21 } |
| 22 |
| 23 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
| 24 let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); |
| 25 |
| 26 /** |
| 27 * Waits for finishing of the page loading, calls `gatherPageInfo` and sends |
| 28 * gahter information using "abpcrawler:pageInfoGathered" message. |
| 29 * https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interfa
ce/nsIWebProgressListener |
| 30 */ |
| 31 let webProgressListener = |
| 32 { |
| 33 onStateChange: function(webProgress, request, flags, status) |
| 34 { |
| 35 // use isTopLevel to filter beacon requests out |
| 36 if (webProgress.isTopLevel && |
| 37 (flags & Ci.nsIWebProgressListener.STATE_STOP) && |
| 38 (flags & Ci.nsIWebProgressListener.STATE_IS_WINDOW)) |
| 39 { |
| 40 if (request instanceof Ci.nsIHttpChannel) |
| 41 { |
| 42 let pageInfo = {headers: []}; |
| 43 try |
| 44 { |
| 45 pageInfo.headers.push("HTTP/x.x " + request.responseStatus + " " + req
uest.responseStatusText); |
| 46 request.visitResponseHeaders((header, value) => pageInfo.headers.push(
header + ": " + value)); |
| 47 } |
| 48 catch (e) |
| 49 { |
| 50 // Ignore if called before the response has been received (before |
| 51 // onStartRequest()). |
| 52 if (e.result == Cr.NS_ERROR_NOT_AVAILABLE) |
| 53 return; |
| 54 reportException(e); |
| 55 } |
| 56 Object.assign(pageInfo, gatherPageInfo(content)); |
| 57 sendAsyncMessage("abpcrawler:pageInfoGathered", pageInfo); |
| 58 } |
| 59 } |
| 60 }, |
| 61 |
| 62 // definitions of the remaining functions see related documentation |
| 63 onLocationChange: function(webProgress, request, URI, flag) {}, |
| 64 onProgressChange: function(aWebProgress, aRequest, curSelf, maxSelf, curTot, m
axTot) {}, |
| 65 onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage) {}, |
| 66 onSecurityChange: function(aWebProgress, aRequest, aState) {}, |
| 67 |
| 68 QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, Ci.nsISuppor
tsWeakReference]) |
| 69 }; |
| 70 |
| 71 let filter = Cc["@mozilla.org/appshell/component/browser-status-filter;1"] |
| 72 .createInstance(Ci.nsIWebProgress); |
| 73 filter.addProgressListener(webProgressListener, Ci.nsIWebProgress.NOTIFY_ALL); |
| 74 |
| 75 let webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor) |
| 76 .getInterface(Ci.nsIWebProgress); |
| 77 webProgress.addProgressListener(filter, Ci.nsIWebProgress.NOTIFY_ALL); |
| 78 |
| 79 /** |
| 80 * Gathers information about page using DOM window. |
| 81 * Currently |
| 82 * - creates a screenshot of the page |
| 83 * - serializes the page source code |
| 84 * @param {nsIDOMWindow} wnd window to process |
| 85 * @return {Object} the object containing "screenshot" and "source" properties. |
| 86 */ |
| 87 function gatherPageInfo(wnd) |
| 88 { |
| 89 let document = wnd.document; |
| 90 let result = {errors:[]}; |
| 91 if (document.documentElement) |
| 92 { |
| 93 try |
| 94 { |
| 95 let canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "can
vas"); |
| 96 // Firefox does not work with large canvas elements. |
| 97 // http://stackoverflow.com/questions/6081483/maximum-size-of-a-canvas-ele
ment |
| 98 canvas.width = document.documentElement.scrollWidth; |
| 99 canvas.height = document.documentElement.scrollHeight; |
| 100 if (canvas.width > 0 && canvas.height > 0) |
| 101 { |
| 102 if (canvas.width > 32767) |
| 103 { |
| 104 result.errors.push("Width exceeds supported limit, " + canvas.width); |
| 105 canvas.width = 32767; |
| 106 } |
| 107 if (canvas.height > 32767) |
| 108 { |
| 109 result.errors.push("Height exceeds supported limit, " + canvas.height)
; |
| 110 canvas.height = 32767; |
| 111 } |
| 112 if (canvas.width * canvas.height > 472907776) |
| 113 { |
| 114 result.errrors.push("Area exceeds supported limit, " + canvas.height +
" * " + canvas.width); |
| 115 canvas.height = 472907776 / canvas.width; |
| 116 } |
| 117 let context = canvas.getContext("2d"); |
| 118 context.drawWindow(wnd, 0, 0, canvas.width, canvas.height, "rgb(255, 255
, 255)"); |
| 119 result.screenshot = canvas.toDataURL("image/jpeg", 0.8); |
| 120 } |
| 121 // TODO: Capture frames as well? |
| 122 let serializer = new wnd.XMLSerializer(); |
| 123 result.source = serializer.serializeToString(document.documentElement); |
| 124 } |
| 125 catch (e) |
| 126 { |
| 127 reportException(e); |
| 128 result.errors.push("Cannot gather page info"); |
| 129 } |
| 130 } |
| 131 return result; |
| 132 } |
OLD | NEW |