Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
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 /* | |
3 * 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 | |
5 * http://mozilla.org/MPL/2.0/. | |
6 */ | |
7 | |
8 /** | |
9 * Sends exception to parent process script using "abpcrawler:reportException" | |
10 * message with CPOW of the exception parameter. | |
11 * @param e exception to be sent. | |
12 */ | |
13 function reportException(e) | |
14 { | |
15 sendSyncMessage("abpcrawler:reportException", undefined, e); | |
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
| |
16 } | |
17 | |
18 const {classes: Cc, interfaces: Ci, utils: Cu} = Components; | |
19 | |
20 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); | |
21 let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); | |
22 | |
23 /** | |
24 * Waits for finishing of the page loading, calls `gatherPageInfo` and sends | |
25 * gahter information using "abpcrawler:pageInfoGathered" message. | |
26 * https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interfa ce/nsIWebProgressListener | |
27 */ | |
28 let webProgressListener = | |
29 { | |
30 onStateChange: function(webProgress, request, flags, status) | |
31 { | |
32 if ((flags & Ci.nsIWebProgressListener.STATE_STOP) && (flags & Ci.nsIWebProg ressListener.STATE_IS_WINDOW)) | |
33 { | |
34 if (request instanceof Ci.nsIHttpChannel) | |
35 { | |
36 let pageInfo = {headers: []}; | |
37 try | |
38 { | |
39 pageInfo.headers.push("HTTP/x.x " + request.responseStatus + " " + req uest.responseStatusText); | |
40 request.visitResponseHeaders((header, value) =>pageInfo.headers.push(h eader + ": " + value)); | |
41 } | |
42 catch (e) | |
43 { | |
44 reportException(e); | |
45 } | |
46 Object.assign(pageInfo, gatherPageInfo(content)); | |
47 sendAsyncMessage("abpcrawler:pageInfoGathered", pageInfo); | |
48 } | |
49 } | |
50 }, | |
51 | |
52 // definitions of the remaining functions see related documentation | |
53 onLocationChange: function(webProgress, request, URI, flag) {}, | |
54 onProgressChange: function(aWebProgress, aRequest, curSelf, maxSelf, curTot, m axTot) {}, | |
55 onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage) {}, | |
56 onSecurityChange: function(aWebProgress, aRequest, aState) {}, | |
57 | |
58 QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, Ci.nsISuppor tsWeakReference]) | |
59 }; | |
60 | |
61 let filter = Cc["@mozilla.org/appshell/component/browser-status-filter;1"] | |
62 .createInstance(Ci.nsIWebProgress); | |
63 filter.addProgressListener(webProgressListener, Ci.nsIWebProgress.NOTIFY_ALL); | |
64 | |
65 let webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor) | |
66 .getInterface(Ci.nsIWebProgress); | |
67 webProgress.addProgressListener(filter, Ci.nsIWebProgress.NOTIFY_ALL); | |
68 | |
69 /** | |
70 * Gathers information about page using DOM window. | |
71 * Currently | |
72 * - creates a screenshot of the page | |
73 * - serializes the page source code | |
74 * @param {nsIDOMWindow} wnd window to process | |
75 * @return {Object} the object containing "screenshot" and "source" properties. | |
76 */ | |
77 function gatherPageInfo(wnd) | |
78 { | |
79 let document = wnd.document; | |
80 let result = {}; | |
81 if (document.documentElement) | |
82 { | |
83 try | |
84 { | |
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 } | |
104 return result; | |
105 } | |
OLD | NEW |