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 26 matching lines...) Expand all Loading... |
37 let panels = Object.create(null); | 37 let panels = Object.create(null); |
38 | 38 |
39 function hasPanels() | 39 function hasPanels() |
40 { | 40 { |
41 return Object.keys(panels).length > 0; | 41 return Object.keys(panels).length > 0; |
42 } | 42 } |
43 | 43 |
44 function getActivePanel(page) | 44 function getActivePanel(page) |
45 { | 45 { |
46 let panel = panels[page.id]; | 46 let panel = panels[page.id]; |
47 if(panel && !panel.reload && !panel.reloading) | 47 if (panel && !panel.reload && !panel.reloading) |
48 return panel; | 48 return panel; |
49 return null; | 49 return null; |
50 } | 50 } |
51 | 51 |
52 function getFilterInfo(filter) | 52 function getFilterInfo(filter) |
53 { | 53 { |
54 if (!filter) | 54 if (!filter) |
55 return null; | 55 return null; |
56 | 56 |
57 let userDefined = false; | 57 let userDefined = false; |
58 let subscriptionTitle = null; | 58 let subscriptionTitle = null; |
59 | 59 |
60 for (let subscription of filter.subscriptions) | 60 for (let subscription of filter.subscriptions) |
61 { | 61 { |
62 if (!subscription.disabled) | 62 if (!subscription.disabled) |
63 { | 63 { |
64 if (subscription instanceof SpecialSubscription) | 64 if (subscription instanceof SpecialSubscription) |
65 userDefined = true; | 65 userDefined = true; |
66 else | 66 else |
67 subscriptionTitle = subscription.title; | 67 subscriptionTitle = subscription.title; |
68 } | 68 } |
69 } | 69 } |
70 | 70 |
71 return { | 71 return { |
72 text: filter.text, | 72 text: filter.text, |
73 whitelisted: filter instanceof WhitelistFilter, | 73 whitelisted: filter instanceof WhitelistFilter, |
74 userDefined: userDefined, | 74 userDefined, |
75 subscription: subscriptionTitle | 75 subscription: subscriptionTitle |
76 }; | 76 }; |
77 } | 77 } |
78 | 78 |
79 function hasRecord(panel, request, filter) | 79 function hasRecord(panel, request, filter) |
80 { | 80 { |
81 return panel.records.some(record => | 81 return panel.records.some(record => |
82 record.request.url == request.url && | 82 record.request.url == request.url && |
83 record.request.docDomain == request.docDomain && | 83 record.request.docDomain == request.docDomain && |
84 | 84 |
85 // Ignore partial (e.g. ELEMHIDE) whitelisting if there is already | 85 // Ignore partial (e.g. ELEMHIDE) whitelisting if there is already |
86 // a DOCUMENT exception which disables all means of blocking. | 86 // a DOCUMENT exception which disables all means of blocking. |
87 (record.request.type == "DOCUMENT" ? nonRequestTypes.indexOf(request.type) !
= -1 | 87 ( |
88 : record.request.type == request.type) && | 88 record.request.type == "DOCUMENT" ? |
| 89 nonRequestTypes.indexOf(request.type) != -1 : |
| 90 record.request.type == request.type |
| 91 ) && |
89 | 92 |
90 // Matched element hiding filters don't relate to a particular request, | 93 // Matched element hiding filters don't relate to a particular request, |
91 // so we also have to match the CSS selector in order to distinguish them. | 94 // so we also have to match the CSS selector in order to distinguish them. |
92 (record.filter && record.filter.selector) == (filter && filter.selector) | 95 (record.filter && record.filter.selector) == (filter && filter.selector) |
93 ); | 96 ); |
94 } | 97 } |
95 | 98 |
96 function addRecord(panel, request, filter) | 99 function addRecord(panel, request, filter) |
97 { | 100 { |
98 if (!hasRecord(panel, request, filter)) | 101 if (!hasRecord(panel, request, filter)) |
99 { | 102 { |
100 panel.port.postMessage({ | 103 panel.port.postMessage({ |
101 type: "add-record", | 104 type: "add-record", |
102 request: request, | 105 request, |
103 filter: getFilterInfo(filter) | 106 filter: getFilterInfo(filter) |
104 }); | 107 }); |
105 | 108 |
106 panel.records.push({ | 109 panel.records.push({request, filter}); |
107 request: request, | |
108 filter: filter | |
109 }); | |
110 } | 110 } |
111 } | 111 } |
112 | 112 |
113 function matchRequest(request) | 113 function matchRequest(request) |
114 { | 114 { |
115 return defaultMatcher.matchesAny( | 115 return defaultMatcher.matchesAny( |
116 request.url, | 116 request.url, |
117 RegExpFilter.typeMap[request.type], | 117 RegExpFilter.typeMap[request.type], |
118 request.docDomain, | 118 request.docDomain, |
119 request.thirdParty, | 119 request.thirdParty, |
120 request.sitekey, | 120 request.sitekey, |
121 request.specificOnly | 121 request.specificOnly |
122 ); | 122 ); |
123 } | 123 } |
124 | 124 |
125 /** | 125 /** |
126 * Logs a request to the devtools panel. | 126 * Logs a request to the devtools panel. |
127 * | 127 * |
128 * @param {Page} page The page the request occured on | 128 * @param {Page} page The page the request occured on |
129 * @param {string} url The URL of the request | 129 * @param {string} url The URL of the request |
130 * @param {string} type The request type | 130 * @param {string} type The request type |
131 * @param {string} docDomain The IDN-decoded hostname of the document | 131 * @param {string} docDomain The IDN-decoded hostname of the document |
132 * @param {boolean} thirdParty Whether the origin of the request and documen
t differs | 132 * @param {boolean} thirdParty Whether the origin of the request and |
| 133 * document differs |
133 * @param {?string} sitekey The active sitekey if there is any | 134 * @param {?string} sitekey The active sitekey if there is any |
134 * @param {?boolean} specificOnly Whether generic filters should be ignored | 135 * @param {?boolean} specificOnly Whether generic filters should be ignored |
135 * @param {?BlockingFilter} filter The matched filter or null if there is no mat
ch | 136 * @param {?BlockingFilter} filter The matched filter or null if there is no |
| 137 * match |
136 */ | 138 */ |
137 exports.logRequest = function(page, url, type, docDomain, | 139 exports.logRequest = function(page, url, type, docDomain, |
138 thirdParty, sitekey, | 140 thirdParty, sitekey, |
139 specificOnly, filter) | 141 specificOnly, filter) |
140 { | 142 { |
141 let panel = getActivePanel(page); | 143 let panel = getActivePanel(page); |
142 if (panel) | 144 if (panel) |
143 { | 145 { |
144 let request = { | 146 addRecord(panel, {url, type, docDomain, thirdParty, sitekey, specificOnly}, |
145 url: url, | 147 filter); |
146 type: type, | |
147 docDomain: docDomain, | |
148 thirdParty: thirdParty, | |
149 sitekey: sitekey, | |
150 specificOnly: specificOnly | |
151 }; | |
152 | |
153 addRecord(panel, request, filter); | |
154 } | 148 } |
155 }; | 149 }; |
156 | 150 |
157 /** | 151 /** |
158 * Logs active element hiding filters to the devtools panel. | 152 * Logs active element hiding filters to the devtools panel. |
159 * | 153 * |
160 * @param {Page} page The page the elements were hidden on | 154 * @param {Page} page The page the elements were hidden on |
161 * @param {string[]} selectors The CSS selectors of active elemhide filters | 155 * @param {string[]} selectors The CSS selectors of active elemhide filters |
162 * @param {string} docDomain The IDN-decoded hostname of the document | 156 * @param {string} docDomain The IDN-decoded hostname of the document |
163 */ | 157 */ |
(...skipping 10 matching lines...) Expand all Loading... |
174 for (let filter of subscription.filters) | 168 for (let filter of subscription.filters) |
175 { | 169 { |
176 if (!(filter instanceof ElemHideFilter) && | 170 if (!(filter instanceof ElemHideFilter) && |
177 !(filter instanceof ElemHideEmulationFilter)) | 171 !(filter instanceof ElemHideEmulationFilter)) |
178 continue; | 172 continue; |
179 if (selectors.indexOf(filter.selector) == -1) | 173 if (selectors.indexOf(filter.selector) == -1) |
180 continue; | 174 continue; |
181 if (!filter.isActiveOnDomain(docDomain)) | 175 if (!filter.isActiveOnDomain(docDomain)) |
182 continue; | 176 continue; |
183 | 177 |
184 addRecord(panel, {type: "ELEMHIDE", docDomain: docDomain}, filter); | 178 addRecord(panel, {type: "ELEMHIDE", docDomain}, filter); |
185 } | 179 } |
186 } | 180 } |
187 } | 181 } |
188 }; | 182 } |
189 | 183 |
190 /** | 184 /** |
191 * Logs a whitelisting filter, that disables (some kind of) | 185 * Logs a whitelisting filter, that disables (some kind of) |
192 * blocking for a particular document, to the devtools panel. | 186 * blocking for a particular document, to the devtools panel. |
193 * | 187 * |
194 * @param {Page} page The page the whitelisting is active on | 188 * @param {Page} page The page the whitelisting is active on |
195 * @param {string} url The url of the whitelisted document | 189 * @param {string} url The url of the whitelisted document |
196 * @param {number} typeMask The bit mask of whitelisting types checked fo
r | 190 * @param {number} typeMask The bit mask of whitelisting types checked |
197 * @param {string} docDomain The IDN-decoded hostname of the parent docume
nt | 191 * for |
| 192 * @param {string} docDomain The IDN-decoded hostname of the parent |
| 193 * document |
198 * @param {WhitelistFilter} filter The matched whitelisting filter | 194 * @param {WhitelistFilter} filter The matched whitelisting filter |
199 */ | 195 */ |
200 exports.logWhitelistedDocument = function(page, url, typeMask, docDomain, filter
) | 196 exports.logWhitelistedDocument = function(page, url, typeMask, docDomain, |
| 197 filter) |
201 { | 198 { |
202 let panel = getActivePanel(page); | 199 let panel = getActivePanel(page); |
203 if (panel) | 200 if (panel) |
204 { | 201 { |
205 for (let type of nonRequestTypes) | 202 for (let type of nonRequestTypes) |
206 { | 203 { |
207 if (typeMask & filter.contentType & RegExpFilter.typeMap[type]) | 204 if (typeMask & filter.contentType & RegExpFilter.typeMap[type]) |
208 addRecord(panel, {url: url, type: type, docDomain: docDomain}, filter); | 205 addRecord(panel, {url, type, docDomain}, filter); |
209 } | 206 } |
210 } | 207 } |
211 }; | 208 }; |
212 | 209 |
213 /** | 210 /** |
214 * Checks whether a page is inspected by the devtools panel. | 211 * Checks whether a page is inspected by the devtools panel. |
215 * | 212 * |
216 * @param {Page} page | 213 * @param {Page} page |
217 * @return {boolean} | 214 * @return {boolean} |
218 */ | 215 */ |
219 exports.hasPanel = function(page) | 216 exports.hasPanel = function(page) |
220 { | 217 { |
221 return page.id in panels; | 218 return page.id in panels; |
222 }; | 219 }; |
223 | 220 |
224 function onBeforeRequest(details) | 221 function onBeforeRequest(details) |
225 { | 222 { |
226 let panel = panels[details.tabId]; | 223 let panel = panels[details.tabId]; |
227 | 224 |
228 // Clear the devtools panel and reload the inspected tab without caching | 225 // Clear the devtools panel and reload the inspected tab without caching |
229 // when a new request is issued. However, make sure that we don't end up | 226 // when a new request is issued. However, make sure that we don't end up |
230 // in an infinite recursion if we already triggered a reload. | 227 // in an infinite recursion if we already triggered a reload. |
231 if (panel.reloading) | 228 if (panel.reloading) |
232 { | |
233 panel.reloading = false; | 229 panel.reloading = false; |
234 } | |
235 else | 230 else |
236 { | 231 { |
237 panel.records = []; | 232 panel.records = []; |
238 panel.port.postMessage({type: "reset"}); | 233 panel.port.postMessage({type: "reset"}); |
239 | 234 |
240 // We can't repeat the request if it isn't a GET request. Chrome would | 235 // We can't repeat the request if it isn't a GET request. Chrome would |
241 // prompt the user to confirm reloading the page, and POST requests are | 236 // prompt the user to confirm reloading the page, and POST requests are |
242 // known to cause issues on many websites if repeated. | 237 // known to cause issues on many websites if repeated. |
243 if (details.method == "GET") | 238 if (details.method == "GET") |
244 panel.reload = true; | 239 panel.reload = true; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 { | 327 { |
333 updateFilters([filter], false); | 328 updateFilters([filter], false); |
334 } | 329 } |
335 | 330 |
336 function onSubscriptionAdded(subscription) | 331 function onSubscriptionAdded(subscription) |
337 { | 332 { |
338 if (subscription instanceof SpecialSubscription) | 333 if (subscription instanceof SpecialSubscription) |
339 updateFilters(subscription.filters, true); | 334 updateFilters(subscription.filters, true); |
340 } | 335 } |
341 | 336 |
342 chrome.runtime.onConnect.addListener(port => | 337 chrome.runtime.onConnect.addListener(newPort => |
343 { | 338 { |
344 let match = port.name.match(/^devtools-(\d+)$/); | 339 let match = newPort.name.match(/^devtools-(\d+)$/); |
345 if (!match) | 340 if (!match) |
346 return; | 341 return; |
347 | 342 |
348 let inspectedTabId = parseInt(match[1], 10); | 343 let inspectedTabId = parseInt(match[1], 10); |
349 let localOnBeforeRequest = onBeforeRequest.bind(); | 344 let localOnBeforeRequest = onBeforeRequest.bind(); |
350 | 345 |
351 chrome.webRequest.onBeforeRequest.addListener( | 346 chrome.webRequest.onBeforeRequest.addListener( |
352 localOnBeforeRequest, | 347 localOnBeforeRequest, |
353 { | 348 { |
354 urls: ["<all_urls>"], | 349 urls: ["<all_urls>"], |
355 types: ["main_frame"], | 350 types: ["main_frame"], |
356 tabId: inspectedTabId | 351 tabId: inspectedTabId |
357 } | 352 } |
358 ); | 353 ); |
359 | 354 |
360 if (!hasPanels()) | 355 if (!hasPanels()) |
361 { | 356 { |
362 ext.pages.onLoading.addListener(onLoading); | 357 ext.pages.onLoading.addListener(onLoading); |
363 FilterNotifier.on("filter.added", onFilterAdded); | 358 FilterNotifier.on("filter.added", onFilterAdded); |
364 FilterNotifier.on("filter.removed", onFilterRemoved); | 359 FilterNotifier.on("filter.removed", onFilterRemoved); |
365 FilterNotifier.on("subscription.added", onSubscriptionAdded); | 360 FilterNotifier.on("subscription.added", onSubscriptionAdded); |
366 } | 361 } |
367 | 362 |
368 port.onDisconnect.addListener(() => | 363 newPort.onDisconnect.addListener(() => |
369 { | 364 { |
370 delete panels[inspectedTabId]; | 365 delete panels[inspectedTabId]; |
371 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); | 366 chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); |
372 | 367 |
373 if (!hasPanels()) | 368 if (!hasPanels()) |
374 { | 369 { |
375 ext.pages.onLoading.removeListener(onLoading); | 370 ext.pages.onLoading.removeListener(onLoading); |
376 FilterNotifier.off("filter.added", onFilterAdded); | 371 FilterNotifier.off("filter.added", onFilterAdded); |
377 FilterNotifier.off("filter.removed", onFilterRemoved); | 372 FilterNotifier.off("filter.removed", onFilterRemoved); |
378 FilterNotifier.off("subscription.added", onSubscriptionAdded); | 373 FilterNotifier.off("subscription.added", onSubscriptionAdded); |
379 } | 374 } |
380 }); | 375 }); |
381 | 376 |
382 panels[inspectedTabId] = {port: port, records: []}; | 377 panels[inspectedTabId] = {port: newPort, records: []}; |
383 }); | 378 }); |
384 | 379 |
385 port.on("devtools.traceElemHide", (message, sender) => | 380 port.on("devtools.traceElemHide", (message, sender) => |
386 { | 381 { |
387 logHiddenElements( | 382 logHiddenElements( |
388 sender.page, message.selectors, | 383 sender.page, message.selectors, |
389 extractHostFromFrame(sender.frame) | 384 extractHostFromFrame(sender.frame) |
390 ); | 385 ); |
391 }); | 386 }); |
OLD | NEW |