Skip to content

Commit

Permalink
try to solve memory leak on live page
Browse files Browse the repository at this point in the history
  • Loading branch information
hanydd committed Jan 27, 2025
1 parent 0e6455a commit 0328d5d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ import { openWarningDialog } from "./utils/warnings";

cleanPage();

detectPageType();

const utils = new Utils();

waitFor(() => Config.isReady(), 5000, 10).then(() => {
Expand All @@ -81,8 +83,6 @@ waitFor(() => Config.isReady(), 5000, 10).then(() => {
) DynamicListener();
});

detectPageType();

if ((document.hidden && getPageType() == PageType.Video) || getPageType() == PageType.List) {
document.addEventListener("visibilitychange", () => videoElementChange(true, getVideo()), { once: true });
window.addEventListener("mouseover", () => videoElementChange(true, getVideo()), { once: true });
Expand Down
4 changes: 4 additions & 0 deletions src/thumbnail-utils/thumbnailManagement.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Config from "../config";
import { getPageLoaded } from "../content";
import { PageType } from "../types";
import { waitFor } from "../utils/";
import { addCleanupListener } from "../utils/cleanup";
import { getPageType } from "../utils/video";
Expand Down Expand Up @@ -55,6 +56,9 @@ function onInitialLoad() {

let thumbnailCheckTimeout: NodeJS.Timer | null = null;
export function checkPageForNewThumbnails() {
// disable on live pages to prevent memory leaks
if (getPageType() === PageType.Live) return;

if (performance.now() - lastThumbnailCheck < 100 || thumbnailCheckTimeout) {
if (!thumbnailCheckTimeout) {
thumbnailCheckTimeout = setTimeout(() => {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export async function whitelistCheck() {
contentMethod.channelIDChange(channelIDInfo);
}

export function detectPageType(): void {
export function detectPageType(): PageType {
pageType = PageType.Unknown;

const urlObject = new URL(document.URL);
Expand Down Expand Up @@ -243,6 +243,7 @@ export function detectPageType(): void {
} else if (urlObject.hostname === "live.bilibili.com") {
pageType = PageType.Live;
}
return pageType;
}

let lastMutationListenerCheck = 0;
Expand Down

0 comments on commit 0328d5d

Please sign in to comment.