Index: lib/elemHide.js |
diff --git a/lib/elemHide.js b/lib/elemHide.js |
index 9aa1ae3948540af980eda636d9530464c6951aa6..1c15b9933169450560460f9fafaa1b240533b4a3 100644 |
--- a/lib/elemHide.js |
+++ b/lib/elemHide.js |
@@ -25,48 +25,36 @@ const {ElemHideException} = require("./filterClasses"); |
const {FilterNotifier} = require("./filterNotifier"); |
/** |
- * Lookup table, filters by their associated key |
- * @type {Filter[]} |
- */ |
-let filterByKey = []; |
- |
-/** |
- * Lookup table, keys of the filters by filter |
- * @type {Map.<Filter,number>} |
- */ |
-let keyByFilter = new Map(); |
- |
-/** |
- * Nested lookup table, filter (or false if inactive) by filter key by domain. |
+ * Lookup table, active flag, by filter by domain. |
* (Only contains filters that aren't unconditionally matched for all domains.) |
- * @type {Map.<string,Map.<number,(Filter|boolean)>>} |
+ * @type {Map.<string,Map.<Filter,boolean>>} |
*/ |
let filtersByDomain = new Map(); |
/** |
- * Lookup table, filter key by selector. (Only used for selectors that are |
+ * Lookup table, filter by selector. (Only used for selectors that are |
* unconditionally matched for all domains.) |
- * @type {Map.<string,number>} |
+ * @type {Map.<string,Filter>} |
*/ |
-let filterKeyBySelector = new Map(); |
+let filterBySelector = new Map(); |
/** |
- * This array caches the keys of filterKeyBySelector table (selectors which |
- * unconditionally apply on all domains). It will be null if the cache needs to |
- * be rebuilt. |
+ * This array caches the keys of filterBySelector table (selectors |
+ * which unconditionally apply on all domains). It will be null if the |
+ * cache needs to be rebuilt. |
*/ |
let unconditionalSelectors = null; |
/** |
- * Object to be used instead when a filter has a blank domains property. |
+ * Map to be used instead when a filter has a blank domains property. |
*/ |
let defaultDomains = new Map([["", true]]); |
/** |
- * Set containing known element hiding exceptions |
- * @type {Set.<string>} |
+ * Set containing known element hiding and exception filters |
+ * @type {Set.<ElemHideBase>} |
*/ |
-let knownExceptions = new Set(); |
+let knownFilters = new Set(); |
/** |
* Lookup table, lists of element hiding exceptions by selector |
@@ -84,39 +72,42 @@ let ElemHide = exports.ElemHide = { |
*/ |
clear() |
{ |
- for (let collection of [keyByFilter, filtersByDomain, filterKeyBySelector, |
- knownExceptions, exceptions]) |
+ for (let collection of [filtersByDomain, filterBySelector, |
+ knownFilters, exceptions]) |
{ |
collection.clear(); |
} |
- filterByKey = []; |
unconditionalSelectors = null; |
FilterNotifier.emit("elemhideupdate"); |
}, |
- _addToFiltersByDomain(key, filter) |
+ _addToFiltersByDomain(filter) |
{ |
let domains = filter.domains || defaultDomains; |
for (let [domain, isIncluded] of domains) |
{ |
+ // There's no need to note that a filter is generically disabled. |
+ if (!isIncluded && domain == "") |
+ continue; |
+ |
let filters = filtersByDomain.get(domain); |
if (!filters) |
filtersByDomain.set(domain, filters = new Map()); |
- filters.set(key, isIncluded ? filter : false); |
+ filters.set(filter, isIncluded); |
} |
}, |
/** |
* Add a new element hiding filter |
- * @param {ElemHideFilter} filter |
+ * @param {ElemHideBase} filter |
*/ |
add(filter) |
{ |
+ if (knownFilters.has(filter)) |
+ return; |
+ |
if (filter instanceof ElemHideException) |
{ |
- if (knownExceptions.has(filter.text)) |
- return; |
- |
let {selector} = filter; |
let list = exceptions.get(selector); |
if (list) |
@@ -127,88 +118,66 @@ let ElemHide = exports.ElemHide = { |
// If this is the first exception for a previously unconditionally |
// applied element hiding selector we need to take care to update the |
// lookups. |
- let filterKey = filterKeyBySelector.get(selector); |
- if (typeof filterKey != "undefined") |
+ let unconditionalFilterForSelector = filterBySelector.get(selector); |
+ if (unconditionalFilterForSelector) |
{ |
- this._addToFiltersByDomain(filterKey, filterByKey[filterKey]); |
- filterKeyBySelector.delete(selector); |
+ this._addToFiltersByDomain(unconditionalFilterForSelector); |
+ filterBySelector.delete(selector); |
unconditionalSelectors = null; |
} |
- |
- knownExceptions.add(filter.text); |
} |
- else |
- { |
- if (keyByFilter.has(filter)) |
- return; |
- |
- let key = filterByKey.push(filter) - 1; |
- keyByFilter.set(filter, key); |
- |
- if (!(filter.domains || exceptions.has(filter.selector))) |
- { |
- // The new filter's selector is unconditionally applied to all domains |
- filterKeyBySelector.set(filter.selector, key); |
- unconditionalSelectors = null; |
- } |
- else |
- { |
- // The new filter's selector only applies to some domains |
- this._addToFiltersByDomain(key, filter); |
- } |
- } |
- |
- FilterNotifier.emit("elemhideupdate"); |
- }, |
- |
- _removeFilterKey(key, filter) |
- { |
- if (filterKeyBySelector.get(filter.selector) == key) |
+ else if (!(filter.domains || exceptions.has(filter.selector))) |
{ |
- filterKeyBySelector.delete(filter.selector); |
+ // The new filter's selector is unconditionally applied to all domains |
+ filterBySelector.set(filter.selector, filter); |
unconditionalSelectors = null; |
- return; |
} |
- |
- // We haven't found this filter in unconditional filters, look in |
- // filtersByDomain. |
- let domains = filter.domains || defaultDomains; |
- for (let domain of domains.keys()) |
+ else |
{ |
- let filters = filtersByDomain.get(domain); |
- if (filters) |
- filters.delete(key); |
+ // The new filter's selector only applies to some domains |
+ this._addToFiltersByDomain(filter); |
} |
+ |
+ knownFilters.add(filter); |
+ FilterNotifier.emit("elemhideupdate"); |
}, |
/** |
* Removes an element hiding filter |
- * @param {ElemHideFilter} filter |
+ * @param {ElemHideBase} filter |
*/ |
remove(filter) |
{ |
+ if (!knownFilters.has(filter)) |
+ return; |
+ |
+ // Whitelisting filters |
if (filter instanceof ElemHideException) |
{ |
- if (!knownExceptions.has(filter.text)) |
- return; |
- |
let list = exceptions.get(filter.selector); |
let index = list.indexOf(filter); |
if (index >= 0) |
list.splice(index, 1); |
- knownExceptions.delete(filter.text); |
} |
+ // Unconditially applied element hiding filters |
+ else if (filterBySelector.get(filter.selector) === filter) |
+ { |
+ filterBySelector.delete(filter.selector); |
+ unconditionalSelectors = null; |
+ } |
+ // Conditionally applied element hiding filters |
else |
{ |
- let key = keyByFilter.get(filter); |
- if (typeof key == "undefined") |
- return; |
- |
- delete filterByKey[key]; |
- keyByFilter.delete(filter); |
- this._removeFilterKey(key, filter); |
+ let domains = filter.domains || defaultDomains; |
+ for (let domain of domains.keys()) |
+ { |
+ let filters = filtersByDomain.get(domain); |
+ if (filters) |
+ filters.delete(filter); |
+ } |
} |
+ knownFilters.delete(filter); |
FilterNotifier.emit("elemhideupdate"); |
}, |
@@ -234,16 +203,6 @@ let ElemHide = exports.ElemHide = { |
return null; |
}, |
- /** |
- * Retrieves an element hiding filter by the corresponding protocol key |
- * @param {number} key |
- * @return {Filter} |
- */ |
- getFilterByKey(key) |
- { |
- return (key in filterByKey ? filterByKey[key] : null); |
- }, |
- |
/** |
* Returns a list of selectors that apply on each website unconditionally. |
* @returns {string[]} |
@@ -251,7 +210,7 @@ let ElemHide = exports.ElemHide = { |
getUnconditionalSelectors() |
{ |
if (!unconditionalSelectors) |
- unconditionalSelectors = [...filterKeyBySelector.keys()]; |
+ unconditionalSelectors = [...filterBySelector.keys()]; |
return unconditionalSelectors.slice(); |
}, |
@@ -303,13 +262,13 @@ let ElemHide = exports.ElemHide = { |
let filters = filtersByDomain.get(currentDomain); |
if (filters) |
{ |
- for (let [filterKey, filter] of filters) |
+ for (let [filter, isIncluded] of filters) |
{ |
- if (seenFilters.has(filterKey)) |
+ if (seenFilters.has(filter)) |
continue; |
- seenFilters.add(filterKey); |
+ seenFilters.add(filter); |
- if (filter && !this.getException(filter, domain)) |
+ if (isIncluded && !this.getException(filter, domain)) |
selectors.push(filter.selector); |
} |
} |