Index: lib/elemHide.js |
diff --git a/lib/elemHide.js b/lib/elemHide.js |
index 9aa1ae3948540af980eda636d9530464c6951aa6..88aef4c93156a65a4b1558489c740f73a077e32a 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") |
- { |
- this._addToFiltersByDomain(filterKey, filterByKey[filterKey]); |
- filterKeyBySelector.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))) |
+ let unconditionalFilterForSelector = filterBySelector.get(selector); |
+ if (unconditionalFilterForSelector) |
{ |
- // The new filter's selector is unconditionally applied to all domains |
- filterKeyBySelector.set(filter.selector, key); |
+ this._addToFiltersByDomain(unconditionalFilterForSelector); |
+ filterBySelector.delete(selector); |
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) |
Manish Jethani
2018/05/04 11:59:55
Looking at the code above, the value returned by f
kzar
2018/05/04 12:06:10
Done.
|
+ { |
+ 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(); |
}, |
@@ -293,8 +252,11 @@ let ElemHide = exports.ElemHide = { |
selectors = this.getUnconditionalSelectors(); |
let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); |
- let seenFilters = new Set(); |
+ let excluded = new Set(); |
let currentDomain = domain ? domain.toUpperCase() : ""; |
+ |
+ // This code is a performance hot-spot, which is why we've made certain |
+ // micro-optimisations. Please be careful before making changes. |
while (true) |
{ |
if (specificOnly && currentDomain == "") |
@@ -303,14 +265,17 @@ 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)) |
- continue; |
- seenFilters.add(filterKey); |
- |
- if (filter && !this.getException(filter, domain)) |
+ if (!isIncluded) |
+ { |
+ excluded.add(filter); |
+ } |
+ else if ((excluded.size == 0 || !excluded.has(filter)) && |
+ !this.getException(filter, domain)) |
+ { |
selectors.push(filter.selector); |
+ } |
} |
} |