LEFT | RIGHT |
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-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 let info = require("info"); | 18 "use strict"; |
19 if (info.platform == "chromium") | 19 |
20 { | 20 let {RegExpFilter, WhitelistFilter, ElemHideFilter} = require("filterClasses"); |
21 let {WhitelistFilter, ElemHideFilter} = require("filterClasses"); | 21 let {SpecialSubscription} = require("subscriptionClasses"); |
22 let {SpecialSubscription} = require("subscriptionClasses"); | 22 let {FilterStorage} = require("filterStorage"); |
23 let {FilterStorage} = require("filterStorage"); | 23 let {defaultMatcher} = require("matcher"); |
24 let {defaultMatcher} = require("matcher"); | 24 let {FilterNotifier} = require("filterNotifier"); |
25 let {FilterNotifier} = require("filterNotifier"); | 25 |
26 | 26 const nonRequestTypes = ["DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE"]; |
27 // Mapping of inspected tabs to their devpanel page | 27 |
28 // and recorded items. We can't use a PageMap here, | 28 // Mapping of inspected tabs to their devpanel page |
29 // because data must persist after navigation/reload. | 29 // and recorded items. We can't use a PageMap here, |
30 let panels = Object.create(null); | 30 // because data must persist after navigation/reload. |
31 | 31 let panels = Object.create(null); |
32 function hasPanels() | 32 |
33 { | 33 function hasPanels() |
34 return Object.keys(panels).length > 0; | 34 { |
35 } | 35 return Object.keys(panels).length > 0; |
36 | 36 } |
37 function getRequestInfo(request) | 37 |
38 { | 38 function getActivePanel(page) |
39 return { | 39 { |
40 url: request.url, | 40 let panel = panels[page._id]; |
41 type: request.type, | 41 if(panel && !panel.reload && !panel.reloading) |
42 docDomain: request.docDomain | 42 return panel; |
| 43 return null; |
| 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.url == request.url && |
| 77 record.request.docDomain == request.docDomain && |
| 78 |
| 79 // Ignore partial (e.g. ELEMHIDE) whitelisting if there is already |
| 80 // a DOCUMENT exception which disables all means of blocking. |
| 81 (record.request.type == "DOCUMENT" ? nonRequestTypes.indexOf(request.type) !
= -1 |
| 82 : record.request.type == request.type) && |
| 83 |
| 84 // Matched element hiding filters don't relate to a particular request, |
| 85 // so we also have to match the CSS selector in order to distinguish them. |
| 86 (record.filter && record.filter.selector) == (filter && filter.selector) |
| 87 ); |
| 88 } |
| 89 |
| 90 function addRecord(panel, request, filter) |
| 91 { |
| 92 if (!hasRecord(panel, request, filter)) |
| 93 { |
| 94 panel.port.postMessage({ |
| 95 type: "add-record", |
| 96 request: request, |
| 97 filter: getFilterInfo(filter) |
| 98 }); |
| 99 |
| 100 panel.records.push({ |
| 101 request: request, |
| 102 filter: filter |
| 103 }); |
| 104 } |
| 105 } |
| 106 |
| 107 function matchRequest(request) |
| 108 { |
| 109 return defaultMatcher.matchesAny( |
| 110 request.url, |
| 111 RegExpFilter.typeMap[request.type], |
| 112 request.docDomain, |
| 113 request.thirdParty, |
| 114 request.sitekey, |
| 115 request.specificOnly |
| 116 ); |
| 117 } |
| 118 |
| 119 /** |
| 120 * Logs a request to the devtools panel. |
| 121 * |
| 122 * @param {Page} page The page the request occured on |
| 123 * @param {string} url The URL of the request |
| 124 * @param {string} type The request type |
| 125 * @param {string} docDomain The IDN-decoded hostname of the document |
| 126 * @param {boolean} thirdParty Whether the origin of the request and documen
t differs |
| 127 * @param {?string} sitekey The active sitekey if there is any |
| 128 * @param {?boolean} specificOnly Whether generic filters should be ignored |
| 129 * @param {?BlockingFilter} filter The matched filter or null if there is no mat
ch |
| 130 */ |
| 131 exports.logRequest = function(page, url, type, docDomain, |
| 132 thirdParty, sitekey, |
| 133 specificOnly, filter) |
| 134 { |
| 135 let panel = getActivePanel(page); |
| 136 if (panel) |
| 137 { |
| 138 let request = { |
| 139 url: url, |
| 140 type: type, |
| 141 docDomain: docDomain, |
| 142 thirdParty: thirdParty, |
| 143 sitekey: sitekey, |
| 144 specificOnly: specificOnly |
43 }; | 145 }; |
44 } | 146 |
45 | 147 addRecord(panel, request, filter); |
46 function getFilterInfo(filter) | 148 } |
47 { | 149 }; |
48 if (!filter) | 150 |
49 return null; | 151 /** |
50 | 152 * Logs active element hiding filters to the devtools panel. |
51 let userDefined = false; | 153 * |
52 let subscriptionTitle = null; | 154 * @param {Page} page The page the elements were hidden on |
53 | 155 * @param {string[]} selectors The CSS selectors of active elemhide filters |
54 for (let subscription of filter.subscriptions) | 156 * @param {string} docDomain The IDN-decoded hostname of the document |
55 { | 157 */ |
56 if (!subscription.disabled) | 158 exports.logHiddenElements = function(page, selectors, docDomain) |
| 159 { |
| 160 let panel = getActivePanel(page); |
| 161 { |
| 162 for (let subscription of FilterStorage.subscriptions) |
| 163 { |
| 164 if (subscription.disabled) |
| 165 continue; |
| 166 |
| 167 for (let filter of subscription.filters) |
57 { | 168 { |
58 if (subscription instanceof SpecialSubscription) | 169 if (!(filter instanceof ElemHideFilter)) |
59 userDefined = true; | 170 continue; |
60 else | 171 if (selectors.indexOf(filter.selector) == -1) |
61 subscriptionTitle = subscription.title; | 172 continue; |
| 173 if (!filter.isActiveOnDomain(docDomain)) |
| 174 continue; |
| 175 |
| 176 addRecord(panel, {type: "ELEMHIDE", docDomain: docDomain}, filter); |
62 } | 177 } |
63 } | 178 } |
64 | 179 } |
65 return { | 180 }; |
66 text: filter.text, | 181 |
67 whitelisted: filter instanceof WhitelistFilter, | 182 /** |
68 userDefined: userDefined, | 183 * Logs a whitelisting filter, that disables (some kind of) |
69 subscription: subscriptionTitle | 184 * blocking for a particular document, to the devtools panel. |
70 }; | 185 * |
71 } | 186 * @param {Page} page The page the whitelisting is active on |
72 | 187 * @param {string} url The url of the whitelisted document |
73 function hasRecord(panel, request, filter) | 188 * @param {number} typeMask The bit mask of whitelisting types checked fo
r |
74 { | 189 * @param {string} docDomain The IDN-decoded hostname of the parent docume
nt |
75 return panel.records.some(record => | 190 * @param {WhitelistFilter} filter The matched whitelisting filter |
76 record.request.type == request.type && | 191 */ |
77 record.request.url == request.url && | 192 exports.logWhitelistedDocument = function(page, url, typeMask, docDomain, filter
) |
78 record.request.docDomain == request.docDomain && | 193 { |
79 | 194 let panel = getActivePanel(page); |
80 // Matched element hiding filters don't relate to a particular request, | 195 if (panel) |
81 // so we also have to match the CSS selector in order to distinguish them. | 196 { |
82 (request.type != "ELEMHIDE" || record.filter.selector == filter.selector) | 197 for (let type of nonRequestTypes) |
83 ); | 198 { |
84 } | 199 if (typeMask & filter.contentType & RegExpFilter.typeMap[type]) |
85 | 200 addRecord(panel, {url: url, type: type, docDomain: docDomain}, filter); |
86 function addRecord(panel, request, filter) | 201 } |
87 { | 202 } |
88 if (!hasRecord(panel, request, filter)) | 203 }; |
89 { | 204 |
90 panel.panel.sendMessage({ | 205 /** |
91 type: "add-record", | 206 * Checks whether a page is inspected by the devtools panel. |
92 request: getRequestInfo(request), | 207 * |
93 filter: getFilterInfo(filter) | 208 * @param {Page} page |
| 209 * @return {boolean} |
| 210 */ |
| 211 exports.hasPanel = function(page) |
| 212 { |
| 213 return page._id in panels; |
| 214 }; |
| 215 |
| 216 function onBeforeRequest(details) |
| 217 { |
| 218 let panel = panels[details.tabId]; |
| 219 |
| 220 // Clear the devtools panel and reload the inspected tab without caching |
| 221 // when a new request is issued. However, make sure that we don't end up |
| 222 // in an infinite recursion if we already triggered a reload. |
| 223 if (panel.reloading) |
| 224 { |
| 225 panel.reloading = false; |
| 226 } |
| 227 else |
| 228 { |
| 229 panel.records = []; |
| 230 panel.port.postMessage({type: "reset"}); |
| 231 |
| 232 // We can't repeat the request if it isn't a GET request. Chrome would |
| 233 // prompt the user to confirm reloading the page, and POST requests are |
| 234 // known to cause issues on many websites if repeated. |
| 235 if (details.method == "GET") |
| 236 panel.reload = true; |
| 237 } |
| 238 } |
| 239 |
| 240 function onLoading(page) |
| 241 { |
| 242 let tabId = page._id; |
| 243 let panel = panels[tabId]; |
| 244 |
| 245 // Reloading the tab is the only way that allows bypassing all caches, in |
| 246 // order to see all requests in the devtools panel. Reloading must not be |
| 247 // performed before the tab changes to "loading", otherwise it will load the |
| 248 // previous URL. |
| 249 if (panel && panel.reload) |
| 250 { |
| 251 chrome.tabs.reload(tabId, {bypassCache: true}); |
| 252 |
| 253 panel.reload = false; |
| 254 panel.reloading = true; |
| 255 } |
| 256 } |
| 257 |
| 258 function onFilterChange(action, arg) |
| 259 { |
| 260 let added, filters; |
| 261 switch (action) |
| 262 { |
| 263 case "filter.added": |
| 264 added = true; |
| 265 filters = [arg]; |
| 266 break; |
| 267 |
| 268 case "filter.removed": |
| 269 added = false; |
| 270 filters = [arg]; |
| 271 break; |
| 272 |
| 273 // When there haven't ever been any user filters before, the subscription is |
| 274 // added, triggering a "subscription.added" instead of a "filter.added" even
t. |
| 275 case "subscription.added": |
| 276 if (arg instanceof SpecialSubscription) |
| 277 { |
| 278 added = true; |
| 279 filters = arg.filters; |
| 280 break; |
| 281 } |
| 282 |
| 283 default: |
| 284 return; |
| 285 } |
| 286 |
| 287 for (let tabId in panels) |
| 288 { |
| 289 let panel = panels[tabId]; |
| 290 |
| 291 for (let i = 0; i < panel.records.length; i++) |
| 292 { |
| 293 let record = panel.records[i]; |
| 294 |
| 295 // If an added filter matches a request shown in the devtools panel, |
| 296 // update that record to show the new filter. Ignore filters that aren't |
| 297 // associated with any sub-resource request. There is no record for these |
| 298 // if they don't already match. In particular, in case of element hiding |
| 299 // filters, we also wouldn't know if any new element matches. |
| 300 if (added) |
| 301 { |
| 302 if (nonRequestTypes.indexOf(record.request.type) != -1) |
| 303 continue; |
| 304 |
| 305 let filter = matchRequest(record.request); |
| 306 if (filters.indexOf(filter) == -1) |
| 307 continue; |
| 308 |
| 309 record.filter = filter; |
| 310 } |
| 311 |
| 312 // If a filter shown in the devtools panel got removed, update that |
| 313 // record to show the filter that matches now, or none, instead. |
| 314 // For filters that aren't associated with any sub-resource request, |
| 315 // just remove the record. We wouldn't know whether another filter |
| 316 // matches instead until the page is reloaded. |
| 317 else |
| 318 { |
| 319 if (filters.indexOf(record.filter) == -1) |
| 320 continue; |
| 321 |
| 322 if (nonRequestTypes.indexOf(record.request.type) != -1) |
| 323 { |
| 324 panel.port.postMessage({ |
| 325 type: "remove-record", |
| 326 index: i |
| 327 }); |
| 328 panel.records.splice(i--, 1); |
| 329 continue; |
| 330 } |
| 331 |
| 332 record.filter = matchRequest(record.request); |
| 333 } |
| 334 |
| 335 panel.port.postMessage({ |
| 336 type: "update-record", |
| 337 index: i, |
| 338 request: record.request, |
| 339 filter: getFilterInfo(record.filter) |
94 }); | 340 }); |
95 | 341 } |
96 panel.records.push({ | 342 } |
97 request: request, | 343 } |
98 filter: filter | 344 |
99 }); | 345 chrome.runtime.onConnect.addListener(port => |
100 } | 346 { |
101 } | 347 let match = port.name.match(/^devtools-(\d+)$/); |
102 | 348 if (!match) |
103 function matchRequest(request) | 349 return; |
104 { | 350 |
105 return defaultMatcher.matchesAny( | 351 let inspectedTabId = parseInt(match[1], 10); |
106 request.url, | 352 let localOnBeforeRequest = onBeforeRequest.bind(); |
107 request.type, | 353 |
108 request.docDomain, | 354 chrome.webRequest.onBeforeRequest.addListener( |
109 request.thirdParty, | 355 localOnBeforeRequest, |
110 request.sitekey | 356 { |
111 ); | 357 urls: ["<all_urls>"], |
112 } | 358 types: ["main_frame"], |
113 | 359 tabId: inspectedTabId |
114 /** | 360 } |
115 * Logs a request in the devtools panel. | 361 ); |
116 * | 362 |
117 * @param {Page} page The page the request occured on | 363 if (!hasPanels()) |
118 * @param {string] url The URL of the request | 364 { |
119 * @param {string} type The request type | 365 ext.pages.onLoading.addListener(onLoading); |
120 * @param {string} docDomain The IDN-decoded hostname of the document | 366 FilterNotifier.addListener(onFilterChange); |
121 * @param {Boolean} thirdParty Whether the origin of the request and document
differs | 367 } |
122 * @param {string} [sitekey] The active sitekey if there is any | 368 |
123 * @param {filter} [filter] The matched filter or null if there is no matc
h | 369 port.onDisconnect.addListener(() => |
124 */ | 370 { |
125 function logRequest(page, url, type, docDomain, thirdParty, sitekey, filter) | 371 delete panels[inspectedTabId]; |
126 { | 372 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); |
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 | 373 |
323 if (!hasPanels()) | 374 if (!hasPanels()) |
324 { | 375 { |
325 ext.pages.onLoading.addListener(onLoading); | 376 FilterNotifier.removeListener(onFilterChange); |
326 FilterNotifier.addListener(onFilterChange); | 377 ext.pages.onLoading.removeListener(onLoading); |
327 } | 378 } |
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 }); | 379 }); |
343 } | 380 |
344 else | 381 panels[inspectedTabId] = {port: port, records: []}; |
345 { | 382 }); |
346 exports.logRequest = () => {}; | |
347 exports.logHiddenElements = () => {}; | |
348 exports.hasDevToolsPanel = () => false; | |
349 } | |
LEFT | RIGHT |