Skip to content

Commit e268f2e

Browse files
committed
refactor: improve readability
1 parent 5a280b2 commit e268f2e

File tree

3 files changed

+70
-73
lines changed

3 files changed

+70
-73
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
export default class Page {
2-
public get isOnSupportedPage() {
3-
return this.videoKind !== null;
1+
export default class ChannelPage {
2+
public static get isOnSupportedPage() {
3+
return !!this.videoKind;
44
}
55

6-
public get videoKind(): VideoKind {
6+
public static get videoKind(): VideoKindNullable {
77
const videoKind = window.location.pathname.split("/").at(-1)!.split("?")[0];
88
switch (videoKind) {
99
case "videos":
@@ -17,7 +17,7 @@ export default class Page {
1717
}
1818
}
1919

20-
public get sortKind(): SortKind {
20+
public static get sortKind(): SortKindNullable {
2121
const selectedButton = document.querySelector("#primary #header #chips>[selected]");
2222
const index = selectedButton
2323
? Array.from(selectedButton.parentNode?.children ?? []).indexOf(selectedButton)
@@ -34,63 +34,29 @@ export default class Page {
3434
}
3535
}
3636

37-
public get hasPlayAllButton() {
37+
public static get hasPlayAllButton() {
3838
return !!document.querySelector(".play-all-btn");
3939
}
4040

4141
public channelId: string;
42+
4243
public constructor(channelId: string) {
4344
this.channelId = channelId;
4445
}
4546

46-
public static applyStyleForPlayAllButton() {
47-
const style = document.createElement("style");
48-
style.textContent = `
49-
.play-all-btn {
50-
background-color: #8000FF;
51-
color: #F1F1F1;
52-
53-
height: 32px;
54-
min-width: 12px;
55-
56-
display: inline-flex;
57-
flex-direction: row;
58-
align-items: center;
59-
justify-content: center;
60-
margin-left: 12px;
61-
62-
border-radius: 8px;
63-
padding: 0 12px;
64-
65-
font-family: 'Roboto', 'Arial', sans-serif;
66-
font-size: 1.4rem;
67-
font-weight: 500;
68-
text-decoration: none;
69-
70-
cursor: pointer;
71-
}
72-
73-
.play-all-btn:hover,
74-
.play-all-btn:focus {
75-
background-color:#9B33FF;
76-
}
77-
`;
78-
document.head.appendChild(style);
79-
}
80-
81-
public ensurePlayAllButton(channelId: string) {
82-
if (!this.hasPlayAllButton) {
83-
this.addPlayAllButton(channelId);
47+
public ensurePlayAllButton() {
48+
if (!ChannelPage.hasPlayAllButton) {
49+
this.addPlayAllButton(ChannelPage.videoKind, ChannelPage.sortKind);
8450
}
8551
}
8652

87-
public addPlayAllButton(channelId: string) {
53+
public addPlayAllButton(videoKind: VideoKindNullable, sortKind: SortKindNullable) {
8854
const playAllButton = document.createElement("a");
89-
const playListPath = this._getPlayListPath(channelId);
9055
playAllButton.classList.add("play-all-btn");
91-
if (playListPath) {
56+
if (videoKind && sortKind) {
57+
const playListPath = this._getPlayListPath(videoKind, sortKind);
9258
playAllButton.href = playListPath;
93-
playAllButton.textContent = `Play All (${this.sortKind})`;
59+
playAllButton.textContent = `Play All (${sortKind})`;
9460
} else {
9561
playAllButton.textContent = `Play All (Not Available)`;
9662
}
@@ -99,17 +65,17 @@ export default class Page {
9965
buttonHolder?.appendChild(playAllButton);
10066
}
10167

102-
private _getPlayListPath(channelId: string): string {
103-
if (this.sortKind === "Oldest") {
68+
private _getPlayListPath(videoKind: VideoKind, sortKind: SortKind): string {
69+
if (sortKind === "Oldest") {
10470
const oldestVideoHref = document.querySelector<HTMLLinkElement>(
10571
"ytd-browse [href^='/watch?v='],ytd-browse [href^='/shorts/']",
10672
)?.href;
10773
const videoId = oldestVideoHref?.match(/(?:watch\?v=|shorts\/)([^&]*)/)?.at(1);
10874
return videoId ? `/watch?v=${videoId}&list=UL01234567890` : "";
10975
} else {
110-
if (channelId && this.videoKind && this.sortKind) {
111-
const playlistPrefix = this._getPlayListPrefix(this.videoKind, this.sortKind);
112-
return `/playlist?list=${playlistPrefix}${channelId}&playnext=1`;
76+
if (videoKind && sortKind) {
77+
const playlistPrefix = this._getPlayListPrefix(videoKind, sortKind);
78+
return `/playlist?list=${playlistPrefix}${this.channelId}&playnext=1`;
11379
} else {
11480
return "";
11581
}
@@ -136,5 +102,8 @@ export default class Page {
136102
}
137103
}
138104

139-
type VideoKind = "Videos" | "Shorts" | "Streams" | null;
140-
type SortKind = "Latest" | "Popular" | "Oldest" | null;
105+
type VideoKind = "Videos" | "Shorts" | "Streams";
106+
type SortKind = "Latest" | "Popular" | "Oldest";
107+
108+
type VideoKindNullable = VideoKind | null;
109+
type SortKindNullable = SortKind | null;

src/entrypoints/content/index.ts

+10-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { defineContentScript } from "wxt/sandbox";
1+
import { defineContentScript } from "wxt/sandbox";
22

3-
import Page from "./page";
3+
import ChannelPage from "./channel-page";
4+
import YoutubePage from "./youtube-page";
45

56
export default defineContentScript({
67
matches: ["https://www.youtube.com/*"],
@@ -9,33 +10,24 @@ export default defineContentScript({
910
});
1011

1112
function main() {
12-
Page.applyStyleForPlayAllButton();
13-
14-
let page = new Page("");
15-
let observer: MutationObserver | null = null;
13+
YoutubePage.applyStyleForPlayAllButton();
1614

1715
// Triggered when navigating to the videos, shorts, or streams page
1816
window.addEventListener("yt-navigate-finish", (e: any) => {
19-
if (!page.isOnSupportedPage) {
17+
if (!ChannelPage.isOnSupportedPage) {
2018
return;
2119
}
2220

2321
const channelId = e.detail.endpoint.browseEndpoint.browseId.slice(2);
24-
if (page.channelId !== channelId) {
25-
page = new Page(channelId);
26-
observer?.disconnect();
27-
}
22+
const channelPage = new ChannelPage(channelId);
23+
channelPage.ensurePlayAllButton();
2824

29-
observer = new MutationObserver(() => {
30-
if (page.isOnSupportedPage) {
31-
page.ensurePlayAllButton(channelId);
25+
const observer = new MutationObserver(() => {
26+
if (ChannelPage.isOnSupportedPage) {
27+
channelPage.ensurePlayAllButton();
3228
}
3329
});
3430

35-
observer.disconnect();
36-
37-
page.ensurePlayAllButton(channelId);
38-
3931
// Callback will be triggered when changing the sort to latest/popular/oldest
4032
const buttonHolder = document.querySelector("#primary #header #chips")!;
4133
observer.observe(buttonHolder, {
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export default class YoutubePage {
2+
public static applyStyleForPlayAllButton() {
3+
const style = document.createElement("style");
4+
style.textContent = `
5+
.play-all-btn {
6+
background-color: #8000FF;
7+
color: #F1F1F1;
8+
9+
height: 32px;
10+
min-width: 12px;
11+
12+
display: inline-flex;
13+
flex-direction: row;
14+
align-items: center;
15+
justify-content: center;
16+
margin-left: 12px;
17+
18+
border-radius: 8px;
19+
padding: 0 12px;
20+
21+
font-family: 'Roboto', 'Arial', sans-serif;
22+
font-size: 1.4rem;
23+
font-weight: 500;
24+
text-decoration: none;
25+
26+
cursor: pointer;
27+
}
28+
29+
.play-all-btn:hover,
30+
.play-all-btn:focus {
31+
background-color:#9B33FF;
32+
}
33+
`;
34+
document.head.appendChild(style);
35+
}
36+
}

0 commit comments

Comments
 (0)