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