| Left: | ||
| Right: |
| 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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 13 matching lines...) Expand all Loading... | |
| 24 const {FilterStorage} = require("filterStorage"); | 24 const {FilterStorage} = require("filterStorage"); |
| 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", | 30 const nonRequestTypes = ["DOCUMENT", "ELEMHIDE", |
| 31 "GENERICBLOCK", "GENERICHIDE", "CSP"]; | 31 "GENERICBLOCK", "GENERICHIDE", "CSP"]; |
| 32 | 32 |
| 33 // Mapping of inspected tabs to their devpanel page | 33 // Mapping of inspected tabs to their devpanel page |
| 34 // and recorded items. We can't use a PageMap here, | 34 // and recorded items. We can't use a PageMap here, |
|
kzar
2018/04/03 17:14:03
Maybe remove/change this comment since it wouldn't
Sebastian Noack
2018/04/04 01:50:33
Well spotted. Done.
| |
| 35 // because data must persist after navigation/reload. | 35 // because data must persist after navigation/reload. |
| 36 let panels = new Map(); | 36 let panels = new Map(); |
| 37 | 37 |
| 38 function isActivePanel(panel) | 38 function isActivePanel(panel) |
| 39 { | 39 { |
| 40 return panel && !panel.reload && !panel.reloading; | 40 return panel && !panel.reload && !panel.reloading; |
| 41 } | 41 } |
| 42 | 42 |
| 43 function getActivePanel(page) | 43 function getActivePanel(tabId) |
| 44 { | 44 { |
| 45 let panel = panels.get(page.id); | 45 let panel = panels.get(tabId); |
| 46 if (isActivePanel(panel)) | 46 if (isActivePanel(panel)) |
| 47 return panel; | 47 return panel; |
| 48 return null; | 48 return null; |
| 49 } | 49 } |
| 50 | 50 |
| 51 function getFilterInfo(filter) | 51 function getFilterInfo(filter) |
| 52 { | 52 { |
| 53 if (!filter) | 53 if (!filter) |
| 54 return null; | 54 return null; |
| 55 | 55 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 request.docDomain, | 115 request.docDomain, |
| 116 request.thirdParty, | 116 request.thirdParty, |
| 117 request.sitekey, | 117 request.sitekey, |
| 118 request.specificOnly | 118 request.specificOnly |
| 119 ); | 119 ); |
| 120 } | 120 } |
| 121 | 121 |
| 122 /** | 122 /** |
| 123 * Logs a request to the devtools panel. | 123 * Logs a request to the devtools panel. |
| 124 * | 124 * |
| 125 * @param {?Page} page The page the request occured on or null if | 125 * @param {number[]} tabIds The tabIds associated with the request |
| 126 * the request isn't associated with a page | |
| 127 * @param {string} url The URL of the request | 126 * @param {string} url The URL of the request |
| 128 * @param {string} type The request type | 127 * @param {string} type The request type |
| 129 * @param {string} docDomain The IDN-decoded hostname of the document | 128 * @param {string} docDomain The IDN-decoded hostname of the document |
| 130 * @param {boolean} thirdParty Whether the origin of the request and | 129 * @param {boolean} thirdParty Whether the origin of the request and |
| 131 * document differs | 130 * document differs |
| 132 * @param {?string} sitekey The active sitekey if there is any | 131 * @param {?string} sitekey The active sitekey if there is any |
| 133 * @param {?boolean} specificOnly Whether generic filters should be ignored | 132 * @param {?boolean} specificOnly Whether generic filters should be ignored |
| 134 * @param {?BlockingFilter} filter The matched filter or null if there is no | 133 * @param {?BlockingFilter} filter The matched filter or null if there is no |
| 135 * match | 134 * match |
| 136 */ | 135 */ |
| 137 exports.logRequest = function(page, url, type, docDomain, | 136 exports.logRequest = function(tabIds, url, type, docDomain, |
| 138 thirdParty, sitekey, | 137 thirdParty, sitekey, |
| 139 specificOnly, filter) | 138 specificOnly, filter) |
| 140 { | 139 { |
| 141 if (panels.size == 0) | 140 if (panels.size == 0) |
| 142 return; | 141 return; |
| 143 | 142 |
| 144 let request = {url, type, docDomain, thirdParty, sitekey, specificOnly}; | 143 let request = {url, type, docDomain, thirdParty, sitekey, specificOnly}; |
| 145 for (let [tabId, panel] of panels) | 144 for (let [tabId, panel] of panels) |
| 146 if ((!page || page.id == tabId) && isActivePanel(panel)) | 145 if ((tabIds.length == 0 || tabIds.includes(tabId)) && isActivePanel(panel)) |
| 147 addRecord(panel, request, filter); | 146 addRecord(panel, request, filter); |
| 148 }; | 147 }; |
| 149 | 148 |
| 150 /** | 149 /** |
| 151 * Logs active element hiding filters to the devtools panel. | 150 * Logs active element hiding filters to the devtools panel. |
| 152 * | 151 * |
| 153 * @param {Page} page The page the elements were hidden on | 152 * @param {number} tabId The ID of the tab, the elements were hidden in |
| 154 * @param {string[]} selectors The selectors of applied ElemHideFilters | 153 * @param {string[]} selectors The selectors of applied ElemHideFilters |
| 155 * @param {string[]} filters The text of applied ElemHideEmulationFilters | 154 * @param {string[]} filters The text of applied ElemHideEmulationFilters |
| 156 * @param {string} docDomain The IDN-decoded hostname of the document | 155 * @param {string} docDomain The IDN-decoded hostname of the document |
| 157 */ | 156 */ |
| 158 function logHiddenElements(page, selectors, filters, docDomain) | 157 function logHiddenElements(tabId, selectors, filters, docDomain) |
| 159 { | 158 { |
| 160 let panel = getActivePanel(page); | 159 let panel = getActivePanel(tabId); |
| 161 if (panel) | 160 if (panel) |
| 162 { | 161 { |
| 163 for (let subscription of FilterStorage.subscriptions) | 162 for (let subscription of FilterStorage.subscriptions) |
| 164 { | 163 { |
| 165 if (subscription.disabled) | 164 if (subscription.disabled) |
| 166 continue; | 165 continue; |
| 167 | 166 |
| 168 for (let filter of subscription.filters) | 167 for (let filter of subscription.filters) |
| 169 { | 168 { |
| 170 // We only know the exact filter in case of element hiding emulation. | 169 // We only know the exact filter in case of element hiding emulation. |
| 171 // For regular element hiding filters, the content script only knows | 170 // For regular element hiding filters, the content script only knows |
| 172 // the selector, so we have to find a filter that has an identical | 171 // the selector, so we have to find a filter that has an identical |
| 173 // selector and is active on the domain the match was reported from. | 172 // selector and is active on the domain the match was reported from. |
| 174 let isActiveElemHideFilter = filter instanceof ElemHideFilter && | 173 let isActiveElemHideFilter = filter instanceof ElemHideFilter && |
| 175 selectors.includes(filter.selector) && | 174 selectors.includes(filter.selector) && |
| 176 filter.isActiveOnDomain(docDomain); | 175 filter.isActiveOnDomain(docDomain); |
| 177 | 176 |
| 178 if (isActiveElemHideFilter || filters.includes(filter.text)) | 177 if (isActiveElemHideFilter || filters.includes(filter.text)) |
| 179 addRecord(panel, {type: "ELEMHIDE", docDomain}, filter); | 178 addRecord(panel, {type: "ELEMHIDE", docDomain}, filter); |
| 180 } | 179 } |
| 181 } | 180 } |
| 182 } | 181 } |
| 183 } | 182 } |
| 184 | 183 |
| 185 /** | 184 /** |
| 186 * Logs a whitelisting filter, that disables (some kind of) | 185 * Logs a whitelisting filter, that disables (some kind of) |
| 187 * blocking for a particular document, to the devtools panel. | 186 * blocking for a particular document, to the devtools panel. |
| 188 * | 187 * |
| 189 * @param {Page} page The page the whitelisting is active on | 188 * @param {number} tabId The tabId the whitelisting is active for |
| 190 * @param {string} url The url of the whitelisted document | 189 * @param {string} url The url of the whitelisted document |
| 191 * @param {number} typeMask The bit mask of whitelisting types checked | 190 * @param {number} typeMask The bit mask of whitelisting types checked |
| 192 * for | 191 * for |
| 193 * @param {string} docDomain The IDN-decoded hostname of the parent | 192 * @param {string} docDomain The IDN-decoded hostname of the parent |
| 194 * document | 193 * document |
| 195 * @param {WhitelistFilter} filter The matched whitelisting filter | 194 * @param {WhitelistFilter} filter The matched whitelisting filter |
| 196 */ | 195 */ |
| 197 exports.logWhitelistedDocument = function(page, url, typeMask, docDomain, | 196 exports.logWhitelistedDocument = function(tabId, url, typeMask, docDomain, |
| 198 filter) | 197 filter) |
| 199 { | 198 { |
| 200 let panel = getActivePanel(page); | 199 let panel = getActivePanel(tabId); |
| 201 if (panel) | 200 if (panel) |
| 202 { | 201 { |
| 203 for (let type of nonRequestTypes) | 202 for (let type of nonRequestTypes) |
| 204 { | 203 { |
| 205 if (typeMask & filter.contentType & RegExpFilter.typeMap[type]) | 204 if (typeMask & filter.contentType & RegExpFilter.typeMap[type]) |
| 206 addRecord(panel, {url, type, docDomain}, filter); | 205 addRecord(panel, {url, type, docDomain}, filter); |
| 207 } | 206 } |
| 208 } | 207 } |
| 209 }; | 208 }; |
| 210 | 209 |
| 211 /** | 210 /** |
| 212 * Checks whether a page is inspected by the devtools panel. | 211 * Checks whether a tab is inspected by the devtools panel. |
| 213 * | 212 * |
| 214 * @param {Page} page | 213 * @param {number} tabId |
| 215 * @return {boolean} | 214 * @return {boolean} |
| 216 */ | 215 */ |
| 217 exports.hasPanel = function(page) | 216 exports.hasPanel = function(tabId) |
| 218 { | 217 { |
| 219 return panels.has(page.id); | 218 return panels.has(tabId); |
| 220 }; | 219 }; |
| 221 | 220 |
| 222 function onBeforeRequest(details) | 221 function onBeforeRequest(details) |
| 223 { | 222 { |
| 224 let panel = panels.get(details.tabId); | 223 let panel = panels.get(details.tabId); |
| 225 | 224 |
| 226 // Clear the devtools panel and reload the inspected tab without caching | 225 // Clear the devtools panel and reload the inspected tab without caching |
| 227 // 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 |
| 228 // in an infinite recursion if we already triggered a reload. | 227 // in an infinite recursion if we already triggered a reload. |
| 229 if (panel.reloading) | 228 if (panel.reloading) |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 348 localOnBeforeRequest, | 347 localOnBeforeRequest, |
| 349 { | 348 { |
| 350 urls: ["http://*/*", "https://*/*"], | 349 urls: ["http://*/*", "https://*/*"], |
| 351 types: ["main_frame"], | 350 types: ["main_frame"], |
| 352 tabId: inspectedTabId | 351 tabId: inspectedTabId |
| 353 } | 352 } |
| 354 ); | 353 ); |
| 355 | 354 |
| 356 if (panels.size == 0) | 355 if (panels.size == 0) |
| 357 { | 356 { |
| 358 ext.pages.onLoading.addListener(onLoading); | 357 ext.pages.onLoading.addListener(onLoading); |
|
kzar
2018/04/03 17:14:03
What about these? Well I don't mind if we remove t
Sebastian Noack
2018/04/04 01:50:33
There is quite some magic around the ext.pages.onL
| |
| 359 FilterNotifier.on("filter.added", onFilterAdded); | 358 FilterNotifier.on("filter.added", onFilterAdded); |
| 360 FilterNotifier.on("filter.removed", onFilterRemoved); | 359 FilterNotifier.on("filter.removed", onFilterRemoved); |
| 361 FilterNotifier.on("subscription.added", onSubscriptionAdded); | 360 FilterNotifier.on("subscription.added", onSubscriptionAdded); |
| 362 } | 361 } |
| 363 | 362 |
| 364 newPort.onDisconnect.addListener(() => | 363 newPort.onDisconnect.addListener(() => |
| 365 { | 364 { |
| 366 panels.delete(inspectedTabId); | 365 panels.delete(inspectedTabId); |
| 367 browser.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); | 366 browser.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); |
| 368 | 367 |
| 369 if (panels.size == 0) | 368 if (panels.size == 0) |
| 370 { | 369 { |
| 371 ext.pages.onLoading.removeListener(onLoading); | 370 ext.pages.onLoading.removeListener(onLoading); |
| 372 FilterNotifier.off("filter.added", onFilterAdded); | 371 FilterNotifier.off("filter.added", onFilterAdded); |
| 373 FilterNotifier.off("filter.removed", onFilterRemoved); | 372 FilterNotifier.off("filter.removed", onFilterRemoved); |
| 374 FilterNotifier.off("subscription.added", onSubscriptionAdded); | 373 FilterNotifier.off("subscription.added", onSubscriptionAdded); |
| 375 } | 374 } |
| 376 }); | 375 }); |
| 377 | 376 |
| 378 panels.set(inspectedTabId, {port: newPort, records: []}); | 377 panels.set(inspectedTabId, {port: newPort, records: []}); |
| 379 }); | 378 }); |
| 380 | 379 |
| 381 port.on("devtools.traceElemHide", (message, sender) => | 380 port.on("devtools.traceElemHide", (message, sender) => |
| 382 { | 381 { |
| 383 logHiddenElements( | 382 logHiddenElements( |
| 384 sender.page, message.selectors, message.filters, | 383 sender.page.id, message.selectors, message.filters, |
| 385 extractHostFromFrame(sender.frame) | 384 extractHostFromFrame(sender.frame) |
| 386 ); | 385 ); |
| 387 }); | 386 }); |
| OLD | NEW |