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