mirror of
https://github.com/gorhill/uBlock.git
synced 2024-11-26 04:12:50 +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:
parent
a54e3c5e39
commit
84be9cde6d
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user