diff options
| -rw-r--r-- | static/script.js | 58 |
1 files changed, 42 insertions, 16 deletions
diff --git a/static/script.js b/static/script.js index 1f88d95..a579a3e 100644 --- a/static/script.js +++ b/static/script.js | |||
| @@ -25,28 +25,54 @@ | |||
| 25 | dropZone.classList.remove(hoverClassName); | 25 | dropZone.classList.remove(hoverClassName); |
| 26 | }); | 26 | }); |
| 27 | 27 | ||
| 28 | // This is the most important event, the event that gives access to files | 28 | // Handle copy/paste |
| 29 | dropZone.addEventListener("paste", function (e) { | ||
| 30 | e.preventDefault(); | ||
| 31 | |||
| 32 | if (e.clipboardData.items.length != 1) { | ||
| 33 | return | ||
| 34 | } | ||
| 35 | |||
| 36 | const item = e.clipboardData.items[0]; | ||
| 37 | if (item.type.indexOf("image") == -1) { | ||
| 38 | return; | ||
| 39 | } | ||
| 40 | |||
| 41 | fetch('/', { | ||
| 42 | method: 'POST', | ||
| 43 | body: item.getAsFile(), | ||
| 44 | }) | ||
| 45 | .then(response => response.text()) | ||
| 46 | .then(body => { // Yes, this is ugly | ||
| 47 | document.open() | ||
| 48 | document.write(body) | ||
| 49 | document.close() | ||
| 50 | }) | ||
| 51 | |||
| 52 | }); | ||
| 53 | |||
| 29 | dropZone.addEventListener("drop", function (e) { | 54 | dropZone.addEventListener("drop", function (e) { |
| 30 | e.preventDefault(); | 55 | e.preventDefault(); |
| 31 | dropZone.classList.remove(hoverClassName); | 56 | dropZone.classList.remove(hoverClassName); |
| 32 | 57 | ||
| 33 | const files = Array.from(e.dataTransfer.files); | 58 | const files = Array.from(e.dataTransfer.files); |
| 34 | if (files.length > 0) { | 59 | if (files.length != 1 ) { |
| 35 | const data = new FormData(); | 60 | return; |
| 36 | for (const file of files) { | 61 | } |
| 37 | data.append('file', file); | 62 | const data = new FormData(); |
| 38 | } | 63 | for (const file of files) { |
| 64 | data.append('file', file); | ||
| 65 | } | ||
| 39 | 66 | ||
| 40 | fetch('/', { | 67 | fetch('/', { |
| 41 | method: 'POST', | 68 | method: 'POST', |
| 42 | body: data, | 69 | body: data, |
| 70 | }) | ||
| 71 | .then(response => response.text()) | ||
| 72 | .then(body => { // Yes, this is ugly | ||
| 73 | document.open() | ||
| 74 | document.write(body) | ||
| 75 | document.close() | ||
| 43 | }) | 76 | }) |
| 44 | .then(response => response.text()) | ||
| 45 | .then(body => { // Yes, this is ugly | ||
| 46 | document.open() | ||
| 47 | document.write(body) | ||
| 48 | document.close() | ||
| 49 | }) | ||
| 50 | } | ||
| 51 | }); | 77 | }); |
| 52 | })(); | 78 | })(); |
