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.
68 lines
2.3 KiB
68 lines
2.3 KiB
interface SavedSearch {
|
|
query: string;
|
|
type: "text" | "title" | "tag";
|
|
sort: "hot" | "new";
|
|
}
|
|
|
|
export default () => {
|
|
// deno-lint-ignore ban-types
|
|
const oldOnloadHandler: Function = window.onload ?? (() => {});
|
|
|
|
window.onload = () => {
|
|
oldOnloadHandler();
|
|
const button = document.getElementById("favorite");
|
|
|
|
if (!button) throw "how did your button not init what";
|
|
|
|
let savedSearches: SavedSearch[] = JSON.parse(
|
|
localStorage.getItem("savedSearches") ?? "[]",
|
|
);
|
|
|
|
function handler() {
|
|
if (button?.textContent === "Favorite") {
|
|
savedSearches.push({
|
|
query: (new URL(document.URL)).searchParams.get(
|
|
"query",
|
|
) as string,
|
|
type: (new URL(document.URL)).searchParams.get(
|
|
"type",
|
|
) as "text" | "title" | "tag",
|
|
sort: (new URL(document.URL)).searchParams.get(
|
|
"sort",
|
|
) as "hot" | "new",
|
|
});
|
|
button.textContent = "Unfavorite";
|
|
} else if (button?.textContent === "Unfavorite") {
|
|
savedSearches = savedSearches.filter((search) =>
|
|
search.query !==
|
|
(new URL(document.URL)).searchParams.get("query") &&
|
|
search.type !==
|
|
(new URL(document.URL)).searchParams.get("type") &&
|
|
search.type !==
|
|
(new URL(document.URL)).searchParams.get("sort")
|
|
);
|
|
button.textContent = "Favorite";
|
|
}
|
|
localStorage.setItem(
|
|
"savedSearches",
|
|
JSON.stringify(savedSearches),
|
|
);
|
|
}
|
|
|
|
if (
|
|
savedSearches.filter((search) =>
|
|
search.query ===
|
|
(new URL(document.URL)).searchParams.get("query") &&
|
|
search.type ===
|
|
(new URL(document.URL)).searchParams.get("type") &&
|
|
search.sort ===
|
|
(new URL(document.URL)).searchParams.get("sort")
|
|
).length > 0
|
|
) {
|
|
button.textContent = "Unfavorite";
|
|
}
|
|
|
|
button.onclick = handler;
|
|
};
|
|
};
|