-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (77 loc) · 2.26 KB
/
index.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
// Initialize Firebase
var config = {
apiKey: "AIzaSyA5BHzoOn89dFPDbKWnTLpZge9OtpztY1s",
authDomain: "tests-b30cd.firebaseapp.com",
databaseURL: "https://tests-b30cd.firebaseio.com",
storageBucket: "tests-b30cd.appspot.com",
messagingSenderId: "619453600174"
};
firebase.initializeApp(config);
var currentUser = '';
/*var testListRef = firebase.database().ref('tests');
testListRef.on('value', function(snapshot) {
var v = snapshot.val();
console.log(arguments, testListRef);
var listExercises = v.split(',');
var currentSrc = document.currentScript.getAttribute('src');
var m = currentSrc.match(/\?n=([0-9]+)/);
var number = 0;
if (m && m[1]) {
number = Number(m[1]);
}
});*/
var canSubmit = true;
var sending = false;
window.submitTest = function() {
if (!currentUser) {
return alert('Inserisci la tua email!');
}
if (!canSubmit) {
return alert('Hai abbena inviato un risultato, aspetta qualche secondo prima di inviarne un altro!');
}
canSubmit = false;
setTimeout(function() {
canSubmit = true;
}, 10 * 1000);
sending = true;
firebase.database().ref('submissions').push({
user: currentUser,
result: document.documentElement.innerHTML
});
}
firebase.database().ref('submissions').on('child_added', function(data) {
if (sending && data.val().user === currentUser) {
alert('Il tuo test è stato inviato con successo.');
sending = false;
}
});
/* DOM */
var div = document.createElement('div');
div.style.position = 'fixed';
div.style.left = '0';
div.style.width = '100%';
div.style.bottom = 0;
div.style.height = '50px';
div.style.border = '1px solid black';
document.body.style.paddingBottom = '60px';
//
var input = document.createElement('input');
input.setAttribute('placeholder', 'Inserisci la tua email');
input.style.width = '80%';
input.style.height = '100%';
input.style.boxSizing = 'border-box';
input.style.fontSize = '24px';
input.value = currentUser;
input.onchange = function() {
currentUser = input.value;
}
//
var button = document.createElement('button');
button.style.width = '20%';
button.style.height = '100%';
button.style.verticalAlign = 'top';
button.innerText = 'Submit';
button.onclick = window.submitTest;
div.appendChild(input);
div.appendChild(button);
document.body.appendChild(div);