From 42938c9b63a42b1c7a42a98c69a736130b32bbd9 Mon Sep 17 00:00:00 2001 From: gorhill Date: Mon, 26 Sep 2016 13:45:55 -0400 Subject: [PATCH] code review re. #1954: also support implicit entity-based scriptlets --- src/js/cosmetic-filtering.js | 51 +++++++++++++++--------------------- 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/src/js/cosmetic-filtering.js b/src/js/cosmetic-filtering.js index ee530ee74..84515a465 100644 --- a/src/js/cosmetic-filtering.js +++ b/src/js/cosmetic-filtering.js @@ -1304,41 +1304,31 @@ FilterContainer.prototype.createUserScriptRule = function(hash, hostname, select // 14 -1 FilterContainer.prototype.retrieveUserScripts = function(domain, hostname) { - if ( this.userScriptCount === 0 ) { - return; - } + if ( this.userScriptCount === 0 ) { return; } var reng = µb.redirectEngine; - if ( !reng ) { - return; - } + if ( !reng ) { return; } var out = [], - scripts = Object.create(null), + scripts = new Map(), pos = domain.indexOf('.'), - entity = pos !== -1 ? domain.slice(0, pos) + '.*' : '', - token, content; + entity = pos !== -1 ? domain.slice(0, pos) + '.*' : ''; // Implicit var hn = hostname; for (;;) { - token = hn + '.js'; - if ( - (scripts[token] === undefined) && - (content = reng.resourceContentFromName(token, 'application/javascript')) - ) { - scripts[token] = out.length; - out.push(content); - } + this._lookupUserScript(scripts, hn + '.js', reng, out); if ( hn === domain ) { break; } pos = hn.indexOf('.'); if ( pos === -1 ) { break; } hn = hn.slice(pos + 1); } + if ( entity !== '' ) { + this._lookupUserScript(scripts, entity + '.js', reng, out); + } // Explicit (hash is domain). - var selectors = [], - selector, bucket; + var selectors = [], bucket; if ( (bucket = this.userScripts.get(domain)) ) { bucket.retrieve(hostname, selectors); } @@ -1347,15 +1337,7 @@ FilterContainer.prototype.retrieveUserScripts = function(domain, hostname) { } var i = selectors.length; while ( i-- ) { - selector = selectors[i]; - token = selector.slice(14, -1); - if ( - (scripts[token] === undefined) && - (content = reng.resourceContentFromName(token, 'application/javascript')) - ) { - scripts[token] = out.length; - out.push(content); - } + this._lookupUserScript(scripts, selectors[i].slice(14, -1), reng, out); } if ( out.length === 0 ) { @@ -1364,7 +1346,7 @@ FilterContainer.prototype.retrieveUserScripts = function(domain, hostname) { // Exceptions should be rare, so we check for exception only if there are // scriptlets returned. - var exceptions = [], j; + var exceptions = [], j, token; if ( (bucket = this.userScripts.get('!' + domain)) ) { bucket.retrieve(hostname, exceptions); } @@ -1374,7 +1356,7 @@ FilterContainer.prototype.retrieveUserScripts = function(domain, hostname) { i = exceptions.length; while ( i-- ) { token = exceptions[i].slice(14, -1); - if ( (j = scripts[token]) !== undefined ) { + if ( (j = scripts.get(token)) !== undefined ) { out[j] = '// User script "' + token + '" excepted.\n'; } } @@ -1382,6 +1364,15 @@ FilterContainer.prototype.retrieveUserScripts = function(domain, hostname) { return out.join('\n'); }; +FilterContainer.prototype._lookupUserScript = function(dict, token, reng, out) { + if ( dict.has(token) ) { return; } + var content = reng.resourceContentFromName(token, 'application/javascript'); + if ( content ) { + dict.set(token, out.length); + out.push(content); + } +}; + /******************************************************************************/ FilterContainer.prototype.toSelfie = function() {