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.

59 lines
2.0 KiB

const oldOnloadHandler = window.onload ?? (() => {});
window.onload = () => {
oldOnloadHandler();
// Sync to position button
const position = localStorage.getItem(
new URL(document.URL).searchParams.get("id") + "_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 = JSON.parse(localStorage.getItem("savedStories") ?? "[]");
function handler() {
if (button?.textContent === "Save Story") {
savedStories.push({
id: new URL(document.URL).searchParams.get("id"),
name: document.getElementById("storyname")?.textContent,
coverURL: document.getElementById("storycover").src
});
button.textContent = "Unsave Story";
} else if (button?.textContent === "Unsave Story") {
savedStories = savedStories.filter(
(story) =>
story.id !== new URL(document.URL).searchParams.get("id")
);
button.textContent = "Save Story";
}
localStorage.setItem("savedStories", JSON.stringify(savedStories));
}
if (
savedStories.filter(
(story) => story.id === new URL(document.URL).searchParams.get("id")
).length > 0
) {
button.textContent = "Unsave Story";
}
button.onclick = handler;
};