OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 14 matching lines...) Expand all Loading... |
25 const {defaultMatcher} = require("matcher"); | 25 const {defaultMatcher} = require("matcher"); |
26 const {FilterNotifier} = require("filterNotifier"); | 26 const {FilterNotifier} = require("filterNotifier"); |
27 const {extractHostFromFrame} = require("url"); | 27 const {extractHostFromFrame} = require("url"); |
28 const {port} = require("messaging"); | 28 const {port} = require("messaging"); |
29 | 29 |
30 const nonRequestTypes = ["DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE"]; | 30 const nonRequestTypes = ["DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE"]; |
31 | 31 |
32 // Mapping of inspected tabs to their devpanel page | 32 // Mapping of inspected tabs to their devpanel page |
33 // and recorded items. We can't use a PageMap here, | 33 // and recorded items. We can't use a PageMap here, |
34 // because data must persist after navigation/reload. | 34 // because data must persist after navigation/reload. |
35 let panels = Object.create(null); | 35 let panels = new Map(); |
36 | 36 |
37 function hasPanels() | 37 function hasPanels() |
38 { | 38 { |
39 return Object.keys(panels).length > 0; | 39 return panels.size > 0; |
| 40 } |
| 41 |
| 42 function isActivePanel(panel) |
| 43 { |
| 44 if (panel && !panel.reload && !panel.reloading) |
| 45 return true; |
| 46 return false; |
40 } | 47 } |
41 | 48 |
42 function getActivePanel(page) | 49 function getActivePanel(page) |
43 { | 50 { |
44 let panel = panels[page.id]; | 51 let panel = panels.get(page.id); |
45 if (panel && !panel.reload && !panel.reloading) | 52 if (isActivePanel(panel)) |
46 return panel; | 53 return panel; |
47 return null; | 54 return null; |
48 } | 55 } |
49 | 56 |
50 function getFilterInfo(filter) | 57 function getFilterInfo(filter) |
51 { | 58 { |
52 if (!filter) | 59 if (!filter) |
53 return null; | 60 return null; |
54 | 61 |
55 let userDefined = false; | 62 let userDefined = false; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 * document differs | 136 * document differs |
130 * @param {?string} sitekey The active sitekey if there is any | 137 * @param {?string} sitekey The active sitekey if there is any |
131 * @param {?boolean} specificOnly Whether generic filters should be ignored | 138 * @param {?boolean} specificOnly Whether generic filters should be ignored |
132 * @param {?BlockingFilter} filter The matched filter or null if there is no | 139 * @param {?BlockingFilter} filter The matched filter or null if there is no |
133 * match | 140 * match |
134 */ | 141 */ |
135 exports.logRequest = function(page, url, type, docDomain, | 142 exports.logRequest = function(page, url, type, docDomain, |
136 thirdParty, sitekey, | 143 thirdParty, sitekey, |
137 specificOnly, filter) | 144 specificOnly, filter) |
138 { | 145 { |
139 let panel = getActivePanel(page); | 146 if (!hasPanels()) |
140 if (panel) | 147 return; |
| 148 let request = {url, type, docDomain, thirdParty, sitekey, specificOnly}; |
| 149 if (page) |
141 { | 150 { |
142 let request = {url, type, docDomain, thirdParty, sitekey, specificOnly}; | 151 let panel = getActivePanel(page); |
143 addRecord(panel, request, filter); | 152 if (panel) |
| 153 addRecord(panel, request, filter); |
| 154 return; |
144 } | 155 } |
| 156 for (let panel of panels.values()) |
| 157 if (isActivePanel(panel)) |
| 158 addRecord(panel, request, filter); |
145 }; | 159 }; |
146 | 160 |
147 /** | 161 /** |
148 * Logs active element hiding filters to the devtools panel. | 162 * Logs active element hiding filters to the devtools panel. |
149 * | 163 * |
150 * @param {Page} page The page the elements were hidden on | 164 * @param {Page} page The page the elements were hidden on |
151 * @param {string[]} selectors The selectors of applied ElemHideFilters | 165 * @param {string[]} selectors The selectors of applied ElemHideFilters |
152 * @param {string[]} filters The text of applied ElemHideEmulationFilters | 166 * @param {string[]} filters The text of applied ElemHideEmulationFilters |
153 * @param {string} docDomain The IDN-decoded hostname of the document | 167 * @param {string} docDomain The IDN-decoded hostname of the document |
154 */ | 168 */ |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 }; | 220 }; |
207 | 221 |
208 /** | 222 /** |
209 * Checks whether a page is inspected by the devtools panel. | 223 * Checks whether a page is inspected by the devtools panel. |
210 * | 224 * |
211 * @param {Page} page | 225 * @param {Page} page |
212 * @return {boolean} | 226 * @return {boolean} |
213 */ | 227 */ |
214 exports.hasPanel = function(page) | 228 exports.hasPanel = function(page) |
215 { | 229 { |
216 return page.id in panels; | 230 return panels.has(page.id); |
217 }; | 231 }; |
218 | 232 |
219 function onBeforeRequest(details) | 233 function onBeforeRequest(details) |
220 { | 234 { |
221 let panel = panels[details.tabId]; | 235 let panel = panels.get(details.tabId); |
222 | 236 |
223 // Clear the devtools panel and reload the inspected tab without caching | 237 // Clear the devtools panel and reload the inspected tab without caching |
224 // when a new request is issued. However, make sure that we don't end up | 238 // when a new request is issued. However, make sure that we don't end up |
225 // in an infinite recursion if we already triggered a reload. | 239 // in an infinite recursion if we already triggered a reload. |
226 if (panel.reloading) | 240 if (panel.reloading) |
227 { | 241 { |
228 panel.reloading = false; | 242 panel.reloading = false; |
229 } | 243 } |
230 else | 244 else |
231 { | 245 { |
232 panel.records = []; | 246 panel.records = []; |
233 panel.port.postMessage({type: "reset"}); | 247 panel.port.postMessage({type: "reset"}); |
234 | 248 |
235 // We can't repeat the request if it isn't a GET request. Chrome would | 249 // We can't repeat the request if it isn't a GET request. Chrome would |
236 // prompt the user to confirm reloading the page, and POST requests are | 250 // prompt the user to confirm reloading the page, and POST requests are |
237 // known to cause issues on many websites if repeated. | 251 // known to cause issues on many websites if repeated. |
238 if (details.method == "GET") | 252 if (details.method == "GET") |
239 panel.reload = true; | 253 panel.reload = true; |
240 } | 254 } |
241 } | 255 } |
242 | 256 |
243 function onLoading(page) | 257 function onLoading(page) |
244 { | 258 { |
245 let tabId = page.id; | 259 let tabId = page.id; |
246 let panel = panels[tabId]; | 260 let panel = panels.get(tabId); |
247 | 261 |
248 // Reloading the tab is the only way that allows bypassing all caches, in | 262 // Reloading the tab is the only way that allows bypassing all caches, in |
249 // order to see all requests in the devtools panel. Reloading must not be | 263 // order to see all requests in the devtools panel. Reloading must not be |
250 // performed before the tab changes to "loading", otherwise it will load the | 264 // performed before the tab changes to "loading", otherwise it will load the |
251 // previous URL. | 265 // previous URL. |
252 if (panel && panel.reload) | 266 if (panel && panel.reload) |
253 { | 267 { |
254 chrome.tabs.reload(tabId, {bypassCache: true}); | 268 chrome.tabs.reload(tabId, {bypassCache: true}); |
255 | 269 |
256 panel.reload = false; | 270 panel.reload = false; |
257 panel.reloading = true; | 271 panel.reloading = true; |
258 } | 272 } |
259 } | 273 } |
260 | 274 |
261 function updateFilters(filters, added) | 275 function updateFilters(filters, added) |
262 { | 276 { |
263 for (let tabId in panels) | 277 for (let panel of panels.values()) |
264 { | 278 { |
265 let panel = panels[tabId]; | |
266 | |
267 for (let i = 0; i < panel.records.length; i++) | 279 for (let i = 0; i < panel.records.length; i++) |
268 { | 280 { |
269 let record = panel.records[i]; | 281 let record = panel.records[i]; |
270 | 282 |
271 // If an added filter matches a request shown in the devtools panel, | 283 // If an added filter matches a request shown in the devtools panel, |
272 // update that record to show the new filter. Ignore filters that aren't | 284 // update that record to show the new filter. Ignore filters that aren't |
273 // associated with any sub-resource request. There is no record for these | 285 // associated with any sub-resource request. There is no record for these |
274 // if they don't already match. In particular, in case of element hiding | 286 // if they don't already match. In particular, in case of element hiding |
275 // filters, we also wouldn't know if any new element matches. | 287 // filters, we also wouldn't know if any new element matches. |
276 if (added) | 288 if (added) |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 if (!hasPanels()) | 367 if (!hasPanels()) |
356 { | 368 { |
357 ext.pages.onLoading.addListener(onLoading); | 369 ext.pages.onLoading.addListener(onLoading); |
358 FilterNotifier.on("filter.added", onFilterAdded); | 370 FilterNotifier.on("filter.added", onFilterAdded); |
359 FilterNotifier.on("filter.removed", onFilterRemoved); | 371 FilterNotifier.on("filter.removed", onFilterRemoved); |
360 FilterNotifier.on("subscription.added", onSubscriptionAdded); | 372 FilterNotifier.on("subscription.added", onSubscriptionAdded); |
361 } | 373 } |
362 | 374 |
363 newPort.onDisconnect.addListener(() => | 375 newPort.onDisconnect.addListener(() => |
364 { | 376 { |
365 delete panels[inspectedTabId]; | 377 delete panels.delete(inspectedTabId); |
366 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); | 378 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); |
367 | 379 |
368 if (!hasPanels()) | 380 if (!hasPanels()) |
369 { | 381 { |
370 ext.pages.onLoading.removeListener(onLoading); | 382 ext.pages.onLoading.removeListener(onLoading); |
371 FilterNotifier.off("filter.added", onFilterAdded); | 383 FilterNotifier.off("filter.added", onFilterAdded); |
372 FilterNotifier.off("filter.removed", onFilterRemoved); | 384 FilterNotifier.off("filter.removed", onFilterRemoved); |
373 FilterNotifier.off("subscription.added", onSubscriptionAdded); | 385 FilterNotifier.off("subscription.added", onSubscriptionAdded); |
374 } | 386 } |
375 }); | 387 }); |
376 | 388 |
377 panels[inspectedTabId] = {port: newPort, records: []}; | 389 panels.set(inspectedTabId, {port: newPort, records: []}); |
378 }); | 390 }); |
379 | 391 |
380 port.on("devtools.traceElemHide", (message, sender) => | 392 port.on("devtools.traceElemHide", (message, sender) => |
381 { | 393 { |
382 logHiddenElements( | 394 logHiddenElements( |
383 sender.page, message.selectors, message.filters, | 395 sender.page, message.selectors, message.filters, |
384 extractHostFromFrame(sender.frame) | 396 extractHostFromFrame(sender.frame) |
385 ); | 397 ); |
386 }); | 398 }); |
OLD | NEW |