Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 "use strict"; | |
Wladimir Palant
2016/03/15 10:07:10
We put "use strict" after the license header.
sergei
2016/03/16 14:44:23
Done.
| |
2 /* | 1 /* |
3 * 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 |
4 * 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 |
5 * http://mozilla.org/MPL/2.0/. | 4 * http://mozilla.org/MPL/2.0/. |
6 */ | 5 */ |
7 | 6 |
7 "use strict"; | |
8 | |
9 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; | |
10 | |
8 /** | 11 /** |
9 * Sends exception to parent process script using "abpcrawler:reportException" | 12 * @param e exception |
10 * message with CPOW of the exception parameter. | |
11 * @param e exception to be sent. | |
12 */ | 13 */ |
13 function reportException(e) | 14 function reportException(e) |
14 { | 15 { |
15 sendSyncMessage("abpcrawler:reportException", undefined, e); | 16 let stack = ""; |
Wladimir Palant
2016/03/15 10:07:10
Don't send sync messages unless you absolutely can
sergei
2016/03/16 14:44:23
Acknowledged.
JIC, it was sync here because CPOW i
| |
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"); | |
16 } | 22 } |
17 | 23 |
18 const {classes: Cc, interfaces: Ci, utils: Cu} = Components; | 24 const {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); |
19 | |
20 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); | |
21 let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); | |
22 | 25 |
23 /** | 26 /** |
24 * Waits for finishing of the page loading, calls `gatherPageInfo` and sends | 27 * Progress listener capturing the data of the current page and calling |
25 * gahter information using "abpcrawler:pageInfoGathered" message. | 28 * onPageLoaded(data) when loading is finished, where data contains |
26 * https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interfa ce/nsIWebProgressListener | 29 * HTTP status and headers. |
30 * | |
31 * @type nsIWebProgressListener | |
27 */ | 32 */ |
28 let webProgressListener = | 33 let webProgressListener = |
29 { | 34 { |
30 onStateChange: function(webProgress, request, flags, status) | 35 onStateChange: function(webProgress, request, flags, status) |
31 { | 36 { |
32 if ((flags & Ci.nsIWebProgressListener.STATE_STOP) && (flags & Ci.nsIWebProg ressListener.STATE_IS_WINDOW)) | 37 if (webProgress.DOMWindow == content && |
38 (flags & Ci.nsIWebProgressListener.STATE_STOP)) | |
33 { | 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}; | |
34 if (request instanceof Ci.nsIHttpChannel) | 49 if (request instanceof Ci.nsIHttpChannel) |
35 { | 50 { |
36 let pageInfo = {headers: []}; | |
37 try | 51 try |
38 { | 52 { |
53 pageInfo.headers = []; | |
39 pageInfo.headers.push("HTTP/x.x " + request.responseStatus + " " + req uest.responseStatusText); | 54 pageInfo.headers.push("HTTP/x.x " + request.responseStatus + " " + req uest.responseStatusText); |
40 request.visitResponseHeaders((header, value) =>pageInfo.headers.push(h eader + ": " + value)); | 55 request.visitResponseHeaders((header, value) => pageInfo.headers.push( header + ": " + value)); |
41 } | 56 } |
42 catch (e) | 57 catch (e) |
43 { | 58 { |
44 reportException(e); | 59 reportException(e); |
45 } | 60 } |
46 Object.assign(pageInfo, gatherPageInfo(content)); | |
47 sendAsyncMessage("abpcrawler:pageInfoGathered", pageInfo); | |
48 } | 61 } |
62 onPageLoaded(pageInfo); | |
49 } | 63 } |
50 }, | 64 }, |
51 | 65 |
52 // definitions of the remaining functions see related documentation | 66 onLocationChange: function() {}, |
53 onLocationChange: function(webProgress, request, URI, flag) {}, | 67 onProgressChange: function() {}, |
54 onProgressChange: function(aWebProgress, aRequest, curSelf, maxSelf, curTot, m axTot) {}, | 68 onStatusChange: function() {}, |
55 onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage) {}, | 69 onSecurityChange: function() {}, |
56 onSecurityChange: function(aWebProgress, aRequest, aState) {}, | 70 |
57 | |
58 QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, Ci.nsISuppor tsWeakReference]) | 71 QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, Ci.nsISuppor tsWeakReference]) |
59 }; | 72 }; |
60 | 73 |
61 let filter = Cc["@mozilla.org/appshell/component/browser-status-filter;1"] | 74 function onPageLoaded(pageInfo) |
62 .createInstance(Ci.nsIWebProgress); | 75 { |
63 filter.addProgressListener(webProgressListener, Ci.nsIWebProgress.NOTIFY_ALL); | 76 Object.assign(pageInfo, gatherPageInfo(content)); |
77 sendAsyncMessage("abpcrawler:pageInfoGathered", pageInfo); | |
78 }; | |
64 | 79 |
65 let webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor) | 80 let webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor).getInterface (Ci.nsIWebProgress); |
66 .getInterface(Ci.nsIWebProgress); | 81 webProgress.addProgressListener(webProgressListener, Ci.nsIWebProgress.NOTIFY_ST ATE_WINDOW); |
67 webProgress.addProgressListener(filter, Ci.nsIWebProgress.NOTIFY_ALL); | |
68 | 82 |
69 /** | 83 /** |
70 * Gathers information about page using DOM window. | 84 * Gathers information about a DOM window. |
71 * Currently | 85 * Currently |
72 * - creates a screenshot of the page | 86 * - creates a screenshot of the page |
73 * - serializes the page source code | 87 * - serializes the page source code |
74 * @param {nsIDOMWindow} wnd window to process | 88 * @param {nsIDOMWindow} wnd window to process |
75 * @return {Object} the object containing "screenshot" and "source" properties. | 89 * @return {Object} the object containing "screenshot" and "source" properties. |
76 */ | 90 */ |
77 function gatherPageInfo(wnd) | 91 function gatherPageInfo(wnd) |
78 { | 92 { |
79 let document = wnd.document; | 93 let document = wnd.document; |
80 let result = {}; | 94 let result = {errors:[]}; |
81 if (document.documentElement) | 95 if (!document.documentElement) |
82 { | 96 { |
83 try | 97 result.errors.push("No document.documentElement"); |
84 { | 98 return result; |
85 let canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "can vas"); | |
86 canvas.width = document.documentElement.scrollWidth; | |
87 canvas.height = document.documentElement.scrollHeight; | |
88 if (canvas.width > 0 && canvas.height > 0) | |
89 { | |
90 let context = canvas.getContext("2d"); | |
91 context.drawWindow(wnd, 0, 0, canvas.width, canvas.height, "rgb(255, 255 , 255)"); | |
92 result.screenshot = canvas.toDataURL("image/jpeg", 0.8); | |
93 } | |
94 // TODO: Capture frames as well? | |
95 let serializer = new wnd.XMLSerializer(); | |
96 result.source = serializer.serializeToString(document.documentElement); | |
97 } | |
98 catch (e) | |
99 { | |
100 reportException(e); | |
101 result.error = "Cannot gather page info"; | |
102 } | |
103 } | 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 | |
104 return result; | 128 return result; |
105 } | 129 } |
LEFT | RIGHT |