1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-05 11:37:01 +02:00

Rename href-from-text to href-sanitizer, add argument

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/2531#issuecomment-1462389539

Usage:
- example.com##+js(href-sanitizer, a[href^="/go?to="]:not([title]))
- example.com##+js(href-sanitizer, a[href^="/go?to="][title], [title])

The second argument is the attribute from which to extract the text
to be used for the `href` attribute of the link. If the second
attribute is absent, the text content of the element will be used.
This commit is contained in:
Raymond Hill 2023-03-09 13:37:06 -05:00
parent 3b3a363dac
commit 4b4ef6a60c
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -1842,11 +1842,14 @@
/// href-from-text.js
/// href-sanitizer.js
(function() {
let selector = '{{1}}';
if ( selector === '{{1}}' ) { selector = ''; }
if ( selector === '' ) { return; }
let source = '{{2}}';
if ( source === '{{2}}' ) { source = ''; }
if ( source === '' ) { source = 'text'; }
const sanitizeCopycats = (href, text) => {
let elems = [];
try {
@ -1858,6 +1861,19 @@
elem.setAttribute('href', text);
}
};
const extractText = (elem, source) => {
if ( /^\[.*\]$/.test(source) ) {
return elem.getAttribute(source.slice(1,-1).trim()) || '';
}
if ( source !== 'text' ) { return ''; }
const text = elem.textContent
.replace(/^[^\x21-\x7e]+/, '') // remove leading invalid characters
.replace(/[^\x21-\x7e]+$/, '') // remove trailing invalid characters
;
if ( /^https:\/\/./.test(text) === false ) { return ''; }
if ( /[^\x21-\x7e]/.test(text) ) { return ''; }
return text;
};
const sanitize = ( ) => {
let elems = [];
try {
@ -1870,12 +1886,8 @@
if ( elem.localName !== 'a' ) { continue; }
if ( elem.hasAttribute('href') === false ) { continue; }
const href = elem.getAttribute('href');
const text = elem.textContent
.replace(/^[^\x21-\x7e]+/, '') // remove leading invalid characters
.replace(/[^\x21-\x7e]+$/, '') // remove trailing invalid characters
;
if ( /^https:\/\/./.test(text) === false ) { continue; }
if ( /[^\x21-\x7e]/.test(text) ) { continue; }
const text = extractText(elem, source);
if ( text === '' ) { continue; }
if ( href === text ) { continue; }
elem.setAttribute('href', text);
sanitizeCopycats(href, text);