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 let {stringifyURL, isThirdParty} = require("url"); | 26 const nonRequestTypes = ["DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE"]; |
27 | 27 |
28 // Mapping of inspected tabs to their devpanel page | 28 // Mapping of inspected tabs to their devpanel page |
29 // and recorded items. We can't use a PageMap here, | 29 // and recorded items. We can't use a PageMap here, |
30 // because data must persist after navigation/reload. | 30 // because data must persist after navigation/reload. |
31 let panels = Object.create(null); | 31 let panels = Object.create(null); |
32 | 32 |
33 function getRequestInfo(request) | 33 function hasPanels() |
34 { | 34 { |
35 return { | 35 return Object.keys(panels).length > 0; |
36 url: request.url && stringifyURL(request.url), | 36 } |
37 type: request.type, | 37 |
38 docDomain: request.docDomain | 38 function getActivePanel(page) |
39 }; | 39 { |
40 } | 40 let panel = panels[page._id]; |
41 | 41 if(panel && !panel.reload && !panel.reloading) |
42 function getFilterInfo(filter) | 42 return panel; |
43 { | 43 return null; |
44 if (!filter) | 44 } |
45 return null; | 45 |
46 | 46 function getFilterInfo(filter) |
47 let userDefined = false; | 47 { |
48 let subscriptionTitle = null; | 48 if (!filter) |
49 | 49 return null; |
50 for (let subscription of filter.subscriptions) | 50 |
51 { | 51 let userDefined = false; |
52 if (!subscription.disabled) | 52 let subscriptionTitle = null; |
53 { | 53 |
54 if (subscription instanceof SpecialSubscription) | 54 for (let subscription of filter.subscriptions) |
55 userDefined = true; | 55 { |
56 else | 56 if (!subscription.disabled) |
57 subscriptionTitle = subscription.title; | 57 { |
58 } | 58 if (subscription instanceof SpecialSubscription) |
59 } | 59 userDefined = true; |
60 | 60 else |
61 return { | 61 subscriptionTitle = subscription.title; |
62 text: filter.text, | 62 } |
63 whitelisted: filter instanceof WhitelistFilter, | 63 } |
64 userDefined: userDefined, | 64 |
65 subscription: subscriptionTitle | 65 return { |
66 }; | 66 text: filter.text, |
67 } | 67 whitelisted: filter instanceof WhitelistFilter, |
68 | 68 userDefined: userDefined, |
69 function addRecord(panel, request, filter) | 69 subscription: subscriptionTitle |
70 { | 70 }; |
71 panel.page.sendMessage({ | 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({ |
72 type: "add-record", | 95 type: "add-record", |
73 request: getRequestInfo(request), | 96 request: request, |
74 filter: getFilterInfo(filter) | 97 filter: getFilterInfo(filter) |
75 }); | 98 }); |
76 | 99 |
77 panel.records.push({ | 100 panel.records.push({ |
78 request: request, | 101 request: request, |
79 filter: filter | 102 filter: filter |
80 }); | 103 }); |
81 } | 104 } |
82 | 105 } |
83 function matchRequest(request) | 106 |
84 { | 107 function matchRequest(request) |
85 return defaultMatcher.matchesAny( | 108 { |
86 stringifyURL(request.url), | 109 return defaultMatcher.matchesAny( |
87 request.type, | 110 request.url, |
88 request.docDomain, | 111 RegExpFilter.typeMap[request.type], |
89 isThirdParty(request.url, request.docDomain), | 112 request.docDomain, |
90 request.sitekey | 113 request.thirdParty, |
91 ); | 114 request.sitekey, |
92 } | 115 request.specificOnly |
93 | 116 ); |
94 /** | 117 } |
95 * Logs a request in the devtools panel. | 118 |
96 * | 119 /** |
97 * @param {Page} [page] The page the request occured on | 120 * Logs a request to the devtools panel. |
98 * @param {URL] [url] The URL of the request | 121 * |
99 * @param {string} [type] The request type | 122 * @param {Page} page The page the request occured on |
100 * @param {string} [docDomain] The IDN-decoded hostname of the document | 123 * @param {string} url The URL of the request |
101 * @param {string} [sitekey] The active sitekey if there is any | 124 * @param {string} type The request type |
102 * @param {filter} [filter] The matched filter or null if there is no match | 125 * @param {string} docDomain The IDN-decoded hostname of the document |
103 */ | 126 * @param {boolean} thirdParty Whether the origin of the request and documen
t differs |
104 function logRequest(page, url, type, docDomain, sitekey, filter) | 127 * @param {?string} sitekey The active sitekey if there is any |
105 { | 128 * @param {?boolean} specificOnly Whether generic filters should be ignored |
106 let panel = panels[page._id]; | 129 * @param {?BlockingFilter} filter The matched filter or null if there is no mat
ch |
107 if (panel) | 130 */ |
108 { | 131 exports.logRequest = function(page, url, type, docDomain, |
109 let request = { | 132 thirdParty, sitekey, |
110 url: url, | 133 specificOnly, filter) |
111 type: type, | 134 { |
112 docDomain: docDomain, | 135 let panel = getActivePanel(page); |
113 sitekey: sitekey | 136 if (panel) |
114 }; | 137 { |
115 | 138 let request = { |
116 addRecord(panel, request, filter); | 139 url: url, |
117 } | 140 type: type, |
118 } | 141 docDomain: docDomain, |
119 exports.logRequest = logRequest; | 142 thirdParty: thirdParty, |
120 | 143 sitekey: sitekey, |
121 /** | 144 specificOnly: specificOnly |
122 * Logs active element hiding filters in the devtools panel. | 145 }; |
123 * | 146 |
124 * @param {Page} [page] The page the elements were hidden on | 147 addRecord(panel, request, filter); |
125 * @param {string[]} [selectors] The CSS selectors of active elemhide filters | 148 } |
126 * @param {string} [docDomain] The IDN-decoded hostname of the document | 149 }; |
127 */ | 150 |
128 function logHiddenElements(page, selectors, docDomain) | 151 /** |
129 { | 152 * Logs active element hiding filters to the devtools panel. |
130 let panel = panels[page._id]; | 153 * |
131 if (panel) | 154 * @param {Page} page The page the elements were hidden on |
132 { | 155 * @param {string[]} selectors The CSS selectors of active elemhide filters |
133 for (let subscription of FilterStorage.subscriptions) | 156 * @param {string} docDomain The IDN-decoded hostname of the document |
| 157 */ |
| 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) |
134 { | 168 { |
135 if (subscription.disabled) | 169 if (!(filter instanceof ElemHideFilter)) |
136 continue; | 170 continue; |
137 | 171 if (selectors.indexOf(filter.selector) == -1) |
138 for (let filter of subscription.filters) | 172 continue; |
| 173 if (!filter.isActiveOnDomain(docDomain)) |
| 174 continue; |
| 175 |
| 176 addRecord(panel, {type: "ELEMHIDE", docDomain: docDomain}, filter); |
| 177 } |
| 178 } |
| 179 } |
| 180 }; |
| 181 |
| 182 /** |
| 183 * Logs a whitelisting filter, that disables (some kind of) |
| 184 * blocking for a particular document, to the devtools panel. |
| 185 * |
| 186 * @param {Page} page The page the whitelisting is active on |
| 187 * @param {string} url The url of the whitelisted document |
| 188 * @param {number} typeMask The bit mask of whitelisting types checked fo
r |
| 189 * @param {string} docDomain The IDN-decoded hostname of the parent docume
nt |
| 190 * @param {WhitelistFilter} filter The matched whitelisting filter |
| 191 */ |
| 192 exports.logWhitelistedDocument = function(page, url, typeMask, docDomain, filter
) |
| 193 { |
| 194 let panel = getActivePanel(page); |
| 195 if (panel) |
| 196 { |
| 197 for (let type of nonRequestTypes) |
| 198 { |
| 199 if (typeMask & filter.contentType & RegExpFilter.typeMap[type]) |
| 200 addRecord(panel, {url: url, type: type, docDomain: docDomain}, filter); |
| 201 } |
| 202 } |
| 203 }; |
| 204 |
| 205 /** |
| 206 * Checks whether a page is inspected by the devtools panel. |
| 207 * |
| 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) |
139 { | 323 { |
140 if (!(filter instanceof ElemHideFilter)) | 324 panel.port.postMessage({ |
141 continue; | 325 type: "remove-record", |
142 if (selectors.indexOf(filter.selector) == -1) | 326 index: i |
143 continue; | 327 }); |
144 if (!filter.isActiveOnDomain(docDomain)) | 328 panel.records.splice(i--, 1); |
145 continue; | 329 continue; |
146 if (panel.records.some(record => record.request.type == "ELEMHIDE" && | |
147 record.request.docDomain == docDomain
&& | |
148 record.filter.selector == filter.sele
ctor)) | |
149 continue; | |
150 | |
151 addRecord(panel, {type: "ELEMHIDE", docDomain: docDomain}, filter); | |
152 } | 330 } |
| 331 |
| 332 record.filter = matchRequest(record.request); |
153 } | 333 } |
154 } | 334 |
155 }; | 335 panel.port.postMessage({ |
156 exports.logHiddenElements = logHiddenElements; | 336 type: "update-record", |
157 | 337 index: i, |
158 /** | 338 request: record.request, |
159 * Starts to record items and shows them in the devtools panel. | 339 filter: getFilterInfo(record.filter) |
160 * | 340 }); |
161 * @param {Page} [panelPage] The page of the the devtools panel | 341 } |
162 * @param {number} [inpectedTabId] The ID of the tab to record item from | 342 } |
163 */ | 343 } |
164 function initDevToolsPanel(panelPage, inpectedTabId) | 344 |
165 { | 345 chrome.runtime.onConnect.addListener(port => |
166 panels[inpectedTabId] = {page: panelPage, records: []}; | 346 { |
167 | 347 let match = port.name.match(/^devtools-(\d+)$/); |
168 // Forget about this devtools panel and the items recorded there | 348 if (!match) |
169 // so far, when the panel (not the tab it is inspecting) is closed. | 349 return; |
170 function onRemoved(tabId) | 350 |
171 { | 351 let inspectedTabId = parseInt(match[1], 10); |
172 if (tabId == panelPage._id) | 352 let localOnBeforeRequest = onBeforeRequest.bind(); |
173 { | 353 |
174 delete panels[inpectedTabId]; | 354 chrome.webRequest.onBeforeRequest.addListener( |
175 chrome.tabs.onRemoved.removeListener(onRemoved); | 355 localOnBeforeRequest, |
176 } | 356 { |
177 } | 357 urls: ["<all_urls>"], |
178 chrome.tabs.onRemoved.addListener(onRemoved); | 358 types: ["main_frame"], |
179 } | 359 tabId: inspectedTabId |
180 exports.initDevToolsPanel = initDevToolsPanel; | 360 } |
181 | 361 ); |
182 /** | 362 |
183 * Checks whether a page is inspected by the devtools panel. | 363 if (!hasPanels()) |
184 * | 364 { |
185 * @param {Page} [page] | 365 ext.pages.onLoading.addListener(onLoading); |
186 * @return {Boolean} | 366 FilterNotifier.addListener(onFilterChange); |
187 */ | 367 } |
188 function hasDevToolsPanel(page) | 368 |
189 { | 369 port.onDisconnect.addListener(() => |
190 return page._id in panels; | 370 { |
191 } | 371 delete panels[inspectedTabId]; |
192 exports.hasDevToolsPanel = hasDevToolsPanel; | 372 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); |
193 | 373 |
194 chrome.webNavigation.onBeforeNavigate.addListener(function(details) | 374 if (!hasPanels()) |
195 { | 375 { |
196 let panel = panels[details.tabId]; | 376 FilterNotifier.removeListener(onFilterChange); |
197 if (panel && details.frameId == 0) | 377 ext.pages.onLoading.removeListener(onLoading); |
198 { | |
199 // We have to flush the in-memory cache on page load. | |
200 // Otherwise requests answered from the in-memory cache | |
201 // will not be shown in the devtools panel. | |
202 chrome.webRequest.handlerBehaviorChanged(); | |
203 | |
204 panel.records = []; | |
205 panel.page.sendMessage({type: "reset"}); | |
206 } | 378 } |
207 }); | 379 }); |
208 | 380 |
209 FilterNotifier.addListener(function(action, filter) | 381 panels[inspectedTabId] = {port: port, records: []}; |
210 { | 382 }); |
211 if (action != "filter.added" && action != "filter.removed") | |
212 return; | |
213 | |
214 for (let tabId in panels) | |
215 { | |
216 let panel = panels[tabId]; | |
217 | |
218 for (let i = 0; i < panel.records.length; i++) | |
219 { | |
220 let record = panel.records[i]; | |
221 | |
222 if (action == "filter.added") | |
223 { | |
224 if (record.request.type == "ELEMHIDE") | |
225 continue; | |
226 | |
227 if (matchRequest(record.request) != filter) | |
228 continue; | |
229 | |
230 record.filter = filter; | |
231 } | |
232 | |
233 if (action == "filter.removed") | |
234 { | |
235 if (record.filter != filter) | |
236 continue; | |
237 | |
238 if (record.request.type == "ELEMHIDE") | |
239 { | |
240 panel.page.sendMessage({ | |
241 type: "remove-record", | |
242 index: i | |
243 }); | |
244 panel.records.splice(i--, 1); | |
245 continue; | |
246 } | |
247 | |
248 record.filter = matchRequest(record.request); | |
249 } | |
250 | |
251 panel.page.sendMessage({ | |
252 type: "update-record", | |
253 index: i, | |
254 request: getRequestInfo(record.request), | |
255 filter: getFilterInfo(record.filter) | |
256 }); | |
257 } | |
258 } | |
259 }); | |
260 } | |
261 else | |
262 { | |
263 exports.logRequest = () => {}; | |
264 exports.logHiddenElements = () => {}; | |
265 exports.initDevToolsPanel = () => {}; | |
266 exports.hasDevToolsPanel = () => false; | |
267 } | |
LEFT | RIGHT |