Skip to content

Commit db1c442

Browse files
committed
chore: added getVideoData for twitch and improves getVideoId
1 parent 558d1f0 commit db1c442

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

packages/ext/src/data/sites.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export default [
100100
/^clips.twitch.tv$/,
101101
/^player.twitch.tv$/,
102102
],
103+
needExtraData: true,
103104
selector: ".video-ref, main > div > section > div > div > div",
104105
},
105106
{

packages/ext/src/helpers/twitch.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BaseHelper, VideoHelperError } from "./base";
2+
import { MinimalVideoData } from "../types/client";
23

34
import * as Sap from "@vot.js/shared/types/helpers/sap";
45

@@ -38,7 +39,23 @@ export default class TwitchHelper extends BaseHelper {
3839
return `${channelName}/clip/${isEmbed ? clipId : clearPathname}`;
3940
}
4041

41-
async getVideoId(url: URL) {
42+
// eslint-disable-next-line @typescript-eslint/require-await
43+
async getVideoData(videoId: string): Promise<MinimalVideoData | undefined> {
44+
const title = document.querySelector<HTMLElement>(
45+
'[data-a-target="stream-title"], [data-test-selector="stream-info-card-component__subtitle"]',
46+
)?.innerText;
47+
const isStream = !!document.querySelector(
48+
'[data-a-target="animated-channel-viewers-count"], .channel-status-info--live, .top-bar--pointer-enabled .tw-channel-status-text-indicator',
49+
);
50+
51+
return {
52+
url: this.service!.url + videoId,
53+
isStream,
54+
title,
55+
};
56+
}
57+
58+
async getVideoId(url: URL): Promise<string | undefined> {
4259
const pathname = url.pathname;
4360
if (/^m\.twitch\.tv$/.test(pathname)) {
4461
return /videos\/([^/]+)/.exec(url.href)?.[0] ?? pathname.slice(1);
@@ -56,6 +73,19 @@ export default class TwitchHelper extends BaseHelper {
5673
return await this.getClipLink(pathname, url.searchParams.get("clip"));
5774
}
5875

59-
return /(?:videos)\/([^/]+)/.exec(pathname)?.[0];
76+
const videoPath = /(?:videos)\/([^/]+)/.exec(pathname);
77+
if (videoPath) {
78+
return videoPath[0];
79+
}
80+
81+
const isUserOfflinePage = document.querySelector<HTMLLinkElement>(
82+
".home-offline-hero .tw-link",
83+
);
84+
if (isUserOfflinePage?.href) {
85+
const pageUrl = new URL(isUserOfflinePage.href);
86+
return /(?:videos)\/([^/]+)/.exec(pageUrl.pathname)?.[0];
87+
}
88+
89+
return document.querySelector(".persistent-player") ? pathname : undefined;
6090
}
6191
}

0 commit comments

Comments
 (0)