diff --git a/scripts/AnimeNotifier.ts b/scripts/AnimeNotifier.ts index 9b26a445..f2e0be37 100644 --- a/scripts/AnimeNotifier.ts +++ b/scripts/AnimeNotifier.ts @@ -200,6 +200,9 @@ export class AnimeNotifier { this.notificationManager.update() } + // Bind unload event + window.addEventListener("beforeunload", this.onBeforeUnload.bind(this)) + // Download popular anime titles for the search // let response = await fetch("/api/popular/anime/titles/500") // let titles = await response.json() @@ -218,6 +221,20 @@ export class AnimeNotifier { // search.setAttribute("list", titleList.id) } + async onBeforeUnload(e: BeforeUnloadEvent) { + let message = undefined + + // Prevent closing tab on new thread page + if(this.app.currentPath === "/new/thread" && document.activeElement.tagName === "TEXTAREA" && (document.activeElement as HTMLTextAreaElement).value.length > 20) { + message = "You have unsaved changes on the current page. Are you sure you want to leave?" + } + + if(message) { + e.returnValue = message + return message + } + } + dragAndDrop() { for(let element of findAll("inventory-slot")) { // Skip elements that have their event listeners attached already