refactor: read watchlist IDs from JSON script tag instead of global var
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
export {};
|
export {};
|
||||||
|
|
||||||
|
import { onReady } from "./utils";
|
||||||
|
|
||||||
type WatchlistStatus = "watching" | "completed" | "plan_to_watch" | "dropped";
|
type WatchlistStatus = "watching" | "completed" | "plan_to_watch" | "dropped";
|
||||||
|
|
||||||
type WatchlistUpdateDisplay =
|
type WatchlistUpdateDisplay =
|
||||||
@@ -428,11 +430,7 @@ const removeWatchlist = async (id: number, title: string, source: HTMLElement):
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (document.readyState === "loading") {
|
onReady(initWatchlistPage);
|
||||||
document.addEventListener("DOMContentLoaded", initWatchlistPage);
|
|
||||||
} else {
|
|
||||||
initWatchlistPage();
|
|
||||||
}
|
|
||||||
|
|
||||||
const initWatchlist = (ids: number[]): void => {
|
const initWatchlist = (ids: number[]): void => {
|
||||||
ids.forEach((id) => watchlistIds.add(id));
|
ids.forEach((id) => watchlistIds.add(id));
|
||||||
@@ -506,16 +504,23 @@ const installDelegatedHandlers = (): void => {
|
|||||||
declare global {
|
declare global {
|
||||||
interface Window {
|
interface Window {
|
||||||
initWatchlist: (ids: number[]) => void;
|
initWatchlist: (ids: number[]) => void;
|
||||||
__WATCHLIST_IDS__?: unknown;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
window.initWatchlist = initWatchlist;
|
window.initWatchlist = initWatchlist;
|
||||||
|
|
||||||
onReady(() => {
|
onReady(() => {
|
||||||
const raw = window.__WATCHLIST_IDS__;
|
const raw = document.getElementById("watchlist-ids-json")?.textContent;
|
||||||
if (Array.isArray(raw)) {
|
if (raw) {
|
||||||
const ids: number[] = raw.filter((entry): entry is number => typeof entry === "number");
|
let parsed: unknown = null;
|
||||||
|
try {
|
||||||
|
parsed = JSON.parse(raw);
|
||||||
|
} catch {
|
||||||
|
parsed = null;
|
||||||
|
}
|
||||||
|
const ids = Array.isArray(parsed)
|
||||||
|
? parsed.filter((entry): entry is number => typeof entry === "number")
|
||||||
|
: [];
|
||||||
if (ids.length > 0) {
|
if (ids.length > 0) {
|
||||||
initWatchlist(ids);
|
initWatchlist(ids);
|
||||||
}
|
}
|
||||||
@@ -528,4 +533,3 @@ onReady(() => {
|
|||||||
|
|
||||||
installDelegatedHandlers();
|
installDelegatedHandlers();
|
||||||
});
|
});
|
||||||
import { onReady } from "./utils";
|
|
||||||
|
|||||||
Reference in New Issue
Block a user