From 424fc816281ce053f8e41b0776fcefd73e5fc8a8 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Fri, 6 Dec 2024 12:13:18 -0500 Subject: [PATCH] [mv3] Minor code review --- platform/mv3/extension/css/strictblock.css | 3 +- platform/mv3/extension/js/strictblock.js | 56 +++++++++++++--------- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/platform/mv3/extension/css/strictblock.css b/platform/mv3/extension/css/strictblock.css index 7f1f0e9c0..5a4594464 100644 --- a/platform/mv3/extension/css/strictblock.css +++ b/platform/mv3/extension/css/strictblock.css @@ -50,7 +50,7 @@ q { font-weight: bold; } .code { - font-size: 13px; + font-size: var(--font-size-smaller); word-break: break-all; } #warningSign { @@ -137,7 +137,6 @@ body[dir="rtl"] #toggleParse { #urlskip a { display: block; overflow-y: auto; - word-break: break-all; } #actionContainer { diff --git a/platform/mv3/extension/js/strictblock.js b/platform/mv3/extension/js/strictblock.js index 7600bcfbb..2e8c74c67 100644 --- a/platform/mv3/extension/js/strictblock.js +++ b/platform/mv3/extension/js/strictblock.js @@ -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 // 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. @@ -179,15 +204,11 @@ qs$('#theURL > p > span:first-of-type').append(urlToFragment(toURL.href)); if ( toFetch.length === 0 ) { return; } await Promise.all(toFetch); if ( iList === -1 ) { return; } - - const fragment = new DocumentFragment(); - const text = i18n$('strictblockReasonSentence1'); - const placeholder = '{{listname}}'; - 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)); + const fragment = fragmentFromTemplate( + i18n$('strictblockReasonSentence1'), + '{{listname}}', rulesetDetails[iList].name, + { tag: 'q' } + ); qs$('#reason').append(fragment); 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; } const finalURL = urlSkip(toURL.href, false, urlskip.steps); if ( finalURL === undefined ) { continue; } - const fragment = new DocumentFragment(); - const text = i18n$('strictblockRedirectSentence1'); - const linkPlaceholder = '{{url}}'; - const pos = text.indexOf(linkPlaceholder); - 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) + const fragment = fragmentFromTemplate( + i18n$('strictblockRedirectSentence1'), + '{{url}}', urlToFragment(finalURL), + { tag: 'a', attributes: [ 'href', finalURL, 'class', 'code' ] } ); qs$('#urlskip').append(fragment); dom.attr('#urlskip', 'hidden', null);