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 // 14 -1
FilterContainer.prototype.retrieveUserScripts = function(domain, hostname) { FilterContainer.prototype.retrieveUserScripts = function(domain, hostname) {
if ( this.userScriptCount === 0 ) { if ( this.userScriptCount === 0 ) { return; }
return;
}
var reng = µb.redirectEngine; var reng = µb.redirectEngine;
if ( !reng ) { if ( !reng ) { return; }
return;
}
var out = [], var out = [],
scripts = Object.create(null), scripts = new Map(),
pos = domain.indexOf('.'), pos = domain.indexOf('.'),
entity = pos !== -1 ? domain.slice(0, pos) + '.*' : '', entity = pos !== -1 ? domain.slice(0, pos) + '.*' : '';
token, content;
// Implicit // Implicit
var hn = hostname; var hn = hostname;
for (;;) { for (;;) {
token = hn + '.js'; this._lookupUserScript(scripts, hn + '.js', reng, out);
if (
(scripts[token] === undefined) &&
(content = reng.resourceContentFromName(token, 'application/javascript'))
) {
scripts[token] = out.length;
out.push(content);
}
if ( hn === domain ) { break; } if ( hn === domain ) { break; }
pos = hn.indexOf('.'); pos = hn.indexOf('.');
if ( pos === -1 ) { break; } if ( pos === -1 ) { break; }
hn = hn.slice(pos + 1); hn = hn.slice(pos + 1);
} }
if ( entity !== '' ) {
this._lookupUserScript(scripts, entity + '.js', reng, out);
}
// Explicit (hash is domain). // Explicit (hash is domain).
var selectors = [], var selectors = [], bucket;
selector, bucket;
if ( (bucket = this.userScripts.get(domain)) ) { if ( (bucket = this.userScripts.get(domain)) ) {
bucket.retrieve(hostname, selectors); bucket.retrieve(hostname, selectors);
} }
@ -1347,15 +1337,7 @@ FilterContainer.prototype.retrieveUserScripts = function(domain, hostname) {
} }
var i = selectors.length; var i = selectors.length;
while ( i-- ) { while ( i-- ) {
selector = selectors[i]; this._lookupUserScript(scripts, selectors[i].slice(14, -1), reng, out);
token = selector.slice(14, -1);
if (
(scripts[token] === undefined) &&
(content = reng.resourceContentFromName(token, 'application/javascript'))
) {
scripts[token] = out.length;
out.push(content);
}
} }
if ( out.length === 0 ) { 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 // Exceptions should be rare, so we check for exception only if there are
// scriptlets returned. // scriptlets returned.
var exceptions = [], j; var exceptions = [], j, token;
if ( (bucket = this.userScripts.get('!' + domain)) ) { if ( (bucket = this.userScripts.get('!' + domain)) ) {
bucket.retrieve(hostname, exceptions); bucket.retrieve(hostname, exceptions);
} }
@ -1374,7 +1356,7 @@ FilterContainer.prototype.retrieveUserScripts = function(domain, hostname) {
i = exceptions.length; i = exceptions.length;
while ( i-- ) { while ( i-- ) {
token = exceptions[i].slice(14, -1); token = exceptions[i].slice(14, -1);
if ( (j = scripts[token]) !== undefined ) { if ( (j = scripts.get(token)) !== undefined ) {
out[j] = '// User script "' + token + '" excepted.\n'; out[j] = '// User script "' + token + '" excepted.\n';
} }
} }
@ -1382,6 +1364,15 @@ FilterContainer.prototype.retrieveUserScripts = function(domain, hostname) {
return out.join('\n'); 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() { FilterContainer.prototype.toSelfie = function() {