LEFT | RIGHT |
1 /* | 1 /* |
2 * This Source Code is subject to the terms of the Mozilla Public License | 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 | 3 * version 2.0 (the "License"). You can obtain a copy of the License at |
4 * http://mozilla.org/MPL/2.0/. | 4 * http://mozilla.org/MPL/2.0/. |
5 */ | 5 */ |
| 6 |
6 "use strict"; | 7 "use strict"; |
7 | 8 |
8 const {classes: Cc, interfaces: Ci, utils: Cu} = Components; | 9 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; |
9 | 10 |
10 /** | 11 /** |
11 * @param e exception | 12 * @param e exception |
12 */ | 13 */ |
13 function reportException(e) | 14 function reportException(e) |
14 { | 15 { |
15 let stack = ""; | 16 let stack = ""; |
16 if (e && typeof e == "object" && "stack" in e) | 17 if (e && typeof e == "object" && "stack" in e) |
17 stack = e.stack + "\n"; | 18 stack = e.stack + "\n"; |
18 | 19 |
19 Cu.reportError(e); | 20 Cu.reportError(e); |
20 dump(e + "\n" + stack + "\n"); | 21 dump(e + "\n" + stack + "\n"); |
21 } | 22 } |
22 | 23 |
23 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); | 24 const {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); |
24 let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); | |
25 | 25 |
26 /** | 26 /** |
27 * Waits for finishing of the page loading, calls `gatherPageInfo` and sends | 27 * Progress listener capturing the data of the current page and calling |
28 * gahter information using "abpcrawler:pageInfoGathered" message. | 28 * onPageLoaded(data) when loading is finished, where data contains |
29 * https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interfa
ce/nsIWebProgressListener | 29 * HTTP status and headers. |
| 30 * |
| 31 * @type nsIWebProgressListener |
30 */ | 32 */ |
31 let webProgressListener = | 33 let webProgressListener = |
32 { | 34 { |
33 onStateChange: function(webProgress, request, flags, status) | 35 onStateChange: function(webProgress, request, flags, status) |
34 { | 36 { |
35 if ((flags & Ci.nsIWebProgressListener.STATE_STOP) && (flags & Ci.nsIWebProg
ressListener.STATE_IS_WINDOW)) | 37 if (webProgress.DOMWindow == content && |
| 38 (flags & Ci.nsIWebProgressListener.STATE_STOP)) |
36 { | 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 case we are not interested in is about:newtab. |
| 46 if (content.location.protocol == "about:") |
| 47 return; |
| 48 let pageInfo = {channelStatus: status}; |
37 if (request instanceof Ci.nsIHttpChannel) | 49 if (request instanceof Ci.nsIHttpChannel) |
38 { | 50 { |
39 let pageInfo = {headers: []}; | |
40 try | 51 try |
41 { | 52 { |
| 53 pageInfo.headers = []; |
42 pageInfo.headers.push("HTTP/x.x " + request.responseStatus + " " + req
uest.responseStatusText); | 54 pageInfo.headers.push("HTTP/x.x " + request.responseStatus + " " + req
uest.responseStatusText); |
43 request.visitResponseHeaders((header, value) =>pageInfo.headers.push(h
eader + ": " + value)); | 55 request.visitResponseHeaders((header, value) => pageInfo.headers.push(
header + ": " + value)); |
44 } | 56 } |
45 catch (e) | 57 catch (e) |
46 { | 58 { |
47 reportException(e); | 59 reportException(e); |
48 } | 60 } |
49 Object.assign(pageInfo, gatherPageInfo(content)); | |
50 sendAsyncMessage("abpcrawler:pageInfoGathered", pageInfo); | |
51 } | 61 } |
| 62 onPageLoaded(pageInfo); |
52 } | 63 } |
53 }, | 64 }, |
54 | 65 |
55 // definitions of the remaining functions see related documentation | 66 onLocationChange: function() {}, |
56 onLocationChange: function(webProgress, request, URI, flag) {}, | 67 onProgressChange: function() {}, |
57 onProgressChange: function(aWebProgress, aRequest, curSelf, maxSelf, curTot, m
axTot) {}, | 68 onStatusChange: function() {}, |
58 onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage) {}, | 69 onSecurityChange: function() {}, |
59 onSecurityChange: function(aWebProgress, aRequest, aState) {}, | 70 |
60 | |
61 QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, Ci.nsISuppor
tsWeakReference]) | 71 QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, Ci.nsISuppor
tsWeakReference]) |
62 }; | 72 }; |
63 | 73 |
64 let filter = Cc["@mozilla.org/appshell/component/browser-status-filter;1"] | 74 function onPageLoaded(pageInfo) |
65 .createInstance(Ci.nsIWebProgress); | 75 { |
66 filter.addProgressListener(webProgressListener, Ci.nsIWebProgress.NOTIFY_ALL); | 76 Object.assign(pageInfo, gatherPageInfo(content)); |
| 77 sendAsyncMessage("abpcrawler:pageInfoGathered", pageInfo); |
| 78 }; |
67 | 79 |
68 let webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor) | 80 let webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor).getInterface
(Ci.nsIWebProgress); |
69 .getInterface(Ci.nsIWebProgress); | 81 webProgress.addProgressListener(webProgressListener, Ci.nsIWebProgress.NOTIFY_ST
ATE_WINDOW); |
70 webProgress.addProgressListener(filter, Ci.nsIWebProgress.NOTIFY_ALL); | |
71 | 82 |
72 /** | 83 /** |
73 * Gathers information about page using DOM window. | 84 * Gathers information about a DOM window. |
74 * Currently | 85 * Currently |
75 * - creates a screenshot of the page | 86 * - creates a screenshot of the page |
76 * - serializes the page source code | 87 * - serializes the page source code |
77 * @param {nsIDOMWindow} wnd window to process | 88 * @param {nsIDOMWindow} wnd window to process |
78 * @return {Object} the object containing "screenshot" and "source" properties. | 89 * @return {Object} the object containing "screenshot" and "source" properties. |
79 */ | 90 */ |
80 function gatherPageInfo(wnd) | 91 function gatherPageInfo(wnd) |
81 { | 92 { |
82 let document = wnd.document; | 93 let document = wnd.document; |
83 let result = {}; | 94 let result = {errors:[]}; |
84 if (document.documentElement) | 95 if (!document.documentElement) |
85 { | 96 { |
86 try | 97 result.errors.push("No document.documentElement"); |
87 { | 98 return result; |
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 } | 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 |
107 return result; | 128 return result; |
108 } | 129 } |
LEFT | RIGHT |