Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(YouTube): add support for new layout #8292

Merged
merged 4 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion websites/Y/YouTube/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"*://www.youtube.com/*",
"*://studio.youtube.com/*"
],
"version": "5.10.3",
"version": "5.10.4",
"logo": "https://cdn.rcd.gg/PreMiD/websites/Y/YouTube/assets/logo.png",
"thumbnail": "https://cdn.rcd.gg/PreMiD/websites/Y/YouTube/assets/thumbnail.jpg",
"color": "#E40813",
Expand Down
15 changes: 10 additions & 5 deletions websites/Y/YouTube/presence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import youtubeEmbedResolver from "./video_sources/embed";
import youtubeMoviesResolver from "./video_sources/movies";
import youtubeTVResolver from "./video_sources/tv";
import youtubeResolver from "./video_sources/default";
import youtubeMiniplayerResolver from "./video_sources/miniplayer";
import youtubeApiResolver from "./video_sources/api";
import {
Resolver,
Expand Down Expand Up @@ -79,6 +80,7 @@ presence.on("UpdateData", async () => {
youtubeOldResolver,
youtubeTVResolver,
youtubeResolver,
youtubeMiniplayerResolver,
youtubeMoviesResolver,
youtubeApiResolver,
nullResolver,
Expand Down Expand Up @@ -114,11 +116,14 @@ presence.on("UpdateData", async () => {
}

if (logo === LogoMode.Channel) {
pfp = document
.querySelector<HTMLImageElement>(
"#avatar.ytd-video-owner-renderer > img"
)
?.src.replace(/=s\d+/, "=s512");
pfp =
resolver === youtubeMiniplayerResolver
? ""
: document
.querySelector<HTMLImageElement>(
"#avatar.ytd-video-owner-renderer > img"
)
?.src.replace(/=s\d+/, "=s512");
}
const unlistedPathElement = document.querySelector<SVGPathElement>(
"g#privacy_unlisted > path"
Expand Down
6 changes: 3 additions & 3 deletions websites/Y/YouTube/video_sources/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ function isActive(): boolean {
}

function getTitle(): string {
return videoCache.get(getVideoID())?.videoDetails.title;
return videoCache.get(getVideoID())?.videoDetails?.title;
}

function getUploader(): string {
return videoCache.get(getVideoID())?.videoDetails.author;
return videoCache.get(getVideoID())?.videoDetails?.author;
}

export function getVideoID(): string {
Expand All @@ -84,7 +84,7 @@ export function getVideoID(): string {

export function getChannelURL(): string {
return `https://www.youtube.com/channel/${
videoCache.get(getVideoID())?.videoDetails.channelId
videoCache.get(getVideoID())?.videoDetails?.channelId
}`;
}

Expand Down
35 changes: 18 additions & 17 deletions websites/Y/YouTube/video_sources/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,36 @@ function isActive(): boolean {
}

function getTitle(): string {
if (document.location.pathname.includes("/watch")) {
return document
.querySelector("h1 yt-formatted-string.ytd-video-primary-info-renderer")
?.textContent.trim();
} else
return document.querySelector(".ytd-miniplayer .title")?.textContent.trim();
return getBaseSection()
?.querySelector("h1 yt-formatted-string.ytd-video-primary-info-renderer")
?.textContent.trim();
}

function getUploader(): string {
return (
document
.querySelector("ytd-video-owner-renderer .ytd-channel-name a")
?.textContent.trim() ||
document.querySelector("yt-formatted-string#owner-name")?.textContent.trim()
);
return getBaseSection()
?.querySelector("ytd-video-owner-renderer .ytd-channel-name a")
?.textContent.trim();
}

export function getVideoID(): string {
return document
.querySelector("#page-manager > ytd-watch-flexy")
?.getAttribute("video-id");
return (
getBaseSection()
?.querySelector("#page-manager > [video-id]")
?.getAttribute("video-id") ??
new URLSearchParams(document.location.search).get("v")
);
}

export function getChannelURL(): string {
return document.querySelector<HTMLLinkElement>(
"#top-row ytd-video-owner-renderer > a"
return getBaseSection()?.querySelector<HTMLAnchorElement>(
"#upload-info #channel-name a"
)?.href;
}

function getBaseSection(): HTMLElement | null {
return document.querySelector(".ytd-page-manager:not([hidden])");
}

const resolver: Resolver = {
isActive,
getTitle,
Expand Down
37 changes: 37 additions & 0 deletions websites/Y/YouTube/video_sources/miniplayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Resolver } from "../util";

function isActive(): boolean {
return !!getTitle() && !!getUploader() && !!getVideoID() && !!getChannelURL();
}

function getTitle(): string {
return document.querySelector(".ytd-miniplayer .title")?.textContent.trim();
}

function getUploader(): string {
return document.querySelector("#owner-name")?.textContent.trim();
}

function getVideoID(): string {
const link =
document.querySelector<HTMLAnchorElement>("#video-title-link").href;
if (!link) return null;
const url = new URL(link);

Check warning on line 19 in websites/Y/YouTube/video_sources/miniplayer.ts

View workflow job for this annotation

GitHub Actions / Compile and Lint

Variable 'url' is only used once
return url.searchParams.get("v");
}

function getChannelURL(): string {
return document.querySelector<HTMLAnchorElement>(
"#ytd-player .ytp-ce-channel-title.ytp-ce-link"
)?.href;
}

const resolver: Resolver = {
isActive,
getTitle,
getUploader,
getChannelURL,
getVideoID,
};

export default resolver;
8 changes: 7 additions & 1 deletion websites/Y/YouTube/video_sources/old.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Resolver } from "../util";
import { getChannelURL, getVideoID } from "./default";
import { getVideoID } from "./default";

function isActive(): boolean {
return (
Expand All @@ -12,6 +12,12 @@ function isActive(): boolean {
);
}

function getChannelURL(): string {
return document.querySelector<HTMLLinkElement>(
"#top-row ytd-video-owner-renderer > a"
)?.href;
}

function getTitle(): string {
return document.querySelector(".watch-title")?.textContent.trim();
}
Expand Down
Loading