Skip to content

Commit

Permalink
Rename href-from-text to href-sanitizer, add argument
Browse files Browse the repository at this point in the history
Related issue:
- uBlockOrigin/uBlock-issues#2531 (comment)

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.
  • Loading branch information
gorhill committed Mar 9, 2023
1 parent 3b3a363 commit 4b4ef6a
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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);
Expand Down

0 comments on commit 4b4ef6a

Please sign in to comment.