From 1e6cef6092995ec871ddb5a7ac05868d95d5d1af Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Tue, 11 Aug 2020 01:43:19 -0400 Subject: [PATCH] require confirmation for navigation away from image upload form (derpibooru/philomena#164) --- assets/js/upload.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/assets/js/upload.js b/assets/js/upload.js index 2be9b9bc..beab2c42 100644 --- a/assets/js/upload.js +++ b/assets/js/upload.js @@ -124,6 +124,26 @@ function setupImageUpload() { else { disableFetch(); } + + // Catch unintentional navigation away from the page + + function beforeUnload(event) { + // Chrome requires returnValue to be set + event.preventDefault(); + event.returnValue = ''; + } + + function registerBeforeUnload() { + window.addEventListener('beforeunload', beforeUnload); + } + + function unregisterBeforeUnload() { + window.removeEventListener('beforeunload', beforeUnload); + } + + fileField.addEventListener('change', registerBeforeUnload); + fetchButton.addEventListener('click', registerBeforeUnload); + form.addEventListener('submit', unregisterBeforeUnload); } export { setupImageUpload };