Left: | ||
Right: |
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'; | |
Wladimir Palant
2016/09/29 11:44:58
Double quotation marks please.
sergei
2016/09/29 15:36:22
Done.
| |
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 const webProgressListener = | |
34 { | |
35 onStateChange: function(webProgress, request, flags, status) | |
36 { | |
37 if (webProgress.DOMWindow == content && | |
38 (flags & Ci.nsIWebProgressListener.STATE_STOP) && | |
39 (flags & Ci.nsIWebProgressListener.STATE_IS_WINDOW)) | |
Wladimir Palant
2016/09/29 11:44:59
You don't have to check STATE_IS_WINDOW any more.
sergei
2016/09/29 15:36:21
Done. I left it just in case.
| |
40 { | |
41 if (content.location.protocol == 'about:') | |
42 return; | |
Wladimir Palant
2016/09/29 11:44:58
This needs an explanation. Is that check necessary
sergei
2016/09/29 15:36:22
Answer is in https://codereview.adblockplus.org/29
| |
43 const pageInfo = {channelStatus: status}; | |
Wladimir Palant
2016/09/29 11:44:58
Please don't declare regular variables as constant
sergei
2016/09/29 15:36:21
const in javascript does not mean immutable, it's
Wladimir Palant
2016/09/30 07:43:12
Yes, that's how const is normally used - to indica
| |
44 if (request instanceof Ci.nsIHttpChannel) | |
45 { | |
46 try | |
47 { | |
48 pageInfo.headers = []; | |
49 pageInfo.headers.push("HTTP/x.x " + request.responseStatus + " " + req uest.responseStatusText); | |
50 request.visitResponseHeaders((header, value) => pageInfo.headers.push( header + ": " + value)); | |
51 } | |
52 catch (e) | |
53 { | |
54 reportException(e); | |
55 } | |
56 } | |
57 onPageLoaded(pageInfo); | |
58 } | |
59 }, | |
60 | |
61 onLocationChange: function() {}, | |
62 onProgressChange: function() {}, | |
63 onStatusChange: function() {}, | |
64 onSecurityChange: function() {}, | |
65 | |
66 QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, Ci.nsISuppor tsWeakReference]) | |
67 }; | |
68 | |
69 function onPageLoaded(pageInfo) | |
70 { | |
71 Object.assign(pageInfo, gatherPageInfo(content)); | |
72 sendAsyncMessage("abpcrawler:pageInfoGathered", pageInfo); | |
73 }; | |
74 | |
75 const webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor).getInterfa ce(Ci.nsIWebProgress); | |
Wladimir Palant
2016/09/29 11:44:58
As above, please don't mark any variable that you
sergei
2016/09/29 15:36:21
Done.
| |
76 webProgress.addProgressListener(webProgressListener, Ci.nsIWebProgress.NOTIFY_ST ATE_WINDOW); | |
77 | |
78 /** | |
79 * Gathers information about a DOM window. | |
80 * Currently | |
81 * - creates a screenshot of the page | |
82 * - serializes the page source code | |
83 * @param {nsIDOMWindow} wnd window to process | |
84 * @return {Object} the object containing "screenshot" and "source" properties. | |
85 */ | |
86 function gatherPageInfo(wnd) | |
87 { | |
88 const document = wnd.document; | |
89 const result = {errors:[]}; | |
90 if (!document.documentElement) | |
91 { | |
92 result.errors.push('No document.documentElement'); | |
Wladimir Palant
2016/09/29 11:44:58
Double quotation marks please.
sergei
2016/09/29 15:36:21
Done.
| |
93 return result; | |
94 } | |
95 | |
96 try | |
97 { | |
98 const canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "can vas"); | |
99 canvas.width = document.documentElement.scrollWidth; | |
100 canvas.height = document.documentElement.scrollHeight; | |
101 const context = canvas.getContext("2d"); | |
102 context.drawWindow(wnd, 0, 0, canvas.width, canvas.height, "rgb(255, 255, 25 5)"); | |
103 result.screenshot = canvas.toDataURL("image/jpeg", 0.8); | |
104 } | |
105 catch (e) | |
106 { | |
107 reportException(e); | |
108 result.errors.push("Cannot make page screenshot"); | |
109 } | |
110 | |
111 try | |
112 { | |
113 // TODO: Capture frames as well? | |
114 const serializer = new wnd.XMLSerializer(); | |
115 result.source = serializer.serializeToString(document.documentElement); | |
116 } | |
117 catch(e) | |
118 { | |
119 reportException(e); | |
120 result.errors.push("Cannot obtain page source code"); | |
121 } | |
122 | |
123 return result; | |
124 } | |
OLD | NEW |