Skip to content

Commit cd72c13

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 0827b63 commit cd72c13

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/features/click-to-load.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ async function replaceYouTubeCTL (trackingElement, widget, togglePlaceholder = f
461461
widget, trackingElement, blockingDialog, currentPlaceholder
462462
)
463463
showExtraUnblockIfShortPlaceholder(shadowRoot, blockingDialog)
464+
hideInfoTextIfNarrowPlaceholder(shadowRoot, blockingDialog, 460)
464465
}
465466
}
466467

@@ -492,6 +493,40 @@ function showExtraUnblockIfShortPlaceholder (shadowRoot, placeholder) {
492493
}
493494
}
494495

496+
/**
497+
* Hide the info text (and move the "Learn More" link) if the placeholder is too
498+
* narrow.
499+
* @param {Element} shadowRoot
500+
* @param {Element} placeholder Placeholder for tracking element
501+
* @param {number} narrowWidth
502+
* Maximum placeholder width (in pixels) for the placeholder to be considered
503+
* narrow.
504+
*/
505+
function hideInfoTextIfNarrowPlaceholder (shadowRoot, placeholder, narrowWidth) {
506+
const { width: placeholderWidth } = placeholder.getBoundingClientRect()
507+
if (placeholderWidth > 0 && placeholderWidth <= narrowWidth) {
508+
const buttonContainer =
509+
shadowRoot.querySelector('.DuckDuckGoButton.primary').parentElement
510+
const contentTitle = shadowRoot.getElementById('contentTitle')
511+
const infoText = shadowRoot.getElementById('infoText')
512+
const innerDiv = shadowRoot.querySelector('.DuckDuckGoSocialContainer')
513+
const learnMoreLink = shadowRoot.querySelector('.learnMoreLink')
514+
515+
// Remove the information text.
516+
infoText.remove()
517+
learnMoreLink.remove()
518+
519+
// Append the "Learn More" link to the title.
520+
contentTitle.innerText += '. '
521+
learnMoreLink.style.removeProperty('font-size')
522+
contentTitle.appendChild(learnMoreLink)
523+
524+
// Improve margin/padding, to ensure as much is displayed as possible.
525+
buttonContainer.style.removeProperty('margin')
526+
innerDiv.style.height = 'calc(100% - 80px)'
527+
}
528+
}
529+
495530
/**
496531
* Replace the blocked CTP elements on the page with placeholders.
497532
* @param {Element} [targetElement]
@@ -600,6 +635,7 @@ function getLearnMoreLink (mode) {
600635
linkElement.href = 'https://help.duckduckgo.com/duckduckgo-help-pages/privacy/embedded-content-protection/'
601636
linkElement.target = '_blank'
602637
linkElement.textContent = sharedStrings.learnMore
638+
linkElement.classList.add('learnMoreLink')
603639
return linkElement
604640
}
605641

@@ -1056,10 +1092,14 @@ async function createContentBlock (widget, button, textButton, img, bottomRow) {
10561092
const contentTitle = document.createElement('div')
10571093
contentTitle.style.cssText = styles.contentTitle
10581094
contentTitle.textContent = widget.replaceSettings.infoTitle
1095+
contentTitle.id = 'contentTitle'
10591096
contentRow.appendChild(contentTitle)
10601097
const contentText = document.createElement('div')
10611098
contentText.style.cssText = styles.contentText
1062-
contentText.textContent = widget.replaceSettings.infoText + ' '
1099+
const contentTextSpan = document.createElement('span')
1100+
contentTextSpan.id = 'infoText'
1101+
contentTextSpan.textContent = widget.replaceSettings.infoText + ' '
1102+
contentText.appendChild(contentTextSpan)
10631103
contentText.appendChild(getLearnMoreLink())
10641104
contentRow.appendChild(contentText)
10651105
element.appendChild(contentRow)

0 commit comments

Comments
 (0)