From fa656577d1968e473c06626a644fafa554e48c66 Mon Sep 17 00:00:00 2001 From: ed Date: Sun, 21 Feb 2021 21:08:53 +0100 Subject: [PATCH] prevent non-spa navigation while uploading --- copyparty/web/browser.js | 8 +++++--- copyparty/web/up2k.js | 35 ++++++++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/copyparty/web/browser.js b/copyparty/web/browser.js index c49e1cea..737978a9 100644 --- a/copyparty/web/browser.js +++ b/copyparty/web/browser.js @@ -980,9 +980,11 @@ function apply_perms(perms) { } var act = document.querySelector('#ops>a.act'); - var areq = act.getAttribute('data-perm'); - if (areq && !has(perms, areq)) - goto(); + if (act) { + var areq = act.getAttribute('data-perm'); + if (areq && !has(perms, areq)) + goto(); + } var have_write = has(perms, "write"); var tds = document.querySelectorAll('#u2conf td'); diff --git a/copyparty/web/up2k.js b/copyparty/web/up2k.js index 13420c4d..2f3f4c96 100644 --- a/copyparty/web/up2k.js +++ b/copyparty/web/up2k.js @@ -424,6 +424,7 @@ function up2k_init(have_crypto) { var tasker = (function () { var mutex = false; + var was_busy = false; function taskerd() { if (mutex) @@ -442,16 +443,25 @@ function up2k_init(have_crypto) { st.busy.upload.length; } - if (flag) { - var need_flag = 0 != - st.todo.hash.length + - st.todo.handshake.length + - st.todo.upload.length + - st.busy.hash.length + - st.busy.handshake.length + - st.busy.upload.length; + var is_busy = 0 != + st.todo.hash.length + + st.todo.handshake.length + + st.todo.upload.length + + st.busy.hash.length + + st.busy.handshake.length + + st.busy.upload.length; - if (need_flag) { + if (was_busy != is_busy) { + was_busy = is_busy; + + if (is_busy) + window.addEventListener("beforeunload", warn_uploader_busy); + else + window.removeEventListener("beforeunload", warn_uploader_busy); + } + + if (flag) { + if (is_busy) { var now = new Date().getTime(); flag.take(now); if (!flag.ours) { @@ -1108,5 +1118,12 @@ function up2k_init(have_crypto) { } +function warn_uploader_busy(e) { + e.preventDefault(); + e.returnValue = ''; + return "upload in progress, click abort and use the file-tree to navigate instead"; +} + + if (document.querySelector('#op_up2k.act')) goto_up2k();