-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsQRScanner.svelte
296 lines (265 loc) · 9.02 KB
/
JsQRScanner.svelte
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<script lang="ts">
import { onMount, onDestroy } from "svelte";
import LibLoader from "./LibLoader.svelte";
import { LIBRARIES } from "../../utils/stores/libraries";
import { detectOS } from "../../utils/system";
import { getNotificationsContext } from "svelte-notifications";
import { fade } from "svelte/transition";
const { addNotification } = getNotificationsContext();
let URL_JSQRSCANNER;
const unsubscribe = LIBRARIES.subscribe(
value => (URL_JSQRSCANNER = value.LIB_JSQRSCANNER)
);
const enum notification_location {
"top-left",
"top-center",
"top-right",
"bottom-left",
"bottom-center",
"bottom-right"
}
const enum notification_type {
"success",
"warning",
"danger"
}
let jbScanner;
let scannerParentElement;
let scannedTextMemo;
let scannedTextMemoHist;
let scannedTextLast = "";
let scannerTextToggle = "Disable Scanner";
const protocol_error_message =
"Your web browser must have JavaScript enabled in order for this application to display correctly.";
const protocol_error_message_short =
"QRCode component requires a secure connection (https).";
const notify = (text: string) => {
addNotification({
text: "👋" + " " + text,
type: "danger",
position: "bottom-center",
removeAfter: 3000
});
};
const checkSecureConnection = () => {
if (location.protocol !== "https:") {
// @ts-ignore
document.getElementById("secure-connection-message").style =
"display: block";
}
};
// location.protocol !== "https:" ? notify(protocol_error_message_short) : null;
const onLoaded = () => {
// jbScanner = new JsQRScanner(onQRCodeScanned);
// @ts-ignore
jbScanner = new JsQRScanner(onQRCodeScanned, provideVideo);
jbScanner.setSnapImageMaxSize(300);
scannerParentElement = document.getElementById("scanner");
if (scannerParentElement) jbScanner.appendTo(scannerParentElement);
};
onMount(async () => {
// checkSecureConnection();
});
onDestroy(async () => {
unsubscribe;
jbScanner = {};
scannerParentElement = {};
scannedTextMemo = {};
scannedTextMemoHist = {};
scannedTextLast = "";
scannerTextToggle = "";
});
const provideVideo = () => {
let n = navigator;
if (n.mediaDevices && n.mediaDevices.getUserMedia) {
return n.mediaDevices.getUserMedia({
video: { facingMode: "environment" },
audio: false
});
}
return Promise.reject("Your browser does not support getUserMedia");
};
const provideVideoQQ = () => {
return navigator.mediaDevices
.enumerateDevices()
.then(function(devices) {
let exCameras = [];
devices.forEach(function(device) {
if (device.kind === "videoinput")
exCameras.push(device.deviceId);
});
return Promise.resolve(exCameras);
})
.then(function(ids) {
if (ids.length === 0)
return Promise.reject("Could not find a webcam");
return navigator.mediaDevices.getUserMedia({
//this way QQ browser opens the rear camera
video: {
// @ts-ignore
optional: [
{ sourceId: ids.length === 1 ? ids[0] : ids[1] }
]
}
});
});
};
const onQRCodeScanned = scannedText => {
scannedTextMemo = document.getElementById("scannedTextMemo");
if (scannedTextMemo) {
scannedTextMemo.value = scannedText;
scannedTextLast = scannedText;
scannedTextMemoHist = document.getElementById(
"scannedTextMemoHist"
);
// addNotification({text: scannedText,type: "success",position: "top-center",removeAfter: 3000,});
}
if (scannedTextMemo && scannedTextMemoHist)
scannedTextMemoHist.value =
scannedTextMemoHist.value + "\n" + scannedText;
};
const toggleQRCodeScanning = () => {
jbScanner.isActive()
? jbScanner.stopScanning()
: jbScanner.resumeScanning();
jbScanner.isActive()
? (scannerTextToggle = "Disable Scanner")
: (scannerTextToggle = "Enable Scanner");
jbScanner.isActive()
? addNotification({
text: "Scanner is Enabled",
type: "danger",
position: "top-center",
removeAfter: 3000
})
: null;
};
const errorHandler = e => {
console.error(`JsQRScanner.svelte error handler:${e}`);
};
$: {
scannedTextLast;
}
</script>
<LibLoader
src={URL_JSQRSCANNER}
libraryExpectedObject="Scanner"
on:loaded={onLoaded}
on:error={errorHandler}
/>
<svelte:head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</svelte:head>
<noscript>
<div class="row-element-set error_message">
{protocol_error_message}
</div>
</noscript>
<div
class="row-element-set error_message"
id="secure-connection-message"
style="display: none;"
hidden
>
You may need to serve this page over a secure connection (https) to run
JavascriptQRScanner correctly.
</div>
<section id="" class="px-2 py-2 bg-pink-100 md:py-24">
<div><h1>Component JsQRScanner.svelte</h1></div>
<p transition:fade={{ duration: 2000 }}>{scannedTextLast}</p>
<br />
<h1>JsQRScanner Example ({detectOS()})</h1>
<div class="row-element-set row-element-set-QRScanner">
<div class="row-element">
<div class="FlexPanel detailsPanel QRScannerShort">
<div class="FlexPanel shortInfoPanel">
<p>
To access the QR-Code generator for Token Addresses,
click on the profile icon likely a random cat picture.
</p>
<div class="gwt-HTML">Point the webcam at a QR code.</div>
<i
>QRCode scanner does not record videos, only looks for
QRcodes.</i
>
<p />
</div>
</div>
<br />
<div class="row-element">
<div class="qrscanner" id="scanner" />
</div>
<div class="row-element">
<div class="form-field form-field-memo">
<div class="form-field-caption-panel">
<div class="gwt-Label form-field-caption">
Scanned text
</div>
</div>
<div class="FlexPanel form-field-input-panel">
<textarea
id="scannedTextMemo"
class="textInput form-memo form-field-input textInput-readonly"
rows="3"
readonly
/>
</div>
</div>
<div class="form-field form-field-memo">
<div class="form-field-caption-panel">
<div class="gwt-Label form-field-caption">
Scanned text history
</div>
</div>
<div class="FlexPanel form-field-input-panel">
<textarea
id="scannedTextMemoHist"
class="textInput form-memo form-field-input textInput-readonly"
value=""
rows="6"
readonly
/>
</div>
</div>
</div>
<div class="row-element">
<button on:click={toggleQRCodeScanning}>
{scannerTextToggle}
</button>
</div>
<br />
</div>
</div>
</section>
<style>
.qrscanner {
max-width: 95%;
max-height: 75%;
}
.row-element-set {
display: flex;
flex-direction: column;
}
.row-element {
padding: 0.2em 0em;
}
.row-element-set-QRScanner {
max-width: 50em;
display: flex;
flex-direction: column;
}
.form-field-caption {
font-weight: bold;
}
.form-field-input {
width: 100%;
}
.error_message {
color: red;
background-color: white;
border: 1px solid red;
padding: 4px;
font-family: sans-serif;
}
</style>