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} = 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(h
eader + ": " + value)); |
| 47 } |
| 48 catch (e) |
| 49 { |
| 50 reportException(e); |
| 51 } |
| 52 Object.assign(pageInfo, gatherPageInfo(content)); |
| 53 sendAsyncMessage("abpcrawler:pageInfoGathered", pageInfo); |
| 54 } |
| 55 } |
| 56 }, |
| 57 |
| 58 // definitions of the remaining functions see related documentation |
| 59 onLocationChange: function(webProgress, request, URI, flag) {}, |
| 60 onProgressChange: function(aWebProgress, aRequest, curSelf, maxSelf, curTot, m
axTot) {}, |
| 61 onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage) {}, |
| 62 onSecurityChange: function(aWebProgress, aRequest, aState) {}, |
| 63 |
| 64 QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, Ci.nsISuppor
tsWeakReference]) |
| 65 }; |
| 66 |
| 67 let filter = Cc["@mozilla.org/appshell/component/browser-status-filter;1"] |
| 68 .createInstance(Ci.nsIWebProgress); |
| 69 filter.addProgressListener(webProgressListener, Ci.nsIWebProgress.NOTIFY_ALL); |
| 70 |
| 71 let webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor) |
| 72 .getInterface(Ci.nsIWebProgress); |
| 73 webProgress.addProgressListener(filter, Ci.nsIWebProgress.NOTIFY_ALL); |
| 74 |
| 75 /** |
| 76 * Gathers information about page using DOM window. |
| 77 * Currently |
| 78 * - creates a screenshot of the page |
| 79 * - serializes the page source code |
| 80 * @param {nsIDOMWindow} wnd window to process |
| 81 * @return {Object} the object containing "screenshot" and "source" properties. |
| 82 */ |
| 83 function gatherPageInfo(wnd) |
| 84 { |
| 85 let document = wnd.document; |
| 86 let result = {}; |
| 87 if (document.documentElement) |
| 88 { |
| 89 try |
| 90 { |
| 91 let canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "can
vas"); |
| 92 canvas.width = document.documentElement.scrollWidth; |
| 93 canvas.height = document.documentElement.scrollHeight; |
| 94 if (canvas.width > 0 && canvas.height > 0) |
| 95 { |
| 96 let context = canvas.getContext("2d"); |
| 97 context.drawWindow(wnd, 0, 0, canvas.width, canvas.height, "rgb(255, 255
, 255)"); |
| 98 result.screenshot = canvas.toDataURL("image/jpeg", 0.8); |
| 99 } |
| 100 // TODO: Capture frames as well? |
| 101 let serializer = new wnd.XMLSerializer(); |
| 102 result.source = serializer.serializeToString(document.documentElement); |
| 103 } |
| 104 catch (e) |
| 105 { |
| 106 reportException(e); |
| 107 result.error = "Cannot gather page info"; |
| 108 } |
| 109 } |
| 110 return result; |
| 111 } |
OLD | NEW |