Skip to content

Commit bfd0d9a

Browse files
authored
Improve narrow YouTube Click to Load placeholders (#422)
When the YouTube Click to Load placeholders are very narrow, the information text takes up too much space and some of the other elements are lost. Let's tweak narrow placeholders, to hide that information text and move the "Learn more" link. This isn't the best approach (using @container queries[1] might be better), but this code is going to be refactored shortly anyway. 1 - https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Container_Queries
1 parent c1999e6 commit bfd0d9a

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/features/click-to-load.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ function replaceYouTubeCTL (trackingElement, widget) {
589589
resizeElementToMatch(oldPlaceholder || trackingElement, blockingDialog)
590590
replaceTrackingElement(widget, trackingElement, blockingDialog)
591591
showExtraUnblockIfShortPlaceholder(shadowRoot, blockingDialog)
592+
hideInfoTextIfNarrowPlaceholder(shadowRoot, blockingDialog, 460)
592593
}
593594
}
594595

@@ -628,6 +629,44 @@ function showExtraUnblockIfShortPlaceholder (shadowRoot, placeholder) {
628629
}
629630
}
630631

632+
/**
633+
* Hide the info text (and move the "Learn More" link) if the placeholder is too
634+
* narrow.
635+
* @param {ShadowRoot} shadowRoot
636+
* @param {HTMLElement} placeholder Placeholder for tracking element
637+
* @param {number} narrowWidth
638+
* Maximum placeholder width (in pixels) for the placeholder to be considered
639+
* narrow.
640+
*/
641+
function hideInfoTextIfNarrowPlaceholder (shadowRoot, placeholder, narrowWidth) {
642+
const { width: placeholderWidth } = placeholder.getBoundingClientRect()
643+
if (placeholderWidth > 0 && placeholderWidth <= narrowWidth) {
644+
const buttonContainer =
645+
shadowRoot.querySelector('.DuckDuckGoButton.primary')?.parentElement
646+
const contentTitle = shadowRoot.getElementById('contentTitle')
647+
const infoText = shadowRoot.getElementById('infoText')
648+
/** @type {HTMLElement?} */
649+
const learnMoreLink = shadowRoot.getElementById('learnMoreLink')
650+
651+
// These elements will exist, but this check keeps TypeScript happy.
652+
if (!buttonContainer || !contentTitle || !infoText || !learnMoreLink) {
653+
return
654+
}
655+
656+
// Remove the information text.
657+
infoText.remove()
658+
learnMoreLink.remove()
659+
660+
// Append the "Learn More" link to the title.
661+
contentTitle.innerText += '. '
662+
learnMoreLink.style.removeProperty('font-size')
663+
contentTitle.appendChild(learnMoreLink)
664+
665+
// Improve margin/padding, to ensure as much is displayed as possible.
666+
buttonContainer.style.removeProperty('margin')
667+
}
668+
}
669+
631670
/**
632671
* Replace the blocked CTL elements on the page with placeholders.
633672
* @param {HTMLElement} [targetElement]
@@ -749,6 +788,7 @@ function getLearnMoreLink (mode = 'lightMode') {
749788
linkElement.href = 'https://help.duckduckgo.com/duckduckgo-help-pages/privacy/embedded-content-protection/'
750789
linkElement.target = '_blank'
751790
linkElement.textContent = sharedStrings.learnMore
791+
linkElement.id = 'learnMoreLink'
752792
return linkElement
753793
}
754794

@@ -1302,10 +1342,14 @@ function createContentBlock (widget, button, textButton, img, bottomRow) {
13021342
const contentTitle = document.createElement('div')
13031343
contentTitle.style.cssText = styles.contentTitle
13041344
contentTitle.textContent = widget.replaceSettings.infoTitle
1345+
contentTitle.id = 'contentTitle'
13051346
contentRow.appendChild(contentTitle)
13061347
const contentText = document.createElement('div')
13071348
contentText.style.cssText = styles.contentText
1308-
contentText.textContent = widget.replaceSettings.infoText + ' '
1349+
const contentTextSpan = document.createElement('span')
1350+
contentTextSpan.id = 'infoText'
1351+
contentTextSpan.textContent = widget.replaceSettings.infoText + ' '
1352+
contentText.appendChild(contentTextSpan)
13091353
contentText.appendChild(getLearnMoreLink())
13101354
contentRow.appendChild(contentText)
13111355
element.appendChild(contentRow)

unit-test/verify-artifacts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { cwd } from '../scripts/script-utils.js'
66
const ROOT = join(cwd(import.meta.url), '..')
77
const BUILD = join(ROOT, 'build')
88
const APPLE_BUILD = join(ROOT, 'Sources/ContentScopeScripts/dist')
9-
const CSS_OUTPUT_SIZE = 551000
9+
const CSS_OUTPUT_SIZE = 560000
1010
const CSS_OUTPUT_SIZE_CHROME = CSS_OUTPUT_SIZE * 1.45 // 45% larger for Chrome MV2 due to base64 encoding
1111

1212
const checks = {

0 commit comments

Comments
 (0)