Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/devtools.js

Issue 6393086494113792: Issue 154 - Added devtools panel showing blocked and blockable items (Closed)
Patch Set: Rebased and fixed various issues Created March 12, 2015, 3:32 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include.preload.js ('k') | metadata.chrome » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 let info = require("info");
19 if (info.platform == "chromium")
20 {
21 let {WhitelistFilter, ElemHideFilter} = require("filterClasses");
22 let {SpecialSubscription} = require("subscriptionClasses");
23 let {FilterStorage} = require("filterStorage");
24 let {defaultMatcher} = require("matcher");
25 let {FilterNotifier} = require("filterNotifier");
26
27 // Mapping of inspected tabs to their devpanel page
28 // and recorded items. We can't use a PageMap here,
29 // because data must persist after navigation/reload.
30 let panels = Object.create(null);
31
32 function hasPanels()
33 {
34 return Object.keys(panels).length > 0;
35 }
36
37 function getRequestInfo(request)
38 {
39 return {
40 url: request.url,
41 type: request.type,
42 docDomain: request.docDomain
43 };
44 }
45
46 function getFilterInfo(filter)
47 {
48 if (!filter)
49 return null;
50
51 let userDefined = false;
52 let subscriptionTitle = null;
53
54 for (let subscription of filter.subscriptions)
55 {
56 if (!subscription.disabled)
57 {
58 if (subscription instanceof SpecialSubscription)
59 userDefined = true;
60 else
61 subscriptionTitle = subscription.title;
62 }
63 }
64
65 return {
66 text: filter.text,
67 whitelisted: filter instanceof WhitelistFilter,
68 userDefined: userDefined,
69 subscription: subscriptionTitle
70 };
71 }
72
73 function hasRecord(panel, request, filter)
74 {
75 return panel.records.some(record =>
76 record.request.type == request.type &&
77 record.request.url == request.url &&
78 record.request.docDomain == request.docDomain &&
79
80 // Matched element hiding filters don't relate to a particular request,
81 // so we also have to match the CSS selector in order to distinguish them.
82 (request.type != "ELEMHIDE" || record.filter.selector == filter.selector)
83 );
84 }
85
86 function addRecord(panel, request, filter)
87 {
88 if (!hasRecord(panel, request, filter))
89 {
90 panel.panel.sendMessage({
91 type: "add-record",
92 request: getRequestInfo(request),
93 filter: getFilterInfo(filter)
94 });
95
96 panel.records.push({
97 request: request,
98 filter: filter
99 });
100 }
101 }
102
103 function matchRequest(request)
104 {
105 return defaultMatcher.matchesAny(
106 request.url,
107 request.type,
108 request.docDomain,
109 request.thirdParty,
110 request.sitekey
111 );
112 }
113
114 /**
115 * Logs a request in the devtools panel.
116 *
117 * @param {Page} page The page the request occured on
118 * @param {string] url The URL of the request
119 * @param {string} type The request type
120 * @param {string} docDomain The IDN-decoded hostname of the document
121 * @param {Boolean} thirdParty Whether the origin of the request and document differs
122 * @param {string} [sitekey] The active sitekey if there is any
123 * @param {filter} [filter] The matched filter or null if there is no matc h
124 */
125 function logRequest(page, url, type, docDomain, thirdParty, sitekey, filter)
126 {
127 let panel = panels[page._id];
128 if (panel && !panel.reload && !panel.reloading)
129 {
130 let request = {
131 url: url,
132 type: type,
133 docDomain: docDomain,
134 thirdParty: thirdParty,
135 sitekey: sitekey
136 };
137
138 addRecord(panel, request, filter);
139 }
140 }
141 exports.logRequest = logRequest;
142
143 /**
144 * Logs active element hiding filters in the devtools panel.
145 *
146 * @param {Page} page The page the elements were hidden on
147 * @param {string[]} selectors The CSS selectors of active elemhide filters
148 * @param {string} docDomain The IDN-decoded hostname of the document
149 */
150 function logHiddenElements(page, selectors, docDomain)
151 {
152 let panel = panels[page._id];
153 if (panel && !panel.reload && !panel.reloading)
154 {
155 for (let subscription of FilterStorage.subscriptions)
156 {
157 if (subscription.disabled)
158 continue;
159
160 for (let filter of subscription.filters)
161 {
162 if (!(filter instanceof ElemHideFilter))
163 continue;
164 if (selectors.indexOf(filter.selector) == -1)
165 continue;
166 if (!filter.isActiveOnDomain(docDomain))
167 continue;
168
169 addRecord(panel, {type: "ELEMHIDE", docDomain: docDomain}, filter);
170 }
171 }
172 }
173 };
174 exports.logHiddenElements = logHiddenElements;
175
176 /**
177 * Checks whether a page is inspected by the devtools panel.
178 *
179 * @param {Page} page
180 * @return {Boolean}
181 */
182 function hasDevToolsPanel(page)
183 {
184 return page._id in panels;
185 }
186 exports.hasDevToolsPanel = hasDevToolsPanel;
187
188 function onBeforeRequest(details)
189 {
190 let panel = panels[details.tabId];
191
192 // Clear the devtools panel and reload the inspected tab without caching
193 // when a new request is issued. However, make sure that we don't end up
194 // in an infinite recursion if we already triggered a reload.
195 if (panel.reloading)
196 {
197 panel.reloading = false;
198 }
199 else
200 {
201 panel.records = [];
202 panel.panel.sendMessage({type: "reset"});
203
204 // We can't repeat the request if it isn't a GET request. Chrome would
205 // prompt the user to confirm reloading the page, and POST requests are
206 // known to cause issues on many websites if repeated.
207 if (details.method == "GET")
208 panel.reload = true;
209 }
210 }
211
212 function onLoading(page)
213 {
214 let tabId = page._id;
215 let panel = panels[tabId];
216
217 // Reloading the tab is the only way that allows bypassing all caches, in
218 // order to see all requests in the devtools panel. Reloading must not be
219 // performed before the tab changes to "loading", otherwise it will load the
220 // previous URL.
221 if (panel && panel.reload)
222 {
223 chrome.tabs.reload(tabId, {bypassCache: true});
224
225 panel.reload = false;
226 panel.reloading = true;
227 }
228 }
229
230 function onFilterChange(action, arg)
231 {
232 // When there haven't ever been any user filters before, the
233 // subscription is added, but no "filter.added" event is triggered.
234 let added, filters;
235 if (action == "subscription.added")
236 {
237 if (!(arg instanceof SpecialSubscription))
238 return;
239
240 added = true;
241 filters = arg.filters;
242 }
243 else
244 {
245 added = (action == "filter.added");
246
247 if (!added && action != "filter.removed")
248 return;
249
250 filters = [arg];
251 }
252
253 for (let tabId in panels)
254 {
255 let panel = panels[tabId];
256
257 for (let i = 0; i < panel.records.length; i++)
258 {
259 let record = panel.records[i];
260
261 // If an added filter matches a request shown in the devtools panel,
262 // update that record to show the new filter. Ignore element hiding
263 // filters since there are only records for element hiding filters that
264 // already match, and also we don't know if any new element matches.
265 if (added)
266 {
267 if (record.request.type == "ELEMHIDE")
268 continue;
269
270 let filter = matchRequest(record.request);
271 if (filters.indexOf(filter) == -1)
272 continue;
273
274 record.filter = filter;
275 }
276
277 // If a filter shown in the devtools panel got removed, update that
278 // record to show the filter (if any) that matches now instead.
279 // However, for element hiding, just remove the record, since we don't
280 // know whether another filter would match until the page is reloaded.
281 else
282 {
283 if (filters.indexOf(record.filter) == -1)
284 continue;
285
286 if (record.request.type == "ELEMHIDE")
287 {
288 panel.panel.sendMessage({
289 type: "remove-record",
290 index: i
291 });
292 panel.records.splice(i--, 1);
293 continue;
294 }
295
296 record.filter = matchRequest(record.request);
297 }
298
299 panel.panel.sendMessage({
300 type: "update-record",
301 index: i,
302 request: getRequestInfo(record.request),
303 filter: getFilterInfo(record.filter)
304 });
305 }
306 }
307 }
308
309 ext.devtools.onCreated.addListener(function(panel)
310 {
311 let inspectedTabId = panel.inspectedTabId;
312 let localOnBeforeRequest = onBeforeRequest.bind();
313
314 chrome.webRequest.onBeforeRequest.addListener(
315 localOnBeforeRequest,
316 {
317 urls: ["<all_urls>"],
318 types: ["main_frame"],
319 tabId: inspectedTabId
320 }
321 );
322
323 if (!hasPanels())
324 {
325 ext.pages.onLoading.addListener(onLoading);
326 FilterNotifier.addListener(onFilterChange);
327 }
328
329 panel.onRemoved.addListener(function()
330 {
331 delete panels[inspectedTabId];
332 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest);
333
334 if (!hasPanels())
335 {
336 FilterNotifier.removeListener(onFilterChange);
337 ext.pages.onLoading.removeListener(onLoading);
338 }
339 });
340
341 panels[inspectedTabId] = {panel: panel, records: []};
342 });
343 }
344 else
345 {
346 exports.logRequest = () => {};
347 exports.logHiddenElements = () => {};
348 exports.hasDevToolsPanel = () => false;
349 }
OLDNEW
« no previous file with comments | « include.preload.js ('k') | metadata.chrome » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld