-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.html
More file actions
442 lines (400 loc) · 13.9 KB
/
index.html
File metadata and controls
442 lines (400 loc) · 13.9 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="data:;base64,=">
<title>QR Code JS</title>
<style type="text/css">
html, body {
font-family: sans-serif;
}
.controls {
margin: 1em 0;
}
textarea {
resize: both;
field-sizing: content;
vertical-align: top;
min-width: 80%;
}
summary {
cursor: pointer;
}
li {
margin-top: 0.25em;
margin-bottom: 0.25em;
}
.hidden {
display: none;
}
#text {
width: 90%;
border-radius: 0.5em;
}
#output_text {
cursor: copy;
font-family: monospace;
-webkit-font-smoothing: none;
font-smooth: never;
text-rendering: optimizeSpeed;
line-height: 1.1;
white-space: nowrap;
}
/* Fix for monospace fonts where the space is not the same width as the block characters */
#output_text span {
display: inline-block;
width: 1ch;
}
#output_img, #output_svg {
cursor: copy;
}
#output_img img, #output_svg svg {
width: 50vw;
max-width: 75vmin;
}
#caution-input, #error {
background: #ffffdd;
border: 1px solid #cccc00;
padding: 0.5em;
margin: 1em 0;
border-radius: 0.5em;
}
#error {
background: #ffdddd;
border: 1px solid #cc0000;
}
#caution-input::before {
content: "⚠️ ";
}
#error::before {
content: "❗ ";
}
</style>
</head>
<body>
<h1>QR Code JS</h1>
<ul>
<li>Generate a QR Code for any input.</li>
<li>Data stays only in your browser and is not sent anywhere else.</li>
<li>Open source repository: <a href="https://github.com/danielgjackson/qrcodejs">github.com/danielgjackson/qrcodejs</a></li>
</ul>
<div class="controls">
<label><input type="checkbox" id="rounded" checked>Rounded.</label>
<label>Error correction level:
<select id="ecl">
<option value="L+" selected>Low, or better for same size (~7%+)</option>
<option value="M+">Medium, or better for same size (~15%+)</option>
<option value="Q+">Quartile, or better for same size (~25%+)</option>
<option value="H">High (~30%)</option>
<option value="Q">Quartile (~25%)</option>
<option value="M">Medium (~15%)</option>
<option value="L">Low (~7%)</option>
</select>
</label>
</div>
<div id="caution-input" class="hidden">
<strong>Caution:</strong> Please double-check the input as it may unintentionally contain: multiple lines, or leading/trailing whitespace, or is an improperly formatted URL.
</div>
<div id="error" class="hidden">
<strong>Error:</strong> <span id="error_message">-</span>
</div>
<div class="controls">
<label>
Text:
<textarea type="text" id="text" wrap="soft" rows="4" autofocus placeholder="https://example.com"></textarea>
</label>
</div>
<details name="qrcode" open>
<summary>Image <svg></summary>
<div id="output_svg">
<svg></svg>
</div>
</details>
<details name="qrcode">
<summary>Image <img></summary>
<div id="output_img">
<img src="" alt="">
</div>
</details>
<details name="qrcode">
<summary>Text</summary>
<div id="output_text">
-
</div>
</details>
<ul>
<li>Simpler codes are easier to scan:
<ul>
<li>Keep the input as short as possible.</li>
<li>If using a URL with a common top-level-domain (such as <code>.com</code>), some readers work without an initial <code>http://</code> or <code>https://</code>, but it's probably best not to rely on this.</li>
<li>All upper-case alphanumeric (with some, e.g. URL, symbols allowed) is more compact than mixed-case, but be careful that any path part may be case sensitive, and some readers may not like the protocol in capital letters.</li>
<li>Lower error correction levels can decrease complexity, but may be more susceptible to damage or occlusion.</li>
</ul>
</li>
<li>Always include a written alternative for the URL.</li>
<li>Always place the QR code on a 'quiet' area: light on a standard code (if the code is inverted, it should be placed on a dark background).</li>
<li>For WiFi QR codes, use the format: <code>WIFI:S:my_ssid;T:WPA;P:my_passphrase;;</code></li>
</ul>
<!-- Don't use as a module here so that this demo page can work over the file: protocol -->
<script src="./qrcode.mjs"></script>
<script> <!-- type="module" -->
//import QrCode from './qrcode.mjs';
function renderHtml(matrix) {
// '▀', '▄', '█' // '\u{0020}' space, '\u{2580}' upper half block, '\u{2584}' lower half block, '\u{2588}' block
// Figure space: '\u{2007}'
const renderOptions = {
segments: ['<span> </span>', '<span>▀</span>', '<span>▄</span>', '<span>█</span>'],
sep: '<br>',
};
return QrCode.render('medium', matrix, renderOptions);
}
function focus() {
document.querySelector('#text').select();
}
function change() {
let errorMessage = null;
const input = document.querySelector('#text').value;
//console.log('Input:', JSON.stringify(input));
// Check for possible ill-formed input
const trimmedInput = input.trim();
const whitespace = input.length != trimmedInput.length;
const multipleLines = trimmedInput.includes('\n');
const startsWithHttp = trimmedInput.toLowerCase().startsWith('http:') || trimmedInput.toLowerCase().startsWith('https:');
const startsWithWiFi = trimmedInput.toUpperCase().startsWith('WIFI:');
const nonUrlCharacters = trimmedInput.match(/[^a-zA-Z0-9\-._~:/?#[\]@!$&'()*+,;=%]/);
let caution = (startsWithHttp || startsWithWiFi) && (whitespace || multipleLines);
if (startsWithHttp && URL.canParse && !URL.canParse(trimmedInput)) {
caution = true;
}
document.querySelector('#caution-input').classList.toggle('hidden', !caution);
let text = '';
let svg;
try {
const ecl = document.querySelector('#ecl').value;
const qrOptions = {
errorCorrectionLevel: QrCode.ErrorCorrectionLevel[ecl[0]],
optimizeEcc: ecl.includes('+'),
};
const matrix = QrCode.generate(input, qrOptions);
text = renderHtml(matrix);
const renderOptions = {
color: 'currentColor',
moduleRound: null, // 1.0 looks nice
finderRound: null, // 0.5 looks nice
alignmentRound: null,
white: false, // Output an element for every module, not just black/dark ones but white/light ones too.
moduleSize: 1,
};
if (document.querySelector('#rounded').checked) {
renderOptions.moduleRound = 1.0;
renderOptions.finderRound = 0.5;
}
svg = QrCode.render('svg', matrix, renderOptions);
} catch (e) {
let message = JSON.stringify(e);
// Check for currently unsupported input
const currentlyUnsupportedInput = [...input].some(c => c.charCodeAt(0) < 0x00 || c.charCodeAt(0) > 0xff);
if (currentlyUnsupportedInput) {
message += ' -- Note: the input contains characters outside the ISO-8859-1 (Latin-1) range (0x00-0xff), and this is currently unsupported.';
}
console.error('Error: ' + message);
errorMessage = message;
}
const textElement = document.querySelector('#output_text');
const imgElement = document.querySelector('#output_img img');
const svgElement = document.querySelector('#output_svg svg');
textElement.innerHTML = text;
imgElement.src = 'data:image/svg+xml,' + encodeURIComponent(svg);
imgElement.alt = input;
svgElement.alt = input;
if (svg) {
svgElement.outerHTML = svg;
} else {
svgElement.innerHTML = svg;
}
if (errorMessage) {
document.querySelector('#error').classList.remove('hidden');
document.querySelector('#error_message').innerText = errorMessage;
} else {
document.querySelector('#error').classList.add('hidden');
document.querySelector('#error_message').innerText = '-';
}
}
function copyMultiLinePlainTextToClipboard(text) {
const temp = document.createElement('textarea');
temp.value = text;
document.body.appendChild(temp);
temp.select();
document.execCommand('copy');
document.body.removeChild(temp);
}
function safeFilename(text) {
text = text.replace(/^https?:\/\//i, '');
text = text.replaceAll(/[^a-zA-Z0-9_.-]/g, '_').toLowerCase();
text = text.replaceAll(/\.+/g, '.');
text = text.replaceAll(/_+/g, '_');
return text;
}
async function dataUriToBlob(dataUri) {
try {
return await (await fetch(dataUri)).blob();
} catch (e) {
const protocolParts = dataUri.split(':');
if (protocolParts[0] != 'data') throw new Error('Invalid data URI');
const nonProtocol = protocolParts.slice(1).join(':');
const dataParts = nonProtocol.split(',');
const metaParts = dataParts[0].split(';');
const type = metaParts[0];
const encoding = metaParts[1];
const rawData = dataParts.slice(1).join('');
const data = encoding == 'base64' ? atob(rawData) : decodeURIComponent(rawData);
const bytes = [];
for (let i = 0; i < binary.length; i++) bytes.push(data.charCodeAt(i));
return new Blob([new Uint8Array(bytes)], { type });
}
}
async function rasterizeImageToBlob(src, type = 'image/png') {
const img = new Image();
const ctx = document.createElement('CANVAS').getContext('2d');
img.crossOrigin = '';
img.src = src;
return new Promise(resolve => {
img.onload = () => {
ctx.canvas.width = img.naturalWidth;
ctx.canvas.height = img.naturalHeight;
ctx.drawImage(img, 0, 0);
ctx.canvas.toBlob((blob) => resolve(blob), type);
};
});
}
function svgToBlob(svg, type = 'image/svg+xml') {
return new Blob([svg], { type });
}
function copyBlobToClipboard(blob) {
const itemSrc = {};
itemSrc[blob.type] = blob;
const item = new ClipboardItem(itemSrc);
return navigator.clipboard.write([item]);
}
function downloadBlob(blob, filename = null, type = null) { // 'application/octet-stream'
if (!(blob instanceof Blob)) { blob = new Blob([blob], type ? { type } : undefined); }
const elem = window.document.createElement('A');
elem.href = window.URL.createObjectURL(blob);
if (filename) elem.download = filename;
if (type) elem.type = type;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
async function copySvgToClipboard(svg, imgSrc, element) {
// Try to copy the SVG as an image to the clipboard
try {
if ('write' in navigator.clipboard && navigator.permissions) {
const permissionResult = await navigator.permissions.query({ name: 'clipboard-write' });
if (permissionResult.state === 'granted') {
const svgBlob = svgToBlob(svg);
const pngBlob = await rasterizeImageToBlob(imgSrc);
const htmlBlob = svgToBlob(svg, 'text/html');
const textBlob = svgToBlob(svg, 'text/plain');
console.log('COPY: clipboard.write(ClipboardItem) -- SVG')
try {
const item = new ClipboardItem({
'image/svg+xml': svgBlob,
//'image/png': pngBlob,
//'text/html': htmlBlob,
//'text/plain': textBlob,
});
await navigator.clipboard.write([item]);
return true;
} catch (e) {
console.error(e.name, e.message);
}
console.log('COPY: clipboard.write(ClipboardItem) -- PNG')
try {
const item = new ClipboardItem({
//'image/svg+xml': svgBlob,
'image/png': pngBlob,
//'text/html': htmlBlob,
//'text/plain': textBlob,
});
await navigator.clipboard.write([item]);
return true;
} catch (e) {
console.error(e.name, e.message);
}
console.log('COPY: clipboard.write(ClipboardItem) -- HTML')
try {
const item = new ClipboardItem({
//'image/svg+xml': svgBlob,
//'image/png': pngBlob,
'text/html': htmlBlob,
'text/plain': textBlob,
});
await navigator.clipboard.write([item]);
return true;
} catch (e) {
console.error(e.name, e.message);
}
}
}
} catch (e) {
console.error(e.name, e.message);
}
try {
// Fallback: this tries to copy the selected range around the image as HTML
console.log('COPY: execCommand(copy) range')
window.getSelection().removeAllRanges();
const range = document.createRange();
range.selectNode(element);
window.getSelection().addRange(range);
document.execCommand('copy');
window.getSelection().removeAllRanges();
return true;
} catch (e) {
console.error(e.name, e.message);
}
// Fall back to copying source
try {
console.log('COPY: writeText source')
await navigator.clipboard.writeText(svg);
return true;
} catch (e) {
console.error(e.name, e.message);
}
return false;
}
function click_text() {
const text = document.querySelector('#output_text').innerText;
copyMultiLinePlainTextToClipboard(text);
document.querySelector('#text').focus();
}
async function click_img() {
const element = document.querySelector('#output_img img');
const svg = decodeURIComponent(element.src.split(',').slice(1).join(''));
//const imgSrc = element.src;
//copySvgToClipboard(svg, imgSrc, element);
downloadBlob(svgToBlob(svg), safeFilename(document.querySelector('#text').value) + '.svg');
}
function click_svg() {
const element = document.querySelector('#output_svg svg');
const svg = element.outerHTML;
//const imgSrc = 'data:image/svg+xml,' + encodeURIComponent(svg);
//copySvgToClipboard(svg, imgSrc, element);
downloadBlob(svgToBlob(svg), safeFilename(document.querySelector('#text').value) + '.svg');
}
function start() {
document.querySelector('#text').addEventListener('focus', focus);
document.querySelector('#text').addEventListener('input', change);
document.querySelector('#ecl').addEventListener('change', change);
document.querySelector('#rounded').addEventListener('change', change);
document.querySelector('#output_text').addEventListener('click', click_text);
document.querySelector('#output_img img').addEventListener('click', click_img);
document.querySelector('#output_svg').addEventListener('click', click_svg);
document.querySelector('#text').focus();
change();
}
window.addEventListener('DOMContentLoaded', start);
</script>
</body>
</html>