From 64b2086ba44e986af2f1b052cdbe2b07b3034e1a Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 5 Oct 2024 14:59:43 -0400 Subject: [PATCH] Add ability to lookup parameter name in `urlskip=` Relate case: https://github.com/uBlockOrigin/uBlock-issues/issues/3206#issuecomment-2395121619 Newly supported step: `&i`, meant to lookup a parameter's name at position `i` (1-based). The parameter name will be used as the URL (whereas `?` is meant to lookup a parameter's value). --- src/js/static-filtering-parser.js | 2 +- src/js/static-net-filtering.js | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/js/static-filtering-parser.js b/src/js/static-filtering-parser.js index efbe8d97c..1249b07b8 100644 --- a/src/js/static-filtering-parser.js +++ b/src/js/static-filtering-parser.js @@ -1531,7 +1531,7 @@ export class AstFilterParser { break; } const value = this.getNetOptionValue(NODE_TYPE_NET_OPTION_NAME_URLSKIP); - if ( value.startsWith('?') === false || value.length < 2 ) { + if ( value.length < 2 ) { this.astError = AST_ERROR_OPTION_BADVALUE; realBad = true; } diff --git a/src/js/static-net-filtering.js b/src/js/static-net-filtering.js index ec62bb945..194229573 100644 --- a/src/js/static-net-filtering.js +++ b/src/js/static-net-filtering.js @@ -5411,8 +5411,9 @@ function urlSkip(urlin, steps) { try { let urlout; for ( const step of steps ) { + const c0 = step.charCodeAt(0); // Extract from URL parameter - if ( step.startsWith('?') ) { + if ( c0 === 0x3F ) { /* ? */ urlout = (new URL(urlin)).searchParams.get(step.slice(1)); if ( urlout === null ) { return; } if ( urlout.includes(' ') ) { @@ -5421,6 +5422,16 @@ function urlSkip(urlin, steps) { urlin = urlout; continue; } + // Extract from URL parameter name at position i + if ( c0 === 0x26 ) { /* & */ + const i = (parseInt(step.slice(1)) || 0) - 1; + if ( i < 0 ) { return; } + const url = new URL(urlin); + if ( i >= url.searchParams.size ) { return; } + const params = Array.from(url.searchParams.keys()); + urlin = urlout = decodeURIComponent(params[i]); + continue; + } // Enforce https if ( step === '+https' ) { const s = urlin.replace(/^https?:\/\//, '');