[mv3] Minor code review

This commit is contained in:
Raymond Hill 2024-12-06 12:13:18 -05:00
parent 4e7bdff8ed
commit 424fc81628
No known key found for this signature in database
GPG key ID: 25E1490B761470C2
2 changed files with 35 additions and 24 deletions

View file

@ -50,7 +50,7 @@ q {
font-weight: bold; font-weight: bold;
} }
.code { .code {
font-size: 13px; font-size: var(--font-size-smaller);
word-break: break-all; word-break: break-all;
} }
#warningSign { #warningSign {
@ -137,7 +137,6 @@ body[dir="rtl"] #toggleParse {
#urlskip a { #urlskip a {
display: block; display: block;
overflow-y: auto; overflow-y: auto;
word-break: break-all;
} }
#actionContainer { #actionContainer {

View file

@ -72,6 +72,31 @@ qs$('#theURL > p > span:first-of-type').append(urlToFragment(toURL.href));
/******************************************************************************/ /******************************************************************************/
function fragmentFromTemplate(template, placeholder, text, details) {
const fragment = new DocumentFragment();
const pos = template.indexOf(placeholder);
if ( pos === -1 ) {
fragment.append(template);
return fragment;
}
const elem = document.createElement(details.tag);
const { attributes } = details;
if ( attributes ) {
for ( let i = 0; i < attributes.length; i+= 2 ) {
elem.setAttribute(attributes[i+0], attributes[i+1]);
}
}
elem.append(text);
fragment.append(
template.slice(0, pos),
elem,
template.slice(pos + placeholder.length)
);
return fragment;
}
/******************************************************************************/
// https://github.com/gorhill/uBlock/issues/691 // https://github.com/gorhill/uBlock/issues/691
// Parse URL to extract as much useful information as possible. This is // Parse URL to extract as much useful information as possible. This is
// useful to assist the user in deciding whether to navigate to the web page. // useful to assist the user in deciding whether to navigate to the web page.
@ -179,15 +204,11 @@ qs$('#theURL > p > span:first-of-type').append(urlToFragment(toURL.href));
if ( toFetch.length === 0 ) { return; } if ( toFetch.length === 0 ) { return; }
await Promise.all(toFetch); await Promise.all(toFetch);
if ( iList === -1 ) { return; } if ( iList === -1 ) { return; }
const fragment = fragmentFromTemplate(
const fragment = new DocumentFragment(); i18n$('strictblockReasonSentence1'),
const text = i18n$('strictblockReasonSentence1'); '{{listname}}', rulesetDetails[iList].name,
const placeholder = '{{listname}}'; { tag: 'q' }
const pos = text.indexOf(placeholder); );
if ( pos === -1 ) { return; }
const q = document.createElement('q');
q.append(rulesetDetails[iList].name);
fragment.append(text.slice(0, pos), q, text.slice(pos + placeholder.length));
qs$('#reason').append(fragment); qs$('#reason').append(fragment);
dom.attr('#reason', 'hidden', null); dom.attr('#reason', 'hidden', null);
})(); })();
@ -210,19 +231,10 @@ qs$('#theURL > p > span:first-of-type').append(urlToFragment(toURL.href));
if ( re.test(toURL.href) === false ) { continue; } if ( re.test(toURL.href) === false ) { continue; }
const finalURL = urlSkip(toURL.href, false, urlskip.steps); const finalURL = urlSkip(toURL.href, false, urlskip.steps);
if ( finalURL === undefined ) { continue; } if ( finalURL === undefined ) { continue; }
const fragment = new DocumentFragment(); const fragment = fragmentFromTemplate(
const text = i18n$('strictblockRedirectSentence1'); i18n$('strictblockRedirectSentence1'),
const linkPlaceholder = '{{url}}'; '{{url}}', urlToFragment(finalURL),
const pos = text.indexOf(linkPlaceholder); { tag: 'a', attributes: [ 'href', finalURL, 'class', 'code' ] }
if ( pos === -1 ) { return; }
const link = document.createElement('a');
link.href = finalURL;
dom.cl.add(link, 'code');
link.append(urlToFragment(finalURL));
fragment.append(
text.slice(0, pos),
link,
text.slice(pos + linkPlaceholder.length)
); );
qs$('#urlskip').append(fragment); qs$('#urlskip').append(fragment);
dom.attr('#urlskip', 'hidden', null); dom.attr('#urlskip', 'hidden', null);