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 |
| 7 "use strict"; |
| 8 |
| 9 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; |
| 10 |
| 11 /** |
| 12 * @param e exception |
| 13 */ |
| 14 function reportException(e) |
| 15 { |
| 16 let stack = ""; |
| 17 if (e && typeof e == "object" && "stack" in e) |
| 18 stack = e.stack + "\n"; |
| 19 |
| 20 Cu.reportError(e); |
| 21 dump(e + "\n" + stack + "\n"); |
| 22 } |
| 23 |
| 24 const {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); |
| 25 |
| 26 /** |
| 27 * Progress listener capturing the data of the current page and calling |
| 28 * onPageLoaded(data) when loading is finished, where data contains |
| 29 * HTTP status and headers. |
| 30 * |
| 31 * @type nsIWebProgressListener |
| 32 */ |
| 33 let webProgressListener = |
| 34 { |
| 35 onStateChange: function(webProgress, request, flags, status) |
| 36 { |
| 37 if (webProgress.DOMWindow == content && |
| 38 (flags & Ci.nsIWebProgressListener.STATE_STOP)) |
| 39 { |
| 40 // First time we receive STATE_STOP for about:blank and the second time |
| 41 // for our interested URL which is distinct from about:blank. |
| 42 // However we should not process about:blank because it can happen that |
| 43 // the message with information about about:blank is delivered when the |
| 44 // code in crawler.js is already waiting for a message from this tab. |
| 45 // Another not interesting for us case is about:newtab. |
| 46 if (content.location.protocol == "about:") |
| 47 return; |
| 48 let pageInfo = {channelStatus: status}; |
| 49 if (request instanceof Ci.nsIHttpChannel) |
| 50 { |
| 51 try |
| 52 { |
| 53 pageInfo.headers = []; |
| 54 pageInfo.headers.push("HTTP/x.x " + request.responseStatus + " " + req
uest.responseStatusText); |
| 55 request.visitResponseHeaders((header, value) => pageInfo.headers.push(
header + ": " + value)); |
| 56 } |
| 57 catch (e) |
| 58 { |
| 59 reportException(e); |
| 60 } |
| 61 } |
| 62 onPageLoaded(pageInfo); |
| 63 } |
| 64 }, |
| 65 |
| 66 onLocationChange: function() {}, |
| 67 onProgressChange: function() {}, |
| 68 onStatusChange: function() {}, |
| 69 onSecurityChange: function() {}, |
| 70 |
| 71 QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, Ci.nsISuppor
tsWeakReference]) |
| 72 }; |
| 73 |
| 74 function onPageLoaded(pageInfo) |
| 75 { |
| 76 Object.assign(pageInfo, gatherPageInfo(content)); |
| 77 sendAsyncMessage("abpcrawler:pageInfoGathered", pageInfo); |
| 78 }; |
| 79 |
| 80 let webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor).getInterface
(Ci.nsIWebProgress); |
| 81 webProgress.addProgressListener(webProgressListener, Ci.nsIWebProgress.NOTIFY_ST
ATE_WINDOW); |
| 82 |
| 83 /** |
| 84 * Gathers information about a DOM window. |
| 85 * Currently |
| 86 * - creates a screenshot of the page |
| 87 * - serializes the page source code |
| 88 * @param {nsIDOMWindow} wnd window to process |
| 89 * @return {Object} the object containing "screenshot" and "source" properties. |
| 90 */ |
| 91 function gatherPageInfo(wnd) |
| 92 { |
| 93 let document = wnd.document; |
| 94 let result = {errors:[]}; |
| 95 if (!document.documentElement) |
| 96 { |
| 97 result.errors.push("No document.documentElement"); |
| 98 return result; |
| 99 } |
| 100 |
| 101 try |
| 102 { |
| 103 let canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "canva
s"); |
| 104 canvas.width = document.documentElement.scrollWidth; |
| 105 canvas.height = document.documentElement.scrollHeight; |
| 106 let context = canvas.getContext("2d"); |
| 107 context.drawWindow(wnd, 0, 0, canvas.width, canvas.height, "rgb(255, 255, 25
5)"); |
| 108 result.screenshot = canvas.toDataURL("image/jpeg", 0.8); |
| 109 } |
| 110 catch (e) |
| 111 { |
| 112 reportException(e); |
| 113 result.errors.push("Cannot make page screenshot"); |
| 114 } |
| 115 |
| 116 try |
| 117 { |
| 118 // TODO: Capture frames as well? |
| 119 let serializer = new wnd.XMLSerializer(); |
| 120 result.source = serializer.serializeToString(document.documentElement); |
| 121 } |
| 122 catch(e) |
| 123 { |
| 124 reportException(e); |
| 125 result.errors.push("Cannot obtain page source code"); |
| 126 } |
| 127 |
| 128 return result; |
| 129 } |
OLD | NEW |