1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-07 03:12:33 +01:00

Implement scriptlet token normalization

The goal is to be able to specify a scriptlet token
without the `.js` part at the end, because that part
is essentially redundant with the `+js` part of
the syntax.

When the next stable release is in widespread use
(to determine), scriptlet tokens will have to be
specified without the `.js` part, and with this
commit the logger will already report the normalized
version of scriptlets.

Eventually, when the migration to sans-`.js` is
complete (also to determine), the internal
normalization of the token will be removed and this
will become official syntax.

Filter list maintainers will have to mind that
uAssets is becoming in use beyond uBO (i.e. Brave)
when skipping the `.js` part -- hopefully Brave will
go along with the change here, which is to remove a
bit of tediousness for filter list maintainers.
This commit is contained in:
Raymond Hill 2019-07-05 10:10:59 -04:00
parent 6220e1d3eb
commit 5552d6717d
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -170,23 +170,45 @@
} }
}; };
})(); })();
const normalizeRawToken = function(raw) {
let rawEnd = raw.length;
let end = raw.indexOf(',');
if ( end === -1 ) {
end = rawEnd;
}
let token = raw.slice(0, end).trim();
let normalized = token.endsWith('.js') ? token.slice(0, -3) : token;
let beg = end + 1;
while ( beg < rawEnd ) {
end = raw.indexOf(',', beg);
if ( end === -1 ) { end = rawEnd; }
normalized += ', ' + raw.slice(beg, end).trim();
beg = end + 1;
}
return normalized;
};
const lookupScriptlet = function(raw, reng, toInject) { const lookupScriptlet = function(raw, reng, toInject) {
if ( toInject.has(raw) ) { return; } const normalized = normalizeRawToken(raw);
if ( toInject.has(normalized) ) { return; }
if ( scriptletCache.resetTime < reng.modifyTime ) { if ( scriptletCache.resetTime < reng.modifyTime ) {
scriptletCache.reset(); scriptletCache.reset();
} }
let content = scriptletCache.lookup(raw); let content = scriptletCache.lookup(normalized);
if ( content === undefined ) { if ( content === undefined ) {
const pos = raw.indexOf(','); const pos = normalized.indexOf(',');
let token, args; let token, args;
if ( pos === -1 ) { if ( pos === -1 ) {
token = raw; token = normalized;
} else { } else {
token = raw.slice(0, pos).trim(); token = normalized.slice(0, pos).trim();
args = raw.slice(pos + 1).trim(); args = normalized.slice(pos + 1).trim();
} }
content = reng.resourceContentFromName(token, 'application/javascript'); content = reng.resourceContentFromName(
`${token}.js`,
'application/javascript'
);
if ( !content ) { return; } if ( !content ) { return; }
if ( args ) { if ( args ) {
content = patchScriptlet(content, args); content = patchScriptlet(content, args);
@ -196,9 +218,9 @@
'try {\n' + 'try {\n' +
content + '\n' + content + '\n' +
'} catch ( e ) { }'; '} catch ( e ) { }';
scriptletCache.add(raw, content); scriptletCache.add(normalized, content);
} }
toInject.set(raw, content); toInject.set(normalized, content);
}; };
// Fill template placeholders. Return falsy if: // Fill template placeholders. Return falsy if:
@ -332,22 +354,29 @@
); );
} }
for ( const token of scriptlets ) { for ( const rawToken of scriptlets ) {
lookupScriptlet(token, reng, scriptletsRegister); lookupScriptlet(rawToken, reng, scriptletsRegister);
} }
if ( scriptletsRegister.size === 0 ) { return; } if ( scriptletsRegister.size === 0 ) { return; }
// Return an array of scriptlets, and log results if needed. // Normalize dictionary of exceptions
// TODO: Eventually remove this code when normalied token usage is
// widespread and it can safely becomes the only valid syntax.
for ( const rawToken of exceptions ) {
exceptions.add(normalizeRawToken(rawToken));
}
// Return an array of scriptlets, and log results if needed.
const out = []; const out = [];
const loggerEnabled = µb.logger.enabled; const loggerEnabled = µb.logger.enabled;
for ( const [ token, code ] of scriptletsRegister ) { for ( const [ normalized, code ] of scriptletsRegister ) {
const isException = exceptionsRegister.has(token); const isException = exceptions.has(normalized);
if ( isException === false ) { if ( isException === false ) {
out.push(code); out.push(code);
} }
if ( loggerEnabled ) { if ( loggerEnabled ) {
logOne(isException, token, request); logOne(isException, normalized, request);
} }
} }