Skip to content

Commit

Permalink
Add basic compatibility with ABP's rewrite option
Browse files Browse the repository at this point in the history
Related issue:
- uBlockOrigin/uBlock-issues#857

The recognized resources are:
- abp-resource:blank-mp3
- abp-resource:blank-js

ABP's tokens are excluded from auto-complete so as to not
get in the way of uBO's filter list maintainers.
  • Loading branch information
gorhill committed Dec 9, 2020
1 parent b553a66 commit 0b5f539
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
17 changes: 14 additions & 3 deletions src/js/codemirror/ubo-static-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ CodeMirror.defineMode('ubo-static-filtering', function() {
// Warn about unknown redirect tokens.
if (
string.charCodeAt(pos - 1) === 0x3D /* '=' */ &&
/[$,]redirect(-rule)?=$/.test(string.slice(0, pos))
/[$,](redirect(-rule)?|rewrite)=$/.test(string.slice(0, pos))
) {
style = 'value';
const end = parser.skipUntil(
Expand Down Expand Up @@ -418,6 +418,12 @@ const initHints = function() {
return (item[1] & 0b01) !== 0;
})
);
const excludedHints = new Set([
'genericblock',
'object-subrequest',
'rewrite',
'webrtc',
]);

const pickBestHints = function(cursor, seedLeft, seedRight, hints) {
const seed = (seedLeft + seedRight).trim();
Expand Down Expand Up @@ -471,6 +477,7 @@ const initHints = function() {
const isException = parser.isException();
const hints = [];
for ( let [ text, bits ] of parser.netOptionTokenDescriptors ) {
if ( excludedHints.has(text) ) { continue; }
if ( isNegated && (bits & parser.OPTCanNegate) === 0 ) { continue; }
if ( isException ) {
if ( (bits & parser.OPTBlockOnly) !== 0 ) { continue; }
Expand All @@ -488,14 +495,18 @@ const initHints = function() {
const getNetRedirectHints = function(cursor, seedLeft, seedRight) {
const hints = [];
for ( const text of redirectNames.keys() ) {
if ( text.startsWith('abp-resource:') ) { continue; }
hints.push(text);
}
return pickBestHints(cursor, seedLeft, seedRight, hints);
};

const getNetHints = function(cursor, line) {
const beg = cursor.ch;
if ( parser.optionsSpan.len === 0 ) {
if (
parser.optionsAnchorSpan.len === 0 &&
line.endsWith('$') === false
) {
if ( /[^\w\x80-\xF4#,.-]/.test(line) === false ) {
return getOriginHints(cursor, line);
}
Expand All @@ -511,7 +522,7 @@ const initHints = function() {
if ( assignPos === -1 ) {
return getNetOptionHints(cursor, matchLeft[0], matchRight[0]);
}
if ( /^redirect(-rule)?=/.test(matchLeft[0]) ) {
if ( /^(redirect(-rule)?|rewrite)=/.test(matchLeft[0]) ) {
return getNetRedirectHints(
cursor,
matchLeft[0].slice(assignPos + 1),
Expand Down
19 changes: 9 additions & 10 deletions src/js/pagestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,16 +377,15 @@ const PageStore = class {
setFrameURL(frameId, frameURL) {
let frameStore = this.frames.get(frameId);
if ( frameStore !== undefined ) {
if ( frameURL !== frameStore.rawURL ) {
frameStore.init(frameURL);
}
} else {
frameStore = FrameStore.factory(frameURL);
this.frames.set(frameId, frameStore);
this.frameAddCount += 1;
if ( (this.frameAddCount & 0b111111) === 0 ) {
this.pruneFrames();
}
return frameURL === frameStore.rawURL
? frameStore
: frameStore.init(frameURL);
}
frameStore = FrameStore.factory(frameURL);
this.frames.set(frameId, frameStore);
this.frameAddCount += 1;
if ( (this.frameAddCount & 0b111111) === 0 ) {
this.pruneFrames();
}
return frameStore;
}
Expand Down
11 changes: 8 additions & 3 deletions src/js/redirect-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const redirectableResources = new Map([
data: 'text',
} ],
[ 'noop-0.1s.mp3', {
alias: 'noopmp3-0.1s',
alias: [ 'noopmp3-0.1s', 'abp-resource:blank-mp3' ],
data: 'blob',
} ],
[ 'noop-1s.mp4', {
Expand All @@ -132,7 +132,7 @@ const redirectableResources = new Map([
alias: 'noopframe',
} ],
[ 'noop.js', {
alias: 'noopjs',
alias: [ 'noopjs', 'abp-resource:blank-js' ],
data: 'text',
} ],
[ 'noop.txt', {
Expand Down Expand Up @@ -461,7 +461,12 @@ RedirectEngine.prototype.loadBuiltinResources = function() {
params: details.params,
});
this.resources.set(name, entry);
if ( details.alias !== undefined ) {
if ( details.alias === undefined ) { return; }
if ( Array.isArray(details.alias) ) {
for ( const alias of details.alias ) {
this.aliases.set(alias, name);
}
} else {
this.aliases.set(details.alias, name);
}
};
Expand Down
2 changes: 2 additions & 0 deletions src/js/static-filtering-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2183,6 +2183,7 @@ const netOptionTokenDescriptors = new Map([
[ 'queryprune', OPTTokenQueryprune | OPTMayAssign | OPTModifierType | OPTNonCspableType | OPTNonRedirectableType ],
[ 'removeparam', OPTTokenQueryprune | OPTMayAssign | OPTModifierType | OPTNonCspableType | OPTNonRedirectableType ],
[ 'redirect', OPTTokenRedirect | OPTMustAssign | OPTAllowMayAssign | OPTModifierType ],
[ 'rewrite', OPTTokenRedirect | OPTMustAssign | OPTAllowMayAssign | OPTModifierType ],
[ 'redirect-rule', OPTTokenRedirectRule | OPTMustAssign | OPTAllowMayAssign | OPTModifierType | OPTNonCspableType ],
[ 'script', OPTTokenScript | OPTCanNegate | OPTNetworkType | OPTModifiableType | OPTRedirectableType | OPTNonCspableType ],
[ 'shide', OPTTokenShide | OPTNonNetworkType | OPTNonCspableType | OPTNonRedirectableType ],
Expand Down Expand Up @@ -2241,6 +2242,7 @@ Parser.netOptionTokenIds = new Map([
[ 'queryprune', OPTTokenQueryprune ],
[ 'removeparam', OPTTokenQueryprune ],
[ 'redirect', OPTTokenRedirect ],
[ 'rewrite', OPTTokenRedirect ],
[ 'redirect-rule', OPTTokenRedirectRule ],
[ 'script', OPTTokenScript ],
[ 'shide', OPTTokenShide ],
Expand Down

1 comment on commit 0b5f539

@gorhill
Copy link
Owner Author

@gorhill gorhill commented on 0b5f539 Dec 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I accidentally committed a completely unrelated change:
0b5f539#diff-d88e38513efe4e855f66a53f0ba00fa16264d07f8d8fe7100cf3131a74fbe0b4

Which was a minor code review related to another issue:
3ff6617

Please sign in to comment.