1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-22 10:22:51 +01:00

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
This commit is contained in:
Raymond Hill 2024-07-23 09:19:16 -04:00
parent a54e3c5e39
commit 84be9cde6d
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -3503,17 +3503,26 @@ function hrefSanitizer(
} }
return ''; 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) => { const extractText = (elem, source) => {
if ( /^\[.*\]$/.test(source) ) { if ( /^\[.*\]$/.test(source) ) {
return elem.getAttribute(source.slice(1,-1).trim()) || ''; return elem.getAttribute(source.slice(1,-1).trim()) || '';
} }
if ( source.startsWith('?') ) { if ( source.startsWith('?') ) {
try { return extractParam(elem.href, source);
const url = new URL(elem.href, document.location);
return url.searchParams.get(source.slice(1)) || '';
} catch(x) {
}
return '';
} }
if ( source === 'text' ) { if ( source === 'text' ) {
return elem.textContent return elem.textContent