Skip to content

Commit

Permalink
Improve href-sanitizer scriptlet
Browse files Browse the repository at this point in the history
Support ability to recursively unwrap destination URL. Example:

    ...##+js(href-sanitizer, a.clickTracker, ?r?u)

Related discussion:
uBlockOrigin/uBlock-discussions#775 (comment)
  • Loading branch information
gorhill committed Jul 23, 2024
1 parent a54e3c5 commit 84be9cd
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 84be9cd

Please sign in to comment.