From f9d5bb3b299359d9f34d2b8ce710f64e8b554837 Mon Sep 17 00:00:00 2001 From: ed Date: Fri, 28 Jul 2023 21:43:40 +0200 Subject: [PATCH] support upload by dragdrop from other browser windows, hello from LO484 https://ocv.me/stuff/aircode.jpg --- copyparty/web/browser.js | 2 ++ copyparty/web/up2k.js | 28 ++++++++++++++++++++++++---- copyparty/web/util.js | 16 ++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/copyparty/web/browser.js b/copyparty/web/browser.js index 78d1badf..466cab91 100644 --- a/copyparty/web/browser.js +++ b/copyparty/web/browser.js @@ -403,6 +403,7 @@ var Ls = { "u_https3": "for better performance", "u_ancient": 'your browser is impressively ancient -- maybe you should use bup instead', "u_nowork": "need firefox 53+ or chrome 57+ or iOS 11+", + "u_uri": "to dragdrop images from other browser windows,\nplease drop it onto the big upload button", "u_enpot": 'switch to potato UI (may improve upload speed)', "u_depot": 'switch to fancy UI (may reduce upload speed)', "u_gotpot": 'switching to the potato UI for improved upload speed,\n\nfeel free to disagree and switch back!', @@ -865,6 +866,7 @@ var Ls = { "u_https3": "for høyere hastighet", "u_ancient": 'nettleseren din er prehistorisk -- mulig du burde bruke bup istedenfor', "u_nowork": "krever firefox 53+, chrome 57+, eller iOS 11+", + "u_uri": "for å laste opp bilder ifra andre nettleservinduer,\nslipp bildet rett på den store last-opp-knappen", "u_enpot": 'bytt til enkelt UI (gir sannsynlig raskere opplastning)', "u_depot": 'bytt til snæsent UI (gir sannsynlig tregere opplastning)', "u_gotpot": 'byttet til et enklere UI for å laste opp raskere,\n\ndu kan gjerne bytte tilbake altså!', diff --git a/copyparty/web/up2k.js b/copyparty/web/up2k.js index 460b8e37..f071459b 100644 --- a/copyparty/web/up2k.js +++ b/copyparty/web/up2k.js @@ -998,13 +998,26 @@ function up2k_init(subtle) { return onover.bind(this)(e); } function onover(e) { + return onovercmn(this, e, false); + } + function onoverbtn(e) { + return onovercmn(this, e, true); + } + function onovercmn(self, e, btn) { try { var ok = false, dt = e.dataTransfer.types; for (var a = 0; a < dt.length; a++) if (dt[a] == 'Files') ok = true; - else if (dt[a] == 'text/uri-list') - return true; + else if (dt[a] == 'text/uri-list') { + if (btn) { + ok = true; + if (toast.txt == L.u_uri) + toast.hide(); + } + else + return toast.inf(10, L.u_uri) || true; + } if (!ok) return true; @@ -1020,8 +1033,11 @@ function up2k_init(subtle) { document.body.ondragenter = document.body.ondragleave = document.body.ondragover = null; return modal.alert('your browser does not support drag-and-drop uploading'); } + if (btn) + return; + clmod(ebi('drops'), 'vis', 1); - var v = this.getAttribute('v'); + var v = self.getAttribute('v'); if (v) clmod(ebi(v), 'hl', 1); } @@ -1045,6 +1061,8 @@ function up2k_init(subtle) { document.body.ondragleave = offdrag; document.body.ondragover = onover; document.body.ondrop = gotfile; + ebi('u2btn').ondrop = gotfile; + ebi('u2btn').ondragover = onoverbtn; var drops = [ebi('up_dz'), ebi('srch_dz')]; for (var a = 0; a < 2; a++) { @@ -1132,7 +1150,7 @@ function up2k_init(subtle) { dst = good_files; if (is_itemlist) { - if (fobj.kind !== 'file') + if (fobj.kind !== 'file' && fobj.type !== 'text/uri-list') continue; try { @@ -1144,6 +1162,8 @@ function up2k_init(subtle) { } catch (ex) { } fobj = fobj.getAsFile(); + if (!fobj) + continue; } try { if (fobj.size < 1) diff --git a/copyparty/web/util.js b/copyparty/web/util.js index 77cab381..6a47a6fa 100644 --- a/copyparty/web/util.js +++ b/copyparty/web/util.js @@ -1274,6 +1274,9 @@ var toast = (function () { r.visible = false; r.txt = null; r.tag = obj; // filler value (null is scary) + r.p_txt = ''; + r.p_sec = 0; + r.p_t = 0; function scrollchk() { if (scrolling) @@ -1306,10 +1309,23 @@ var toast = (function () { }; r.show = function (cl, sec, txt, tag) { + var same = r.visible && txt == r.p_txt && r.p_sec == sec, + delta = Date.now() - r.p_t; + + if (same && delta < 100) + return; + + r.p_txt = txt; + r.p_sec = sec; + r.p_t = Date.now(); + clearTimeout(te); if (sec) te = setTimeout(r.hide, sec * 1000); + if (same && delta < 1000) + return; + if (txt.indexOf('') + 1) txt = txt.slice(0, txt.indexOf('<')) + ' [...]';