|
| 1 | +const target = arguments[0]; |
| 2 | +const offsetX = arguments[1]; |
| 3 | +const offsetY = arguments[2]; |
| 4 | + |
| 5 | +const document = target.ownerDocument || document; |
| 6 | +const window = document.defaultView || window; |
| 7 | + |
| 8 | +const input = document.createElement('INPUT'); |
| 9 | +input.type = 'file'; |
| 10 | +input.onchange = function () { |
| 11 | + const rect = target.getBoundingClientRect(); |
| 12 | + const x = rect.left + (offsetX || (rect.width >> 1)); |
| 13 | + const y = rect.top + (offsetY || (rect.height >> 1)); |
| 14 | + |
| 15 | + const dataTransfer = new DataTransfer(); |
| 16 | + dataTransfer.items.add(input.files[0]); |
| 17 | + |
| 18 | + console.log('Got uploads: ' + dataTransfer.items); |
| 19 | + |
| 20 | + for (let t = 0; t < dataTransfer.items.length; t++) { |
| 21 | + const n = dataTransfer.items[t]; |
| 22 | + console.log("Upload " + t + ": " + n.kind); |
| 23 | + if (n.kind === "file") { |
| 24 | + const e = n.getAsFile(); |
| 25 | + if (e) { |
| 26 | + console.log("File upload " + t + ": " + e.name); |
| 27 | + } else { |
| 28 | + console.warn("Cannot get file"); |
| 29 | + } |
| 30 | + } else { |
| 31 | + console.warn("Not a file upload"); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + ['dragenter', 'drop'].forEach(function (name) { |
| 36 | + console.log('Dispatching mouse event with file transfer') |
| 37 | + const evt = document.createEvent('MouseEvent'); |
| 38 | + evt.initMouseEvent(name, !0, !0, window, 0, 0, 0, x, y, !1, !1, !1, !1, 0, null); |
| 39 | + evt.dataTransfer = dataTransfer; |
| 40 | + target.dispatchEvent(evt); |
| 41 | + }); |
| 42 | + |
| 43 | + // setTimeout(function () { |
| 44 | + // document.body.removeChild(input); |
| 45 | + // }, 25); |
| 46 | +}; |
| 47 | +target.appendChild(input); |
| 48 | + |
| 49 | +console.log('Created file upload input') |
| 50 | + |
| 51 | +// noinspection JSAnnotator |
| 52 | +return input; |
0 commit comments