Skip to content

Commit 96c1d3a

Browse files
committed
Improve narrow YouTube Click to Load placeholders
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 96c1d3a

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/features/click-to-load.js

Lines changed: 49 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,48 @@ 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 innerDiv = shadowRoot.querySelector('.DuckDuckGoSocialContainer')
650+
/** @type {HTMLElement?} */
651+
const learnMoreLink = shadowRoot.querySelector('.learnMoreLink')
652+
653+
// These elements will exist, but this check keeps TypeScript happy.
654+
if (!buttonContainer || !contentTitle || !infoText || !innerDiv ||
655+
!learnMoreLink) {
656+
return
657+
}
658+
659+
// Remove the information text.
660+
infoText.remove()
661+
learnMoreLink.remove()
662+
663+
// Append the "Learn More" link to the title.
664+
contentTitle.innerText += '. '
665+
learnMoreLink.style.removeProperty('font-size')
666+
contentTitle.appendChild(learnMoreLink)
667+
668+
// Improve margin/padding, to ensure as much is displayed as possible.
669+
buttonContainer.style.removeProperty('margin')
670+
innerDiv.style.height = 'calc(100% - 80px)'
671+
}
672+
}
673+
631674
/**
632675
* Replace the blocked CTL elements on the page with placeholders.
633676
* @param {HTMLElement} [targetElement]
@@ -749,6 +792,7 @@ function getLearnMoreLink (mode = 'lightMode') {
749792
linkElement.href = 'https://help.duckduckgo.com/duckduckgo-help-pages/privacy/embedded-content-protection/'
750793
linkElement.target = '_blank'
751794
linkElement.textContent = sharedStrings.learnMore
795+
linkElement.classList.add('learnMoreLink')
752796
return linkElement
753797
}
754798

@@ -1302,10 +1346,14 @@ function createContentBlock (widget, button, textButton, img, bottomRow) {
13021346
const contentTitle = document.createElement('div')
13031347
contentTitle.style.cssText = styles.contentTitle
13041348
contentTitle.textContent = widget.replaceSettings.infoTitle
1349+
contentTitle.id = 'contentTitle'
13051350
contentRow.appendChild(contentTitle)
13061351
const contentText = document.createElement('div')
13071352
contentText.style.cssText = styles.contentText
1308-
contentText.textContent = widget.replaceSettings.infoText + ' '
1353+
const contentTextSpan = document.createElement('span')
1354+
contentTextSpan.id = 'infoText'
1355+
contentTextSpan.textContent = widget.replaceSettings.infoText + ' '
1356+
contentText.appendChild(contentTextSpan)
13091357
contentText.appendChild(getLearnMoreLink())
13101358
contentRow.appendChild(contentText)
13111359
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)