-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
110 lines (101 loc) · 3.33 KB
/
app.js
File metadata and controls
110 lines (101 loc) · 3.33 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
108
109
110
import jsQR from "jsqr";
var video = document.createElement("video");
var canvasElement = document.getElementById("canvas");
var successElement = document.getElementById("success");
var successContentElement = document.getElementById("successcontent");
var canvas = canvasElement.getContext("2d");
var loadingMessage = document.getElementById("loadingMessage");
var outputContainer = document.getElementById("output");
function processCodeData(data) {
console.log("DATA==" + data + "==");
fetch("/api/check", {
method: "POST",
headers: {
"Content-Type": "text/plain",
},
body: data,
})
.then((response) => response.text())
.then((text) => {
if (text != "error") {
const result = JSON.parse(text);
var timestamp = new Date(result.timestamp);
const metadata = [];
if (result.system_hash && result.software_version) {
const system_hash_pretty =
result.system_hash.substring(0, result.system_hash.length / 2) +
"<br>" +
result.system_hash.substring(result.system_hash.length / 2);
metadata.push(
["System hash", "<tt>" + system_hash_pretty + "</tt>"],
["Version", result.software_version]
);
}
metadata.push(
["Machine ID", result.machine_id],
["Election ID", "<tt>" + (result.election_id || "None") + "</tt>"],
[
"Timestamp",
timestamp.toLocaleDateString() +
" " +
timestamp.toLocaleTimeString(),
]
);
const innerHtml = metadata
.map((item) => item[0] + ":<br><b>" + item[1] + "</b>")
.join("<br><br>");
successContentElement.innerHTML = innerHtml;
canvasElement.hidden = true;
successElement.hidden = false;
continueAnimation = false;
outputContainer.hidden = true;
} else {
console.log("verification failed");
}
});
}
var continueAnimation = true;
function tick() {
loadingMessage.innerText = "⌛ Loading video...";
if (video.readyState === video.HAVE_ENOUGH_DATA && continueAnimation) {
loadingMessage.hidden = true;
canvasElement.hidden = false;
outputContainer.hidden = false;
canvasElement.height = video.videoHeight;
canvasElement.width = video.videoWidth;
canvas.drawImage(video, 0, 0, canvasElement.width, canvasElement.height);
var imageData = canvas.getImageData(
0,
0,
canvasElement.width,
canvasElement.height
);
var code = jsQR(imageData.data, imageData.width, imageData.height, {
inversionAttempts: "dontInvert",
});
if (code && code.data) {
console.log("got QR code -- " + code.data);
}
if (
code &&
code.data &&
(code.data.startsWith("1//lc") || code.data.startsWith("1//shv1"))
) {
processCodeData(code.data);
continueAnimation = false;
}
}
if (continueAnimation) {
requestAnimationFrame(tick);
}
}
document.getElementById("startvideo").onclick = () => {
navigator.mediaDevices
.getUserMedia({ video: { facingMode: "environment" } })
.then(function (stream) {
video.srcObject = stream;
video.setAttribute("playsinline", true); // required to tell iOS safari we don't want fullscreen
video.play();
requestAnimationFrame(tick);
});
};