-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
143 lines (121 loc) · 5.01 KB
/
index.html
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
137
138
139
140
141
142
143
<!DOCTYPE html>
<html>
<head>
<title>Barcode Detector</title>
<style type="text/css">
html {
width: 100%;
height: 100%;
}
body {
height: 100%;
margin: 0;
}
video {
width: 100%;
height: 100%;
margin: 0;
}
#camera-select {
float: right;
font-size: 4vh;
}
</style>
</head>
<body>
<select name="camera" id="camera-select"></select>
<video id="viewport" autoplay></video>
<script>
const barcodeDetector = new BarcodeDetector({formats: ['upc_a', 'ean_13', 'code_128']});
const camselect = document.getElementById('camera-select');
const video = document.getElementById('viewport');
const originUrl = new URL(window.location.href);
const targetUrl = new URL('https://www.ean-search.org/');
function selectCamera(id) {
const constraints = {
video: {
width: 1920
}
};
if (id) {
constraints.video.deviceId = id;
} else {
constraints.video.facingMode = 'environment';
}
if (video.srcObject) {
video.srcObject.getTracks().forEach((track) => {
track.stop();
});
video.srcObject = null;
}
navigator.mediaDevices.getUserMedia(constraints).then((mediaStream) => {
const cameraId = mediaStream.getVideoTracks()[0].getSettings().deviceId;
video.srcObject = mediaStream;
try {
if (localStorage.getItem('selected_camera') !== cameraId) {
localStorage.setItem('selected_camera', cameraId);
}
} catch {
localStorage.clear();
}
while (camselect.options.length > 0) {
camselect.remove(0);
}
navigator.mediaDevices.enumerateDevices().then((devices) => {
devices.forEach((device) => {
if (device.kind === 'videoinput') {
const cam = document.createElement('option');
cam.value = device.deviceId;
cam.text = device.label;
if (device.deviceId === cameraId) {
cam.selected = true;
}
camselect.add(cam);
}
})
}).catch((e) => {
console.error(e);
});
}).catch((e) => {
console.error(e);
localStorage.removeItem('selected_camera');
});
}
function validate(str, def=null, matcher=/^[A-Za-z0-9\.]+$/) {
if (str && matcher.test(str)) {
return str;
}
return def;
}
let targetQuery = validate(originUrl.searchParams.get('q'), 'q');
if (originUrl.searchParams.has('s') && originUrl.searchParams.get('s') === '0') {
targetUrl.protocol = 'http:';
}
targetUrl.hostname = validate(originUrl.searchParams.get('h'), targetUrl.hostname);
targetUrl.port = validate(originUrl.searchParams.get('p'), targetUrl.port, /^[0-9]+$/);
targetUrl.pathname = validate(originUrl.searchParams.get('n'), targetUrl.pathname, /^[A-Za-z0-9\.\/]+$/);
camselect.addEventListener('change', (event) => {
const options = event.target.selectedOptions;
if (options.length > 0) {
selectCamera(options[0].value);
}
});
selectCamera(localStorage.getItem('selected_camera'));
let lastDetection = performance.now() - 200;
(function detectionLoop(timestamp) {
if (video.srcObject && timestamp - lastDetection >= 200) {
lastDetection = timestamp;
barcodeDetector.detect(video).then((barcodes) => {
if (barcodes.length >= 1) {
targetUrl.searchParams.append(targetQuery, barcodes[0].rawValue);
window.location.href = targetUrl.href;
}
}).catch((e) => {
console.error(e);
});
}
requestAnimationFrame(detectionLoop);
})(performance.now());
</script>
</body>
</html>