From 4b4ef6a60c24630e6bbd012f33e86c47a340a1c3 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Thu, 9 Mar 2023 13:37:06 -0500 Subject: [PATCH] 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. --- assets/resources/scriptlets.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/assets/resources/scriptlets.js b/assets/resources/scriptlets.js index 94173959a..12a3ecf29 100644 --- a/assets/resources/scriptlets.js +++ b/assets/resources/scriptlets.js @@ -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);