You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.6 KiB

interface SavedStory {
id: string;
// in *theory* we could fetch these from the cache, but if it's not there you'd have to wait years to open the homepage lol
name: string;
coverURL: string;
}
export default () => {
// deno-lint-ignore ban-types
const oldOnloadHandler: Function = window.onload ?? (() => {});
window.onload = () => {
oldOnloadHandler();
// Sync to position button
const position = localStorage.getItem(
(new URL(document.URL)).searchParams.get(
"id",
) as string + "_progress",
);
if (position) {
const buttonsList = document.getElementById("detail-buttons");
const buttonEle = document.createElement("button");
buttonEle.className = "favorite-story-button";
buttonEle.textContent = "Sync to story position";
buttonEle.onclick = () => {
const currURL = new URL(document.location.href);
currURL.searchParams.set("chapter", position);
window.location.href = currURL.toString();
};
buttonsList?.appendChild(buttonEle);
}
// Favorite button
const button = document.getElementById("favorite");
if (!button) throw "how did your button not init what";
let savedStories: SavedStory[] = JSON.parse(
localStorage.getItem("savedStories") ?? "[]",
);
function handler() {
if (button?.textContent === "Favorite") {
savedStories.push({
id: (new URL(document.URL)).searchParams.get(
"id",
) as string,
name: document.getElementById("storyname")
?.textContent as string,
coverURL: (document.getElementById(
"storycover",
) as HTMLImageElement)
.src,
});
button.textContent = "Unfavorite";
} else if (button?.textContent === "Unfavorite") {
savedStories = savedStories.filter((story) =>
story.id !== (new URL(document.URL)).searchParams.get("id")
);
button.textContent = "Favorite";
}
localStorage.setItem("savedStories", JSON.stringify(savedStories));
}
if (
savedStories.filter((story) =>
story.id === (new URL(document.URL)).searchParams.get("id")
).length > 0
) {
button.textContent = "Unfavorite";
}
button.onclick = handler;
};
};