Skip to content

Commit 9951574

Browse files
committed
chore: Update YouTubeAdapter to improve initial data processing and loading indicators
1 parent a2bdefd commit 9951574

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

src/platforms/webPage.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ export abstract class WebPage {
66
constructor() {
77
this.subject = new Subject();
88
}
9+
abstract processInitialData(): void;
10+
abstract hiddenLoading(html: Element): void;
11+
abstract removeLoading(html: Element): void;
912

1013
abstract hiddenSpoiler(
1114
html: HTMLElement,

src/platforms/youtubeAdapter.ts

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,27 @@ export class YouTubeAdapter extends WebPage {
88
}
99

1010
processInitialData() {
11-
const videoList: HTMLElement[] = Array.from(
12-
document.querySelectorAll("ytd-rich-item-renderer")
13-
);
14-
const videoList2: HTMLElement[] = Array.from(
15-
document.querySelectorAll("ytd-video-renderer")
16-
);
17-
this.subject.notify([...videoList, ...videoList2]);
11+
const observer = new MutationObserver((mutationsList, observer) => {
12+
const ytBrowse = document.body.querySelector("ytd-browse");
13+
const ytSearch = document.body.querySelector("ytd-search");
14+
const container = ytBrowse ?? ytSearch;
15+
if (!container) {
16+
return null;
17+
}
18+
const contents = container.querySelector("#contents");
19+
if (!contents) {
20+
return null;
21+
}
22+
observer.disconnect();
23+
const videoList: HTMLElement[] = Array.from(
24+
document.querySelectorAll("ytd-rich-item-renderer")
25+
);
26+
const videoList2: HTMLElement[] = Array.from(
27+
document.querySelectorAll("ytd-video-renderer")
28+
);
29+
this.subject.notify([...videoList, ...videoList2]);
30+
});
31+
observer.observe(document.body, { childList: true, subtree: true });
1832
}
1933

2034
private waitForBrowse(callback: () => Element | null): Promise<Element> {
@@ -140,6 +154,38 @@ export class YouTubeAdapter extends WebPage {
140154
);
141155
}
142156

157+
hiddenLoading(video: HTMLElement) {
158+
let fontSize = "16px";
159+
if (video.nodeName === "YTD-COMPACT-VIDEO-RENDERER") {
160+
fontSize = "14px";
161+
}
162+
video
163+
.querySelector("#dismissible")
164+
?.setAttribute("style", "filter: blur(10px)");
165+
166+
const spoilerContainer = document.createElement("div");
167+
spoilerContainer.setAttribute("id", "spoiler-container");
168+
spoilerContainer.style.position = "absolute";
169+
spoilerContainer.style.top = "0";
170+
spoilerContainer.style.left = "0";
171+
spoilerContainer.style.width = "100%";
172+
spoilerContainer.style.height = "100%";
173+
spoilerContainer.style.color = "white";
174+
spoilerContainer.style.display = "flex";
175+
spoilerContainer.style.flexDirection = "column";
176+
spoilerContainer.style.justifyContent = "center";
177+
spoilerContainer.style.alignItems = "center";
178+
spoilerContainer.style.zIndex = "10";
179+
180+
const text = document.createElement("p");
181+
text.style.marginBottom = "10px";
182+
text.textContent = "Analyzing...";
183+
text.style.fontSize = fontSize;
184+
spoilerContainer.appendChild(text);
185+
video.style.position = "relative";
186+
video.appendChild(spoilerContainer);
187+
}
188+
143189
hiddenSpoiler(video: HTMLElement, name: string, spoilerProbability: number) {
144190
// is element ytd-compact-video-renderer
145191
let fontSize = "16px";
@@ -194,6 +240,14 @@ export class YouTubeAdapter extends WebPage {
194240
video.querySelector("#dismissible")?.setAttribute("style", "filter: none");
195241
}
196242

243+
removeLoading(html: HTMLElement): void {
244+
const spoilerContainer = html.querySelector("#spoiler-container");
245+
if (spoilerContainer) {
246+
spoilerContainer.remove();
247+
}
248+
this.displayContent(html);
249+
}
250+
197251
getInfoItem(element: HTMLElement): InfoContent | null {
198252
const title = element.querySelector("#video-title")?.textContent;
199253
if (!title) {

src/scripts/contentScript.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ async function main() {
1414
}
1515

1616
if (platform) {
17+
platform.processInitialData();
1718
platform.subscribeToNewData(async (videos) => {
19+
videos.forEach((item) => platform.hiddenLoading(item));
1820
const content = videos
1921
.map((item) => platform.getInfoItem(item))
2022
.filter((item) => item !== null);
2123

2224
const result = await ai.isSpoiler(content);
25+
videos.forEach((item) => platform.removeLoading(item));
26+
2327
result.data.forEach(async (item) => {
2428
const { settings } = await chrome.storage.sync.get(["settings"]);
2529
const umbral = settings.umbral ?? 0.5;

0 commit comments

Comments
 (0)