From 84be9cde6d5ec3381a0a563796b92ed733c202a4 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 23 Jul 2024 09:19:16 -0400 Subject: [PATCH] Improve `href-sanitizer` scriptlet Support ability to recursively unwrap destination URL. Example: ...##+js(href-sanitizer, a.clickTracker, ?r?u) Related discussion: https://github.com/uBlockOrigin/uBlock-discussions/discussions/775#discussioncomment-10120835 --- assets/resources/scriptlets.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/assets/resources/scriptlets.js b/assets/resources/scriptlets.js index 55f512dde..4cc2e938f 100644 --- a/assets/resources/scriptlets.js +++ b/assets/resources/scriptlets.js @@ -3503,17 +3503,26 @@ function hrefSanitizer( } return ''; }; + const extractParam = (href, source) => { + if ( Boolean(source) === false ) { return href; } + const recursive = source.includes('?', 1); + const end = recursive ? source.indexOf('?', 1) : source.length; + try { + const url = new URL(href, document.location); + const value = url.searchParams.get(source.slice(1, end)); + if ( value === null ) { return href } + if ( recursive ) { return extractParam(value, source.slice(end)); } + return value; + } catch(x) { + } + return href; + }; const extractText = (elem, source) => { if ( /^\[.*\]$/.test(source) ) { return elem.getAttribute(source.slice(1,-1).trim()) || ''; } if ( source.startsWith('?') ) { - try { - const url = new URL(elem.href, document.location); - return url.searchParams.get(source.slice(1)) || ''; - } catch(x) { - } - return ''; + return extractParam(elem.href, source); } if ( source === 'text' ) { return elem.textContent