Skip to content

Commit 960f214

Browse files
committed
betterEmbedsYT: video description single line fixes
1 parent 17b9152 commit 960f214

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/betterEmbedsYT/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"$schema": "https://moonlight-mod.github.io/manifest.schema.json",
33
"id": "betterEmbedsYT",
4-
"version": "1.1.2",
4+
"version": "1.1.3",
55
"meta": {
66
"name": "Better YouTube Embeds",
77
"tagline": "Bypass copyright blocks, descriptions, block ads and trackers (works with Watch Together).",
88
"description": "Ad blocking uses code from the [Iridium](https://github.com/ParticleCore/Iridium) web extension.",
99
"authors": ["Cynosphere"],
1010
"tags": ["qol", "privacy"],
1111
"source": "https://github.com/Cynosphere/moonlight-extensions",
12-
"changelog": "Embeds now are a bit more compact and don't have the icon button"
12+
"changelog": "Video descriptions: single line detection now uses visual width instead of character count"
1313
},
1414
"settings": {
1515
"description": {

src/betterEmbedsYT/webpackModules/description.tsx

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type RenderDescription = (embed: any, description: string, headings: boolean) =>
88

99
const logger = moonlight.getLogger("Better YouTube Embeds - Description");
1010
const descriptionCache = new Map<string, string>();
11+
const sizeCache = new Map<string, number>();
1112

1213
const API_KEY = "AIzaSyCpphGplamUhCCEIcum1VyDXBt0i1nOqac"; // one of Google's own
1314
const FAKE_EMBED = { type: "rich" };
@@ -27,6 +28,7 @@ function YTDescription({
2728
const [fullDescription, setFullDescription] = React.useState(
2829
descriptionCache.has(videoId) ? descriptionCache.get(videoId) : description
2930
);
31+
const [firstLineLength, setFirstLineLength] = React.useState(sizeCache.has(videoId) ? sizeCache.get(videoId) : -1);
3032

3133
React.useEffect(() => {
3234
if (!descriptionCache.has(videoId))
@@ -53,15 +55,35 @@ function YTDescription({
5355
}
5456
});
5557

56-
const lines = fullDescription!.split("\n");
58+
const lines = fullDescription!.trim().split("\n");
59+
const descClass = EmbedClasses.embedDescription + " " + EmbedClasses.embedMargin;
60+
61+
if (lines.length === 1 && firstLineLength === -1) {
62+
const inner = document.createElement("span");
63+
inner.innerText = fullDescription!;
64+
const wrapper = document.createElement("div");
65+
wrapper.className = descClass;
66+
wrapper.appendChild(inner);
67+
wrapper.style.position = "absolute";
68+
wrapper.style.opacity = "0";
69+
wrapper.style.pointerEvents = "none";
70+
document.body.appendChild(wrapper);
71+
72+
const width = wrapper.clientWidth;
73+
74+
setFirstLineLength(width);
75+
sizeCache.set(videoId, width);
76+
77+
setTimeout(() => wrapper.remove(), 0);
78+
}
5779

5880
const rendered = renderDescription(FAKE_EMBED, fullDescription!, false);
5981
const firstLine = renderDescription(FAKE_EMBED, lines[0], false);
6082

61-
return lines.length === 1 && description.length <= 40 ? (
62-
<div className={EmbedClasses.embedDescription + " " + EmbedClasses.embedMargin}>{rendered}</div>
83+
return lines.length === 1 && firstLineLength! > 0 && firstLineLength! < 400 ? (
84+
<div className={descClass}>{rendered}</div>
6385
) : (
64-
<div className={EmbedClasses.embedDescription + " " + EmbedClasses.embedMargin}>
86+
<div className={descClass}>
6587
{expanded ? rendered : <div className="betterEmbedsYT-description-firstLine">{firstLine}</div>}
6688
<Clickable className="betterEmbedsYT-description-button" onClick={() => setExpanded(!expanded)}>
6789
{expanded ? "Show less" : "Show more"}

0 commit comments

Comments
 (0)