code review re. #1954: also support implicit entity-based scriptlets

This commit is contained in:
gorhill 2016-09-26 13:45:55 -04:00
parent 56176c8eb7
commit 42938c9b63

View file

@ -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() {