-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
107 lines (96 loc) · 3.77 KB
/
scripts.js
File metadata and controls
107 lines (96 loc) · 3.77 KB
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
let contact = getContact();
document.getElementById('delete-contact').addEventListener('click', deleteContact);
if (!contact) {
// Show contact form
document.getElementById('contact-form').style.display = 'block';
document.getElementById('save-contact').addEventListener('click', saveContact);
} else {
// Show contact form as disabled and scanner
document.getElementById('contact-form').style.display = 'block';
document.getElementById('contact-name').value = contact.name;
document.getElementById('contact-email').value = contact.email;
document.getElementById('contact-name').disabled = true;
document.getElementById('contact-email').disabled = true;
startScanner();
}
function getContact() {
let contactStr = document.cookie.match('(^|[^;]+)\\s*contact\\s*=\\s*([^;]+)');
if (contactStr) {
return JSON.parse(contactStr.pop());
}
return null;
}
function saveContact() {
let name = document.getElementById('contact-name').value.trim();
let email = document.getElementById('contact-email').value.trim();
if (!name || !email) {
alert('Please enter a valid name and email.');
return;
}
let contact = { name: name, email: email };
document.cookie = 'contact=' + JSON.stringify(contact) + ';path=/';
document.getElementById('contact-name').disabled = true;
document.getElementById('contact-email').disabled = true;
startScanner();
document.getElementById('save-contact').removeEventListener('click', saveContact);
}
function deleteContact() {
document.cookie = "contact=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
document.getElementById('contact-name').value = "";
document.getElementById('contact-email').value = "";
document.getElementById('contact-name').disabled = false;
document.getElementById('contact-email').disabled = false;
location.reload();
}
function startScanner() {
// Hide contact form and show scanner
// document.getElementById('contact-form').style.display = 'none';
document.getElementById('scanner').style.display = 'block';
let scanner = new Instascan.Scanner({ video: document.getElementById('preview') });
scanner.addListener('scan', function(content) {
sendData(content);
});
Instascan.Camera.getCameras().then(function(cameras) {
if (cameras.length > 1) {
scanner.start(cameras[1]);
} else if (cameras.length > 0) {
scanner.start(cameras[0]);
}
else {
console.error('No cameras found.');
}
}).catch(function(e) {
console.error(e);
});
function sendData(studentURL) {
let contact = getContact();
let data = { studentURL: studentURL, companyContact: contact };
fetch('https://prod-117.westeurope.logic.azure.com:443/workflows/6063fa4dc6e74fd79450369e4907d8cb/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=NRqi05Ha0BSJ-04XzI_YzjymY9bPjtAYP8qQ5FBqLZ8', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
console.log('QR code data sent successfully');
scanner.stop();
document.getElementById('student-cv').href = studentURL;
document.getElementById('message').style.display = 'block';
document.getElementById('restart-scanner').style.display = 'block';
document.getElementById('preview').style.display = 'none';
document.getElementById('restart-scanner').addEventListener('click', restartScanner);
})
.catch(error => {
console.error('Error sending QR code data:', error);
});
}
function restartScanner() {
// Hide message and restart scanner
document.getElementById('student-cv').href = "";
document.getElementById('preview').style.display = 'block';
document.getElementById('message').style.display = 'none';
document.getElementById('restart-scanner').style.display = 'none';
startScanner();
}}