Index: lib/devtools.js |
=================================================================== |
--- a/lib/devtools.js |
+++ b/lib/devtools.js |
@@ -1,6 +1,6 @@ |
/* |
* This file is part of Adblock Plus <https://adblockplus.org/>, |
- * Copyright (C) 2006-2016 Eyeo GmbH |
+ * Copyright (C) 2006-2017 eyeo GmbH |
* |
* Adblock Plus is free software: you can redistribute it and/or modify |
* it under the terms of the GNU General Public License version 3 as |
@@ -17,7 +17,9 @@ |
"use strict"; |
-const {RegExpFilter, WhitelistFilter, ElemHideFilter} = require("filterClasses"); |
+const {RegExpFilter, |
+ WhitelistFilter, |
+ ElemHideFilter} = require("filterClasses"); |
const {SpecialSubscription} = require("subscriptionClasses"); |
const {FilterStorage} = require("filterStorage"); |
const {defaultMatcher} = require("matcher"); |
@@ -30,17 +32,12 @@ |
// Mapping of inspected tabs to their devpanel page |
// and recorded items. We can't use a PageMap here, |
// because data must persist after navigation/reload. |
-let panels = Object.create(null); |
- |
-function hasPanels() |
-{ |
- return Object.keys(panels).length > 0; |
-} |
+let panels = new Map(); |
function getActivePanel(page) |
{ |
- let panel = panels[page.id]; |
- if(panel && !panel.reload && !panel.reloading) |
+ let panel = panels.get(page.id); |
+ if (panel && !panel.reload && !panel.reloading) |
return panel; |
return null; |
} |
@@ -67,7 +64,7 @@ |
return { |
text: filter.text, |
whitelisted: filter instanceof WhitelistFilter, |
- userDefined: userDefined, |
+ userDefined, |
subscription: subscriptionTitle |
}; |
} |
@@ -75,16 +72,17 @@ |
function hasRecord(panel, request, filter) |
{ |
return panel.records.some(record => |
- record.request.url == request.url && |
+ record.request.url == request.url && |
record.request.docDomain == request.docDomain && |
// Ignore partial (e.g. ELEMHIDE) whitelisting if there is already |
// a DOCUMENT exception which disables all means of blocking. |
- (record.request.type == "DOCUMENT" ? nonRequestTypes.indexOf(request.type) != -1 |
- : record.request.type == request.type) && |
+ (record.request.type == "DOCUMENT" ? |
+ nonRequestTypes.includes(request.type) : |
+ record.request.type == request.type) && |
// Matched element hiding filters don't relate to a particular request, |
- // so we also have to match the CSS selector in order to distinguish them. |
+ // so we have to compare the selector in order to avoid duplicates. |
(record.filter && record.filter.selector) == (filter && filter.selector) |
); |
} |
@@ -95,14 +93,11 @@ |
{ |
panel.port.postMessage({ |
type: "add-record", |
- request: request, |
+ request, |
filter: getFilterInfo(filter) |
}); |
- panel.records.push({ |
- request: request, |
- filter: filter |
- }); |
+ panel.records.push({request, filter}); |
} |
} |
@@ -125,10 +120,12 @@ |
* @param {string} url The URL of the request |
* @param {string} type The request type |
* @param {string} docDomain The IDN-decoded hostname of the document |
- * @param {boolean} thirdParty Whether the origin of the request and document differs |
+ * @param {boolean} thirdParty Whether the origin of the request and |
+ * document differs |
* @param {?string} sitekey The active sitekey if there is any |
* @param {?boolean} specificOnly Whether generic filters should be ignored |
- * @param {?BlockingFilter} filter The matched filter or null if there is no match |
+ * @param {?BlockingFilter} filter The matched filter or null if there is no |
+ * match |
*/ |
exports.logRequest = function(page, url, type, docDomain, |
thirdParty, sitekey, |
@@ -137,15 +134,7 @@ |
let panel = getActivePanel(page); |
if (panel) |
{ |
- let request = { |
- url: url, |
- type: type, |
- docDomain: docDomain, |
- thirdParty: thirdParty, |
- sitekey: sitekey, |
- specificOnly: specificOnly |
- }; |
- |
+ let request = {url, type, docDomain, thirdParty, sitekey, specificOnly}; |
addRecord(panel, request, filter); |
} |
}; |
@@ -154,12 +143,14 @@ |
* Logs active element hiding filters to the devtools panel. |
* |
* @param {Page} page The page the elements were hidden on |
- * @param {string[]} selectors The CSS selectors of active elemhide filters |
+ * @param {string[]} selectors The selectors of applied ElemHideFilters |
+ * @param {string[]} filters The text of applied ElemHideEmulationFilters |
* @param {string} docDomain The IDN-decoded hostname of the document |
*/ |
-function logHiddenElements(page, selectors, docDomain) |
+function logHiddenElements(page, selectors, filters, docDomain) |
{ |
let panel = getActivePanel(page); |
+ if (panel) |
{ |
for (let subscription of FilterStorage.subscriptions) |
{ |
@@ -168,18 +159,20 @@ |
for (let filter of subscription.filters) |
{ |
- if (!(filter instanceof ElemHideFilter)) |
- continue; |
- if (selectors.indexOf(filter.selector) == -1) |
- continue; |
- if (!filter.isActiveOnDomain(docDomain)) |
- continue; |
+ // We only know the exact filter in case of element hiding emulation. |
+ // For regular element hiding filters, the content script only knows |
+ // the selector, so we have to find a filter that has an identical |
+ // selector and is active on the domain the match was reported from. |
+ let isActiveElemHideFilter = filter instanceof ElemHideFilter && |
+ selectors.includes(filter.selector) && |
+ filter.isActiveOnDomain(docDomain); |
- addRecord(panel, {type: "ELEMHIDE", docDomain: docDomain}, filter); |
+ if (isActiveElemHideFilter || filters.includes(filter.text)) |
+ addRecord(panel, {type: "ELEMHIDE", docDomain}, filter); |
} |
} |
} |
-}; |
+} |
/** |
* Logs a whitelisting filter, that disables (some kind of) |
@@ -187,11 +180,14 @@ |
* |
* @param {Page} page The page the whitelisting is active on |
* @param {string} url The url of the whitelisted document |
- * @param {number} typeMask The bit mask of whitelisting types checked for |
- * @param {string} docDomain The IDN-decoded hostname of the parent document |
+ * @param {number} typeMask The bit mask of whitelisting types checked |
+ * for |
+ * @param {string} docDomain The IDN-decoded hostname of the parent |
+ * document |
* @param {WhitelistFilter} filter The matched whitelisting filter |
*/ |
-exports.logWhitelistedDocument = function(page, url, typeMask, docDomain, filter) |
+exports.logWhitelistedDocument = function(page, url, typeMask, docDomain, |
+ filter) |
{ |
let panel = getActivePanel(page); |
if (panel) |
@@ -199,7 +195,7 @@ |
for (let type of nonRequestTypes) |
{ |
if (typeMask & filter.contentType & RegExpFilter.typeMap[type]) |
- addRecord(panel, {url: url, type: type, docDomain: docDomain}, filter); |
+ addRecord(panel, {url, type, docDomain}, filter); |
} |
} |
}; |
@@ -212,12 +208,12 @@ |
*/ |
exports.hasPanel = function(page) |
{ |
- return page.id in panels; |
+ return panels.has(page.id); |
}; |
function onBeforeRequest(details) |
{ |
- let panel = panels[details.tabId]; |
+ let panel = panels.get(details.tabId); |
// Clear the devtools panel and reload the inspected tab without caching |
// when a new request is issued. However, make sure that we don't end up |
@@ -242,7 +238,7 @@ |
function onLoading(page) |
{ |
let tabId = page.id; |
- let panel = panels[tabId]; |
+ let panel = panels.get(tabId); |
// Reloading the tab is the only way that allows bypassing all caches, in |
// order to see all requests in the devtools panel. Reloading must not be |
@@ -259,10 +255,8 @@ |
function updateFilters(filters, added) |
{ |
- for (let tabId in panels) |
+ for (let panel of panels.values()) |
{ |
- let panel = panels[tabId]; |
- |
for (let i = 0; i < panel.records.length; i++) |
{ |
let record = panel.records[i]; |
@@ -333,9 +327,9 @@ |
updateFilters(subscription.filters, true); |
} |
-chrome.runtime.onConnect.addListener(port => |
+chrome.runtime.onConnect.addListener(newPort => |
{ |
- let match = port.name.match(/^devtools-(\d+)$/); |
+ let match = newPort.name.match(/^devtools-(\d+)$/); |
if (!match) |
return; |
@@ -345,13 +339,13 @@ |
chrome.webRequest.onBeforeRequest.addListener( |
localOnBeforeRequest, |
{ |
- urls: ["<all_urls>"], |
+ urls: ["http://*/*", "https://*/*"], |
types: ["main_frame"], |
tabId: inspectedTabId |
} |
); |
- if (!hasPanels()) |
+ if (panels.size == 0) |
{ |
ext.pages.onLoading.addListener(onLoading); |
FilterNotifier.on("filter.added", onFilterAdded); |
@@ -359,12 +353,12 @@ |
FilterNotifier.on("subscription.added", onSubscriptionAdded); |
} |
- port.onDisconnect.addListener(() => |
+ newPort.onDisconnect.addListener(() => |
{ |
- delete panels[inspectedTabId]; |
+ panels.delete(inspectedTabId); |
chrome.webRequest.onBeforeRequest.removeListener(localOnBeforeRequest); |
- if (!hasPanels()) |
+ if (panels.size == 0) |
{ |
ext.pages.onLoading.removeListener(onLoading); |
FilterNotifier.off("filter.added", onFilterAdded); |
@@ -373,13 +367,13 @@ |
} |
}); |
- panels[inspectedTabId] = {port: port, records: []}; |
+ panels.set(inspectedTabId, {port: newPort, records: []}); |
}); |
port.on("devtools.traceElemHide", (message, sender) => |
{ |
logHiddenElements( |
- sender.page, message.selectors, |
+ sender.page, message.selectors, message.filters, |
extractHostFromFrame(sender.frame) |
); |
}); |