mirror of https://github.com/gorhill/uBlock.git
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:
parent
a54e3c5e39
commit
84be9cde6d
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue