Fix Shorts support and add userscript voting

Harden Shorts controls, reaction state, and navigation; share voting logic
with the extension; improve ratio-bar rendering; and add comprehensive Jest,
Playwright, artifact, and live-browser regression coverage.
This commit is contained in:
Anarios committed 2026-09-01 23:41:28 +02:00
1 parent 4e6dfd1977
commit 3657de9b2c
74 files changed
+23328 -588

No files matched your search

@@ -0,0 +1,645 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Userscript navigation browser-test fixture</title>
<style>
html,
body {
margin: 0;
min-height: 768px;
width: 100%;
}
[hidden] {
display: none !important;
}
#fixture-account,
#fixture-navigation,
#fixture-page {
display: block;
}
#fixture-navigation a,
#fixture-page > a {
display: inline-block;
margin: 4px;
min-height: 24px;
min-width: 96px;
}
ytd-watch-flexy,
ytd-menu-renderer,
segmented-like-dislike-button-view-model,
like-button-view-model,
dislike-button-view-model,
ytd-shorts,
ytd-reel-video-renderer,
ytm-reel-video-renderer,
ytm-reel-player-overlay-renderer,
reel-action-bar-view-model,
ytm-like-button-renderer {
display: block;
}
#top-row,
ytd-menu-renderer.ytd-watch-metadata > div {
width: min(320px, 100%);
}
segmented-like-dislike-button-view-model {
display: flex;
gap: 8px;
min-height: 48px;
width: 320px;
}
segmented-like-dislike-button-view-model button {
min-height: 36px;
min-width: 96px;
}
ytd-reel-video-renderer {
display: block;
min-height: 640px;
width: 420px;
}
ytd-reel-video-renderer[hidden] {
display: none;
}
ytm-reel-video-renderer[hidden] {
display: none;
}
.slim-video-action-bar-actions,
.segmented-buttons {
display: flex;
gap: 8px;
}
ytm-like-button-renderer button {
min-height: 36px;
min-width: 96px;
}
reel-action-bar-view-model {
align-items: flex-start;
display: flex;
flex-direction: column;
min-height: 320px;
width: 48px;
}
.ytwReelActionBarViewModelHostDesktopActionButton,
reel-action-bar-view-model > button-view-model {
box-sizing: content-box;
display: block;
height: 70px;
margin: 0;
padding: 0 0 8px;
width: 48px;
}
.ytSpecButtonShapeWithLabelHost {
align-items: center;
display: flex;
flex-direction: column;
height: 70px;
margin: 0;
padding: 0;
width: 48px;
}
reel-action-bar-view-model button {
align-items: center;
border: 0;
box-sizing: border-box;
display: flex;
height: 48px;
justify-content: center;
margin: 0;
min-height: 0;
min-width: 0;
padding: 12px;
width: 48px;
}
.ytSpecButtonShapeNextIcon,
.ytSpecButtonShapeNextIcon svg {
display: block;
height: 24px;
width: 24px;
}
.ytSpecButtonShapeWithLabelLabel {
align-items: center;
display: flex;
font:
12px/18px Arial,
sans-serif;
height: 22px;
justify-content: center;
width: 48px;
}
</style>
</head>
<body>
<div id="fixture-account"><button id="avatar-btn" aria-label="Account menu">Account</button></div>
<nav id="fixture-navigation" aria-label="Fixture navigation"></nav>
<main id="fixture-page"></main>
<script>
(() => {
const firstVideoId = "__VIDEO_ID__";
const secondVideoId = "__SECOND_VIDEO_ID__";
const marker = "rydNavigationFixture=1";
const navigation = document.getElementById("fixture-navigation");
const page = document.getElementById("fixture-page");
const isMobile = location.hostname === "m.youtube.com";
let pendingDelayedRender = null;
if (!customElements.get("like-button-view-model")) {
customElements.define(
"like-button-view-model",
class extends HTMLElement {
get className() {
return undefined;
}
},
);
}
const hrefFor = (pageKind, videoId) => {
if (pageKind === "shorts") return `/shorts/${videoId}?${marker}`;
if (pageKind === "watch") return `/watch?v=${videoId}&${marker}`;
return `/@FixtureChannel?${marker}`;
};
const linkMarkup = (id, label, pageKind, videoId, options = {}) => `
<a
id="${id}"
href="${hrefFor(pageKind, videoId)}"
data-fixture-page-kind="${pageKind}"
data-fixture-video-id="${videoId || ""}"
data-fixture-control-delay="${options.controlDelay || 0}"
>${label}</a>
`;
function channelMarkup() {
const decoyControls = isMobile
? `
<div class="slim-video-action-bar-actions" data-fixture-decoy-controls>
<div class="segmented-buttons" data-ryd-role="buttons">
<like-button-view-model class="style-text" data-ryd-role="like">
<button type="button" aria-label="Channel preview likes" aria-pressed="false">
<span id="text" role="text">100</span>
</button>
</like-button-view-model>
<dislike-button-view-model class="style-text" data-ryd-role="dislike">
<button type="button" aria-label="Channel preview dislike" aria-pressed="false">
<span id="text" role="text"></span>
</button>
</dislike-button-view-model>
</div>
</div>
`
: `
<div id="menu-container" style="display: none"></div>
<ytd-menu-renderer class="ytd-watch-metadata" data-fixture-decoy-controls>
<div id="top-level-buttons-computed">
<segmented-like-dislike-button-view-model data-ryd-role="buttons">
<like-button-view-model class="style-text" data-ryd-role="like">
<button type="button" aria-label="Channel preview likes" aria-pressed="false">
<span id="text" role="text">100</span>
</button>
</like-button-view-model>
<dislike-button-view-model class="style-text" data-ryd-role="dislike">
<button type="button" aria-label="Channel preview dislike" aria-pressed="false">
<span id="text" role="text"></span>
</button>
</dislike-button-view-model>
</segmented-like-dislike-button-view-model>
</div>
</ytd-menu-renderer>
`;
return `
<section data-fixture-page-kind="channel">
<h1>Fixture channel</h1>
${decoyControls}
${linkMarkup("channel-short", "Open Short", "shorts", firstVideoId, { controlDelay: 600 })}
${linkMarkup("channel-watch", "Open video", "watch", firstVideoId, { controlDelay: 200 })}
</section>
`;
}
function watchMarkup(videoId) {
const nextVideoId = videoId === firstVideoId ? secondVideoId : firstVideoId;
return `
<section data-fixture-page-kind="watch" data-fixture-video-id="${videoId}">
<div id="menu-container" style="display: none"></div>
<div id="player" loading="false"></div>
<ytd-watch-flexy video-id="${videoId}"></ytd-watch-flexy>
<div id="top-row">
<ytd-menu-renderer class="ytd-watch-metadata">
<div id="top-level-buttons-computed">
<segmented-like-dislike-button-view-model
data-ryd-role="buttons"
data-fixture-control-video-id="${videoId}"
>
<like-button-view-model class="style-text" data-ryd-role="like">
<button type="button" aria-label="100 likes" aria-pressed="false">
<span id="text" role="text">100</span>
</button>
</like-button-view-model>
<dislike-button-view-model class="style-text" data-ryd-role="dislike">
<button type="button" aria-label="Dislike this video" aria-pressed="false">
<span id="text" role="text"></span>
</button>
</dislike-button-view-model>
</segmented-like-dislike-button-view-model>
</div>
</ytd-menu-renderer>
</div>
${linkMarkup("watch-to-short", "Open Short", "shorts", nextVideoId, { controlDelay: 200 })}
${linkMarkup("watch-related", "Open related video", "watch", nextVideoId)}
${linkMarkup("watch-next", "Play next video", "watch", nextVideoId)}
<video id="fixture-media" data-fixture-autoplay-kind="watch" data-fixture-autoplay-id="${nextVideoId}"></video>
</section>
`;
}
function shortRendererMarkup(videoId, active) {
return `
<ytd-reel-video-renderer video-id="${videoId}" ${active ? "is-active" : "hidden"}>
<a href="/shorts/${videoId}" aria-label="Short ${videoId}"></a>
<reel-action-bar-view-model data-ryd-role="buttons">
<like-button-view-model
class="ytLikeButtonViewModelHost ytwReelActionBarViewModelHostDesktopActionButton"
data-ryd-role="like"
>
<label class="ytSpecButtonShapeWithLabelHost">
<button class="ytSpecButtonShapeNextHost" type="button" aria-label="100 likes" aria-pressed="false">
<span class="ytSpecButtonShapeNextIcon" aria-hidden="true">
<svg viewBox="0 0 24 24"><path d="M12 21"></path></svg>
</span>
</button>
<div class="ytSpecButtonShapeWithLabelLabel"><span id="text" role="text">100</span></div>
</label>
</like-button-view-model>
<button-view-model data-fixture-control="comments">
<button type="button" aria-label="View comments"><span role="text">12</span></button>
</button-view-model>
<button-view-model data-fixture-control="share">
<button type="button" aria-label="Share">Share</button>
</button-view-model>
<button-view-model data-fixture-control="remix">
<button type="button" aria-label="Remix">Remix</button>
</button-view-model>
</reel-action-bar-view-model>
</ytd-reel-video-renderer>
`;
}
function mobileShortRendererMarkup(videoId, active) {
return `
<ytm-reel-video-renderer
data-fixture-mobile-short="${videoId}"
video-id="${videoId}"
${active ? "is-active" : "hidden"}
>
<a href="/shorts/${videoId}" aria-label="Short ${videoId}"></a>
<ytm-reel-player-overlay-renderer>
<div id="like-button">
<ytm-like-button-renderer data-ryd-role="buttons">
<like-button-view-model class="style-text" data-ryd-role="like">
<button type="button" aria-label="100 likes" aria-pressed="false">
<span id="text" class="button-renderer-text" role="text">100</span>
</button>
</like-button-view-model>
<dislike-button-view-model class="style-text" data-ryd-role="dislike">
<button type="button" aria-label="Dislike this video" aria-pressed="false">
<span id="text" class="button-renderer-text" role="text"></span>
</button>
</dislike-button-view-model>
</ytm-like-button-renderer>
</div>
</ytm-reel-player-overlay-renderer>
</ytm-reel-video-renderer>
`;
}
function shortsMarkup(videoId) {
const nextVideoId = videoId === firstVideoId ? secondVideoId : firstVideoId;
return `
<section data-fixture-page-kind="shorts" data-fixture-video-id="${videoId}">
${
isMobile
? `<div id="fixture-shorts-reels">
${mobileShortRendererMarkup(videoId, true)}
${mobileShortRendererMarkup(nextVideoId, false)}
</div>`
: `<ytd-shorts>
<div id="fixture-shorts-reels">
${shortRendererMarkup(videoId, true)}
${shortRendererMarkup(nextVideoId, false)}
</div>
</ytd-shorts>`
}
${linkMarkup("short-to-watch", "Open video", "watch", nextVideoId, { controlDelay: 200 })}
${linkMarkup("short-next", "Play next Short", "shorts", nextVideoId)}
<video id="fixture-media" data-fixture-autoplay-kind="shorts" data-fixture-autoplay-id="${nextVideoId}"></video>
</section>
`;
}
function pageMarkup(pageKind, videoId) {
if (pageKind === "shorts") return shortsMarkup(videoId);
if (pageKind === "watch") return watchMarkup(videoId);
return channelMarkup();
}
function dispatchNavigation() {
document.dispatchEvent(new Event("yt-navigate-finish", { bubbles: true }));
}
function render(pageKind, videoId) {
page.innerHTML = pageMarkup(pageKind, videoId);
navigation.innerHTML =
pageKind === "channel"
? ""
: linkMarkup("return-to-channel", "Return to channel", "channel", "", { controlDelay: 20 });
}
function navigate(pageKind, videoId, { controlDelay = 0, dispatchEvent = true } = {}) {
history.pushState({}, "", hrefFor(pageKind, videoId));
if (controlDelay > 0) {
page.dataset.fixtureTransitionPending = pageKind;
if (dispatchEvent) dispatchNavigation();
setTimeout(() => {
delete page.dataset.fixtureTransitionPending;
render(pageKind, videoId);
}, controlDelay);
return;
}
render(pageKind, videoId);
if (dispatchEvent) dispatchNavigation();
}
function recycleShort(videoId) {
const section = page.querySelector('[data-fixture-page-kind="shorts"]');
const activeRenderer = section?.querySelector(
isMobile ? "ytm-reel-video-renderer[is-active]" : "ytd-reel-video-renderer[is-active]",
);
const nextRenderer = section?.querySelector(
isMobile
? `ytm-reel-video-renderer[video-id="${videoId}"]`
: `ytd-reel-video-renderer[video-id="${videoId}"]`,
);
if (!section || !activeRenderer || !nextRenderer) return;
history.pushState({}, "", hrefFor("shorts", videoId));
section.dataset.fixtureVideoId = videoId;
activeRenderer.removeAttribute("is-active");
activeRenderer.hidden = true;
nextRenderer.hidden = false;
nextRenderer.setAttribute("is-active", "");
const nextLink = section.querySelector("#short-next");
nextLink?.setAttribute("data-fixture-video-id", firstVideoId);
nextLink?.setAttribute("href", hrefFor("shorts", firstVideoId));
section.querySelector("#fixture-media")?.setAttribute("data-fixture-autoplay-id", firstVideoId);
}
function churnInactiveDesktopShortIdentity(sequence) {
const renderer = page.querySelector("ytd-reel-video-renderer:not([is-active])");
if (!renderer) return;
const churnVideoId = sequence % 2 === 0 ? secondVideoId : "CCCCCCCCCCC";
renderer.setAttribute("video-id", churnVideoId);
renderer.querySelector('a[href*="/shorts/"]')?.setAttribute("href", `/shorts/${churnVideoId}`);
}
function queueDelayedRender(pageKind, videoId) {
page.dataset.fixtureTransitionPending = pageKind;
pendingDelayedRender = () => {
delete page.dataset.fixtureTransitionPending;
render(pageKind, videoId);
};
}
function finishDelayedNavigation() {
if (!pendingDelayedRender) throw new Error("No delayed fixture navigation is pending");
const finish = pendingDelayedRender;
pendingDelayedRender = null;
finish();
}
function navigateDelayedShort(videoId) {
const rendererTag = isMobile ? "ytm-reel-video-renderer" : "ytd-reel-video-renderer";
const activeRenderer = page.querySelector(`${rendererTag}[is-active]`);
for (const renderer of page.querySelectorAll(`${rendererTag}:not([is-active])`)) {
renderer.remove();
}
if (!activeRenderer) return;
activeRenderer.querySelector('a[href*="/shorts/"]')?.setAttribute("href", `/shorts/${videoId}`);
history.pushState({}, "", hrefFor("shorts", videoId));
queueDelayedRender("shorts", videoId);
dispatchNavigation();
}
function anonymizeActiveDesktopShort() {
const activeRenderer = page.querySelector("ytd-reel-video-renderer[is-active]");
if (!activeRenderer) return;
activeRenderer.removeAttribute("video-id");
activeRenderer.querySelector('a[href*="/shorts/"]')?.removeAttribute("href");
}
function navigateDelayedAnonymousShort(videoId) {
const activeRenderer = page.querySelector("ytd-reel-video-renderer[is-active]");
for (const renderer of page.querySelectorAll("ytd-reel-video-renderer:not([is-active])")) {
renderer.remove();
}
if (!activeRenderer) return;
anonymizeActiveDesktopShort();
history.pushState({}, "", hrefFor("shorts", videoId));
queueDelayedRender("shorts", videoId);
dispatchNavigation();
}
function navigateDelayedWatch(videoId) {
const watch = page.querySelector("ytd-watch-flexy");
if (!watch) return;
history.pushState({}, "", hrefFor("watch", videoId));
watch.setAttribute("video-id", videoId);
queueDelayedRender("watch", videoId);
dispatchNavigation();
}
function beginSameNodeWatchNavigation(videoId) {
const watch = page.querySelector("ytd-watch-flexy");
if (!watch) return;
document.dispatchEvent(new Event("yt-navigate-start", { bubbles: true }));
history.pushState({}, "", hrefFor("watch", videoId));
watch.setAttribute("video-id", videoId);
}
function finishSameNodeWatchNavigation() {
dispatchNavigation();
}
function mutateOutgoingWatchDescendant() {
const dislike = page.querySelector('[data-fixture-page-kind="watch"] [data-ryd-role="dislike"]');
if (!dislike) return;
const countMarker = document.createElement("span");
countMarker.hidden = true;
countMarker.setAttribute("data-fixture-irrelevant-watch-count-mutation", "true");
dislike.querySelector("#text")?.appendChild(countMarker);
const tooltipMarker = document.createElement("span");
tooltipMarker.hidden = true;
tooltipMarker.setAttribute("data-fixture-irrelevant-watch-tooltip-mutation", "true");
page.querySelector(".ryd-tooltip")?.appendChild(tooltipMarker);
}
function replaceDelayedWatchControl(role) {
const control = page.querySelector(`[data-fixture-page-kind="watch"] [data-ryd-role="${role}"]`);
if (!control) return;
const replacement = control.cloneNode(true);
replacement.setAttribute("data-fixture-watch-replacement", role);
replacement.classList.remove("style-default-active");
replacement.classList.add("style-text");
replacement.querySelector("button")?.setAttribute("aria-pressed", "false");
if (role === "dislike") {
const count = replacement.querySelector("#text");
if (count) count.textContent = "";
}
control.replaceWith(replacement);
}
function replaceCurrentWatchActions({ retainOutgoing = false } = {}) {
const section = page.querySelector('[data-fixture-page-kind="watch"]');
const currentActions = section?.querySelector("#top-level-buttons-computed");
const videoId = section?.dataset.fixtureVideoId;
if (!section || !currentActions || !videoId) return false;
const replacement = currentActions.cloneNode(true);
replacement.setAttribute("data-fixture-watch-actions-replacement", videoId);
replacement.querySelectorAll(".ryd-tooltip").forEach((tooltip) => tooltip.remove());
const controls = replacement.querySelector('[data-ryd-role="buttons"]');
controls?.setAttribute("data-fixture-control-video-id", videoId);
for (const role of ["like", "dislike"]) {
const control = controls?.querySelector(`[data-ryd-role="${role}"]`);
control?.classList.remove("style-default-active");
control?.classList.add("style-text");
const button = control?.querySelector("button");
button?.setAttribute("aria-pressed", "false");
button?.removeAttribute("disabled");
button?.setAttribute("aria-disabled", "false");
}
const dislikeCount = controls?.querySelector('[data-ryd-role="dislike"] #text');
if (dislikeCount) dislikeCount.textContent = "";
currentActions.replaceWith(replacement);
globalThis.__fixtureReplacedWatchActions = currentActions;
if (retainOutgoing) {
const retained = document.createElement("div");
retained.hidden = true;
retained.setAttribute("data-fixture-retained-settling-watch-actions", videoId);
retained.appendChild(currentActions);
document.body.appendChild(retained);
}
return true;
}
function refreshReusedWatchControl(role, videoId) {
const control = page.querySelector(`[data-fixture-page-kind="watch"] [data-ryd-role="${role}"]`);
if (!control) return;
control.querySelector("button")?.setAttribute("aria-label", `${role} refreshed for ${videoId}`);
}
function driftWatchControlBeforeNavigation(role, videoId) {
const button = page.querySelector(`[data-fixture-page-kind="watch"] [data-ryd-role="${role}"] button`);
if (!button) return;
button.setAttribute("aria-label", `${role} drift while ${videoId}`);
button.setAttribute("title", `${role} title drift while ${videoId}`);
}
function replaceActiveMobileOverlay() {
const activeRenderer = page.querySelector("ytm-reel-video-renderer[is-active]");
const overlay = activeRenderer?.querySelector("ytm-reel-player-overlay-renderer");
if (!overlay) return;
const replacement = overlay.cloneNode(true);
replacement.setAttribute("data-fixture-replacement", "true");
const dislike = replacement.querySelector('[data-ryd-role="dislike"]');
dislike?.classList.remove("style-default-active");
dislike?.classList.add("style-text");
dislike?.querySelector("button")?.setAttribute("aria-pressed", "false");
const dislikeCount = dislike?.querySelector("#text");
if (dislikeCount) dislikeCount.textContent = "";
overlay.replaceWith(replacement);
}
function navigateToShortsDescendant(videoId) {
history.pushState({}, "", `/shorts/${videoId}/extra?${marker}`);
dispatchNavigation();
}
document.addEventListener("click", (event) => {
const link = event.target.closest("a[data-fixture-page-kind]");
if (!link) return;
event.preventDefault();
if (link.id === "short-next") {
recycleShort(link.dataset.fixtureVideoId);
dispatchNavigation();
return;
}
navigate(link.dataset.fixturePageKind, link.dataset.fixtureVideoId, {
controlDelay: Number(link.dataset.fixtureControlDelay || 0),
});
});
document.addEventListener(
"ended",
(event) => {
const media = event.target.closest("#fixture-media");
if (!media) return;
if (media.dataset.fixtureAutoplayKind === "shorts") {
recycleShort(media.dataset.fixtureAutoplayId);
return;
}
navigate(media.dataset.fixtureAutoplayKind, media.dataset.fixtureAutoplayId, {
dispatchEvent: false,
});
},
true,
);
window.__navigationFixture = {
anonymizeActiveDesktopShort,
beginSameNodeWatchNavigation,
churnInactiveDesktopShortIdentity,
dispatchEnded() {
page.querySelector("#fixture-media")?.dispatchEvent(new Event("ended"));
},
finishDelayedNavigation,
finishSameNodeWatchNavigation,
driftWatchControlBeforeNavigation,
navigate,
navigateDelayedAnonymousShort,
navigateDelayedShort,
navigateDelayedWatch,
navigateToShortsDescendant,
mutateOutgoingWatchDescendant,
recycleShort,
replaceActiveMobileOverlay,
replaceCurrentWatchActions,
replaceDelayedWatchControl,
refreshReusedWatchControl,
};
render("__INITIAL_PAGE_KIND__", "__VIDEO_ID__");
})();
</script>
</body>
</html>
@@ -0,0 +1,348 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Userscript Shorts browser-test fixture</title>
<style>
html,
body {
width: 100%;
min-height: 768px;
margin: 0;
}
#shorts-root,
[data-short-video],
ytd-reel-video-renderer,
reel-action-bar-view-model,
ytd-like-button-renderer,
ytm-like-button-renderer,
like-button-view-model,
dislike-button-view-model {
display: block;
}
[data-short-video][hidden] {
display: none;
}
ytd-like-button-renderer,
ytm-like-button-renderer {
width: 240px;
min-height: 96px;
}
ytd-reel-video-renderer {
display: block;
width: 420px;
min-height: 640px;
}
reel-action-bar-view-model {
align-items: flex-start;
display: flex;
flex-direction: column;
width: 48px;
min-height: 320px;
}
.ytwReelActionBarViewModelHostDesktopActionButton,
reel-action-bar-view-model > button-view-model {
box-sizing: content-box;
display: block;
height: 70px;
margin: 0;
padding: 0 0 8px;
width: 48px;
}
.ytSpecButtonShapeWithLabelHost {
align-items: center;
display: flex;
flex-direction: column;
height: 70px;
margin: 0;
padding: 0;
width: 48px;
}
reel-action-bar-view-model button {
align-items: center;
border: 0;
box-sizing: border-box;
display: flex;
height: 48px;
justify-content: center;
margin: 0;
min-height: 0;
min-width: 0;
padding: 12px;
width: 48px;
}
like-button-view-model .ytSpecButtonShapeNextIcon {
display: flex;
height: 24px;
width: 24px;
}
like-button-view-model .ytSpecButtonShapeNextIcon svg {
display: block;
height: 24px;
width: 24px;
}
.ytSpecButtonShapeWithLabelLabel {
align-items: center;
box-sizing: border-box;
display: flex;
font-family: Arial, sans-serif;
font-size: 12px;
height: 22px;
justify-content: center;
line-height: 18px;
width: 48px;
}
ytm-like-button-renderer button,
ytd-like-button-renderer button {
min-height: 36px;
min-width: 96px;
}
</style>
</head>
<body>
<div id="fixture-account"></div>
<div id="shorts-root"></div>
<script>
(() => {
const root = document.getElementById("shorts-root");
const isMobile = location.hostname === "m.youtube.com";
if (!customElements.get("like-button-view-model")) {
customElements.define(
"like-button-view-model",
class extends HTMLElement {
get className() {
return undefined;
}
},
);
}
document.getElementById("fixture-account").innerHTML = __SIGNED_IN__
? '<button id="avatar-btn" aria-label="Account menu">Account</button>'
: '<a id="sign-in" href="https://accounts.google.com/ServiceLogin">Sign in</a>';
function controlsMarkup(videoId) {
if (!isMobile) {
return `
<section data-short-video="${videoId}" hidden>
<ytd-reel-video-renderer video-id="${videoId}">
<a href="/shorts/${videoId}" aria-label="Short ${videoId}"></a>
<reel-action-bar-view-model data-ryd-role="buttons">
<like-button-view-model
class="ytLikeButtonViewModelHost ytwReelActionBarViewModelHostDesktopActionButton"
data-ryd-role="like"
>
<label class="ytSpecButtonShapeWithLabelHost">
<button class="ytSpecButtonShapeNextHost" type="button" aria-label="100 likes" aria-pressed="false">
<span class="ytSpecButtonShapeNextIcon" aria-hidden="true"><svg viewBox="0 0 24 24"><path d="M12 21"></path></svg></span>
</button>
<div class="ytSpecButtonShapeWithLabelLabel">
<span
id="text"
class="ytAttributedStringHost ytAttributedStringTextAlignmentCenter"
role="text"
>100</span>
</div>
</label>
</like-button-view-model>
<button-view-model data-fixture-control="comments">
<button type="button" aria-label="View comments"><span role="text">12</span></button>
</button-view-model>
<button-view-model data-fixture-control="share">
<button type="button" aria-label="Share"><span role="text">Share</span></button>
</button-view-model>
<button-view-model data-fixture-control="remix">
<button type="button" aria-label="Remix"><span role="text">Remix</span></button>
</button-view-model>
</reel-action-bar-view-model>
</ytd-reel-video-renderer>
</section>
`;
}
return `
<section data-short-video="${videoId}" hidden>
<div id="like-button">
<__SHORTS_RENDERER_TAG__ data-ryd-role="buttons">
<like-button-view-model class="style-text" data-ryd-role="like">
<button type="button" aria-label="100 likes" aria-pressed="false">
<span id="text" class="button-renderer-text" role="text">100</span>
</button>
</like-button-view-model>
<dislike-button-view-model class="style-text" data-ryd-role="dislike">
<button type="button" aria-label="Dislike this video" aria-pressed="false">
<span id="text" class="button-renderer-text" role="text"></span>
</button>
</dislike-button-view-model>
</__SHORTS_RENDERER_TAG__>
</div>
</section>
`;
}
function applyNativeState(section, state) {
const like = section.querySelector('[data-ryd-role="like"]');
const dislike = section.querySelector('[data-ryd-role="dislike"]');
if (isMobile) {
like.classList.toggle("style-default-active", state === "liked");
like.classList.toggle("style-text", state !== "liked");
dislike?.classList.toggle("style-default-active", state === "disliked");
dislike?.classList.toggle("style-text", state !== "disliked");
} else {
like.classList.toggle("style-default-active", state === "liked");
dislike?.classList.toggle("style-default-active", state === "disliked");
}
like.querySelector("button").setAttribute("aria-pressed", String(state === "liked"));
dislike?.querySelector("button")?.setAttribute("aria-pressed", String(state === "disliked"));
section.dataset.nativeState = state;
}
function ensureVideo(videoId) {
let section = root.querySelector(`[data-short-video="${videoId}"]`);
if (!section) {
root.insertAdjacentHTML("beforeend", controlsMarkup(videoId));
section = root.querySelector(`[data-short-video="${videoId}"]`);
applyNativeState(section, "neutral");
}
return section;
}
function activate(videoId, { dispatchNavigation = true, state = "neutral" } = {}) {
const active = ensureVideo(videoId);
for (const section of root.querySelectorAll("[data-short-video]")) {
section.hidden = section !== active;
}
applyNativeState(active, state);
history.pushState({}, "", `/shorts/${videoId}`);
if (dispatchNavigation) {
document.dispatchEvent(new Event("yt-navigate-finish", { bubbles: true }));
}
}
function activeSection() {
return root.querySelector("[data-short-video]:not([hidden])");
}
function removeSyntheticDislike() {
activeSection()?.querySelector("[data-ryd-synthetic-shorts-dislike]")?.remove();
}
function replaceActionBar() {
const section = activeSection();
const actionBar = section?.querySelector("reel-action-bar-view-model");
if (!actionBar) return;
const replacement = actionBar.cloneNode(true);
replacement.querySelector("[data-ryd-synthetic-shorts-dislike]")?.remove();
actionBar.replaceWith(replacement);
applyNativeState(section, section.dataset.nativeState || "neutral");
}
function installNativeDislike() {
const section = activeSection();
const actionBar = section?.querySelector("reel-action-bar-view-model");
if (!actionBar || actionBar.querySelector("dislike-button-view-model")) return;
const nativeDislike = document.createElement("dislike-button-view-model");
nativeDislike.className = "style-text";
nativeDislike.setAttribute("data-ryd-role", "dislike");
const button = document.createElement("button");
button.type = "button";
button.setAttribute("aria-label", "Dislike this video");
button.setAttribute("aria-pressed", "false");
const text = document.createElement("span");
text.id = "text";
text.setAttribute("role", "text");
button.appendChild(text);
nativeDislike.appendChild(button);
actionBar.insertBefore(nativeDislike, actionBar.children[1] ?? null);
}
function removeNativeDislike() {
activeSection()?.querySelector("dislike-button-view-model")?.remove();
}
function replaceInnerButton(role) {
const control = activeSection()?.querySelector(`[data-ryd-role="${role}"]`);
const button = control?.querySelector("button");
if (!button) return;
const replacement = button.cloneNode(true);
if (control.matches("[data-ryd-synthetic-shorts-dislike]")) {
replacement.disabled = true;
replacement.setAttribute("aria-disabled", "true");
}
button.replaceWith(replacement);
}
function recycleActiveRenderer(videoId, { state = "neutral" } = {}) {
const section = activeSection();
const renderer = section?.querySelector("ytd-reel-video-renderer");
if (!section || !renderer) return;
section.setAttribute("data-short-video", videoId);
renderer.setAttribute("video-id", videoId);
const link = renderer.querySelector('a[href*="/shorts/"]');
link?.setAttribute("href", `/shorts/${videoId}`);
applyNativeState(section, state);
history.pushState({}, "", `/shorts/${videoId}`);
document.dispatchEvent(new Event("yt-navigate-finish", { bubbles: true }));
}
function setClassOnlyLiked() {
const section = activeSection();
if (!section) return;
applyNativeState(section, "liked");
section.querySelector('[data-ryd-role="like"]').classList.add("style-default-active");
section.querySelector('[data-ryd-role="like"] button').setAttribute("aria-pressed", "false");
}
root.addEventListener(
"click",
(event) => {
const actionButton = event.target.closest("button");
if (!actionButton) return;
const section = event.target.closest("[data-short-video]");
if (!section) return;
const current = section.dataset.nativeState || "neutral";
if (actionButton.closest('[data-ryd-role="like"]')) {
applyNativeState(section, current === "liked" ? "neutral" : "liked");
}
if (
actionButton.closest('[data-ryd-role="dislike"]') &&
!actionButton.closest("[data-ryd-synthetic-shorts-dislike]")
) {
applyNativeState(section, current === "disliked" ? "neutral" : "disliked");
}
},
true,
);
window.__shortsFixture = {
activate,
activeSection,
ensureVideo,
installNativeDislike,
recycleActiveRenderer,
replaceInnerButton,
removeNativeDislike,
removeSyntheticDislike,
replaceActionBar,
setClassOnlyLiked,
};
activate("__VIDEO_ID__", { dispatchNavigation: false, state: "__INITIAL_STATE__" });
ensureVideo("__SECOND_VIDEO_ID__");
})();
</script>
</body>
</html>
@@ -0,0 +1,250 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Userscript browser-test fixture</title>
<style>
:root {
--yt-spec-10-percent-layer: rgba(255, 255, 255, 0.1);
--yt-spec-base-background: rgb(15, 15, 15);
--yt-spec-icon-disabled: rgba(255, 255, 255, 0.3);
--yt-spec-text-primary: rgb(241, 241, 241);
--yt-spec-text-secondary: rgb(170, 170, 170);
}
html,
body {
background: rgb(15, 15, 15);
width: 100%;
min-height: 768px;
margin: 0;
}
ytd-watch-flexy,
ytd-menu-renderer,
.slim-video-action-bar-actions,
.segmented-buttons,
segmented-like-dislike-button-view-model,
like-button-view-model,
dislike-button-view-model {
display: block;
}
ytd-menu-renderer.ytd-watch-metadata > div {
display: block;
width: min(320px, 100%);
min-height: 36px;
}
#top-row {
width: min(320px, 100%);
}
segmented-like-dislike-button-view-model {
display: flex;
gap: 0;
}
.segmented-buttons {
display: flex;
gap: 8px;
width: 320px;
min-height: 48px;
}
[data-fixture-shell][hidden] {
display: none;
}
[data-ryd-role="like"],
[data-ryd-role="dislike"] {
box-sizing: border-box;
flex: 0 0 96px;
height: 36px;
margin: 0;
padding: 0;
width: 96px;
}
[data-ryd-role="like"] button,
[data-ryd-role="dislike"] button {
align-items: center;
background: rgb(39, 39, 39);
border: 0;
border-radius: 18px;
box-sizing: border-box;
color: rgb(241, 241, 241);
display: inline-flex;
font:
500 14px/20px Arial,
sans-serif;
gap: 6px;
height: 36px;
justify-content: center;
margin: 0;
min-height: 36px;
min-width: 96px;
padding: 0 12px;
width: 96px;
}
[data-ryd-role].style-default-active button {
background: rgb(241, 241, 241);
color: rgb(15, 15, 15);
}
[data-fixture-icon] {
display: block;
flex: 0 0 20px;
height: 20px;
margin: 0;
padding: 0;
width: 20px;
}
[data-ryd-role] #text {
display: block;
font:
500 14px/20px Arial,
sans-serif;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<!-- A hidden menu-container selects the desktop metadata layout used by the real page. -->
<div id="menu-container" style="display: none"></div>
<div id="fixture-account"></div>
<div id="player" loading="false"></div>
<ytd-watch-flexy id="fixture-watch" video-id="__VIDEO_ID__"></ytd-watch-flexy>
<div id="top-row" data-fixture-shell="desktop">
<ytd-menu-renderer class="ytd-watch-metadata">
<div id="top-level-buttons-computed"></div>
</ytd-menu-renderer>
</div>
<div class="slim-video-action-bar-actions" data-fixture-shell="mobile">
<div id="fixture-mobile-buttons-host" class="segmented-buttons"></div>
</div>
<script>
(() => {
const account = document.getElementById("fixture-account");
const watch = document.getElementById("fixture-watch");
const isMobile = location.hostname === "m.youtube.com";
const desktopHost = document.getElementById("top-level-buttons-computed");
const mobileHost = document.getElementById("fixture-mobile-buttons-host");
const host = isMobile ? mobileHost : desktopHost;
document.querySelector('[data-fixture-shell="desktop"]').hidden = isMobile;
document.querySelector('[data-fixture-shell="mobile"]').hidden = !isMobile;
function setSignedIn(signedIn) {
account.innerHTML = signedIn
? '<button id="avatar-btn" aria-label="Account menu">Account</button>'
: '<a id="sign-in" href="https://accounts.google.com/ServiceLogin">Sign in</a>';
}
function desktopButtonMarkup() {
return `
<segmented-like-dislike-button-view-model data-ryd-role="buttons">
<like-button-view-model class="style-text" data-ryd-role="like">
<button type="button" aria-label="100 likes" aria-pressed="false">
<svg data-fixture-icon="like" aria-hidden="true" viewBox="0 0 24 24">
<path fill="currentColor" d="M8 21H5V9h3v12Zm2 0V9l4-7 2 1v5h5v5l-3 8h-8Z"></path>
</svg>
<span id="text" role="text">100</span>
</button>
</like-button-view-model>
<dislike-button-view-model class="style-text" data-ryd-role="dislike">
<button type="button" aria-label="Dislike this video" aria-pressed="false">
<svg data-fixture-icon="dislike" aria-hidden="true" viewBox="0 0 24 24">
<path fill="currentColor" d="M16 3h3v12h-3V3Zm-2 0v12l-4 7-2-1v-5H3v-5l3-8h8Z"></path>
</svg>
<span id="text" role="text"></span>
</button>
</dislike-button-view-model>
</segmented-like-dislike-button-view-model>
`;
}
function mobileButtonMarkup() {
return `
<like-button-view-model class="style-text" data-ryd-role="like">
<button type="button" aria-label="100 likes" aria-pressed="false">
<svg data-fixture-icon="like" aria-hidden="true" viewBox="0 0 24 24">
<path fill="currentColor" d="M8 21H5V9h3v12Zm2 0V9l4-7 2 1v5h5v5l-3 8h-8Z"></path>
</svg>
<span id="text" class="button-renderer-text" role="text">100</span>
</button>
</like-button-view-model>
<dislike-button-view-model class="style-text" data-ryd-role="dislike">
<button type="button" aria-label="Dislike this video" aria-pressed="false">
<svg data-fixture-icon="dislike" aria-hidden="true" viewBox="0 0 24 24">
<path fill="currentColor" d="M16 3h3v12h-3V3Zm-2 0v12l-4 7-2-1v-5H3v-5l3-8h8Z"></path>
</svg>
<span id="text" class="button-renderer-text" role="text"></span>
</button>
</dislike-button-view-model>
`;
}
function applyNativeState(state) {
const like = host.querySelector('[data-ryd-role="like"]');
const dislike = host.querySelector('[data-ryd-role="dislike"]');
if (!like || !dislike) return;
like.classList.toggle("style-default-active", state === "liked");
like.classList.toggle("style-text", state !== "liked");
dislike.classList.toggle("style-default-active", state === "disliked");
dislike.classList.toggle("style-text", state !== "disliked");
like.querySelector("button").setAttribute("aria-pressed", String(state === "liked"));
dislike.querySelector("button").setAttribute("aria-pressed", String(state === "disliked"));
host.dataset.nativeState = state;
}
function insertButtons(state = "neutral") {
host.innerHTML = isMobile ? mobileButtonMarkup() : desktopButtonMarkup();
applyNativeState(state);
}
function removeButtons() {
host.replaceChildren();
host.dataset.nativeState = "neutral";
}
function navigate(videoId, { state = "neutral", buttons = true } = {}) {
history.pushState({}, "", `/watch?v=${videoId}`);
watch.setAttribute("video-id", videoId);
if (buttons) insertButtons(state);
else removeButtons();
document.dispatchEvent(new Event("yt-navigate-finish", { bubbles: true }));
}
// Model the native YouTube toggle before userscript listeners observe the bubbled click.
host.addEventListener(
"click",
(event) => {
const like = event.target.closest('[data-ryd-role="like"]');
const dislike = event.target.closest('[data-ryd-role="dislike"]');
const current = host.dataset.nativeState || "neutral";
if (like) applyNativeState(current === "liked" ? "neutral" : "liked");
if (dislike) applyNativeState(current === "disliked" ? "neutral" : "disliked");
},
true,
);
window.__youtubeFixture = {
applyNativeState,
insertButtons,
navigate,
removeButtons,
setSignedIn,
};
setSignedIn(__SIGNED_IN__);
if (__INITIAL_BUTTONS__) insertButtons("__INITIAL_STATE__");
})();
</script>
</body>
</html>