-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpraktomatupload.user.js
136 lines (121 loc) · 3.95 KB
/
praktomatupload.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// ==UserScript==
// @name Praktomat File Upload
// @version 1.0
// @description Make file uploading to Praktomat easier
// @author Temm
// @match http*://praktomat.inf.h-brs.de/*/tasks/*/solutionupload/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=h-brs.de
// @grant none
// @copyright 2022, Temm (https://openuserjs.org/users/Temm)
// @license GPL-3.0-or-later
// @updateURL https://openuserjs.org/meta/Temm/Praktomat_File_Upload.meta.js
// @downloadURL https://openuserjs.org/install/Temm/Praktomat_File_Upload.user.js
// ==/UserScript==
console.log("Praktomat File Upload Loaded");
// Blatantly stolen from https://medium.com/creative-technology-concepts-code/creating-a-drag-drop-upload-area-bdbe891446a8
// and https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/File_drag_and_drop
document.querySelector(".left > form").insertAdjacentHTML("afterend", `
<div class="area">
<div id="areatext">
Drag and drop files here
</div>
<div id="upload"></div>
</div>
<style>
.area {
position: relative;
width: 100%;
aspect-ratio: 2/1;
border: 4px dashed #000;
background-position: center;
background-repeat: no-repeat;
background-size: 64px 64px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
filter: alpha(opacity=50);
-khtml-opacity: 0.5;
-moz-opacity: 0.5;
opacity: 0.5;
text-align: center;
}
.area:hover,
.area.dragging,
.area.uploading {
filter: alpha(opacity=100);
-khtml-opacity: 1;
-moz-opacity: 1;
opacity: 1;
}
#upload {
margin: 0;
width: 100%;
height: 100%;
border: none;
cursor: pointer;
}
.area #upload:focus {
outline: none;
}
#areatext {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 1.5em;
font-weight: bold;
color: #000;
z-index: -1;
}
</style>
`)
var upload = document.getElementById("upload");
upload.addEventListener("dragenter", function (e) {
upload.parentNode.className = "area dragging";
}, false);
upload.addEventListener("dragleave", function (e) {
upload.parentNode.className = "area";
}, false);
upload.addEventListener("drop", (e) => {
console.log('File(s) dropped');
// Prevent default behavior (Prevent file from being opened)
e.preventDefault();
if (e.dataTransfer.items) {
// Use DataTransferItemList interface to access the file(s)
[...e.dataTransfer.items].forEach((item, i) => {
// If dropped items aren't files, reject them
if (item.kind === 'file') {
const file = item.getAsFile();
onFileAdded(file);
}
});
} else {
// Use DataTransfer interface to access the file(s)
[...e.dataTransfer.files].forEach((file, i) => {
onFileAdded(file);
});
}
}, false);
upload.addEventListener("dragover", (e) => {
console.log('File(s) in drop zone');
// Prevent default behavior (Prevent file from being opened)
e.preventDefault();
})
function onFileAdded(file) {
console.log("File added: " + file.name);
console.log(file);
// input type file that should not already have a file
let inputElem = Array.from(document.querySelectorAll(".form-row.file.dynamic-form > input[type=file]")).filter((elem) => !elem.files.length)[0];
if (!inputElem) {
// If no input element is found, click the plus button to add a new one
let plus = document.querySelector(".icon.icon-orange.ui-icon-circle-plus");
plus.click();
inputElem = Array.from(document.querySelectorAll(".form-row.file.dynamic-form > input[type=file]")).filter((elem) => !elem.files.length)[0];
if (!inputElem) {
console.log("Something went wrong, no input element found");
}
}
let transfer = new DataTransfer();
transfer.items.add(file);
inputElem.files = transfer.files;
}