mirror of https://github.com/gorhill/uBlock.git
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:
parent
3b3a363dac
commit
4b4ef6a60c
|
@ -1842,11 +1842,14 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// href-from-text.js
|
/// href-sanitizer.js
|
||||||
(function() {
|
(function() {
|
||||||
let selector = '{{1}}';
|
let selector = '{{1}}';
|
||||||
if ( selector === '{{1}}' ) { selector = ''; }
|
if ( selector === '{{1}}' ) { selector = ''; }
|
||||||
if ( selector === '' ) { return; }
|
if ( selector === '' ) { return; }
|
||||||
|
let source = '{{2}}';
|
||||||
|
if ( source === '{{2}}' ) { source = ''; }
|
||||||
|
if ( source === '' ) { source = 'text'; }
|
||||||
const sanitizeCopycats = (href, text) => {
|
const sanitizeCopycats = (href, text) => {
|
||||||
let elems = [];
|
let elems = [];
|
||||||
try {
|
try {
|
||||||
|
@ -1858,6 +1861,19 @@
|
||||||
elem.setAttribute('href', text);
|
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 = ( ) => {
|
const sanitize = ( ) => {
|
||||||
let elems = [];
|
let elems = [];
|
||||||
try {
|
try {
|
||||||
|
@ -1870,12 +1886,8 @@
|
||||||
if ( elem.localName !== 'a' ) { continue; }
|
if ( elem.localName !== 'a' ) { continue; }
|
||||||
if ( elem.hasAttribute('href') === false ) { continue; }
|
if ( elem.hasAttribute('href') === false ) { continue; }
|
||||||
const href = elem.getAttribute('href');
|
const href = elem.getAttribute('href');
|
||||||
const text = elem.textContent
|
const text = extractText(elem, source);
|
||||||
.replace(/^[^\x21-\x7e]+/, '') // remove leading invalid characters
|
if ( text === '' ) { continue; }
|
||||||
.replace(/[^\x21-\x7e]+$/, '') // remove trailing invalid characters
|
|
||||||
;
|
|
||||||
if ( /^https:\/\/./.test(text) === false ) { continue; }
|
|
||||||
if ( /[^\x21-\x7e]/.test(text) ) { continue; }
|
|
||||||
if ( href === text ) { continue; }
|
if ( href === text ) { continue; }
|
||||||
elem.setAttribute('href', text);
|
elem.setAttribute('href', text);
|
||||||
sanitizeCopycats(href, text);
|
sanitizeCopycats(href, text);
|
||||||
|
|
Loading…
Reference in New Issue