-
Notifications
You must be signed in to change notification settings - Fork 7
/
js-demo.html
302 lines (243 loc) · 8.49 KB
/
js-demo.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="color-scheme" content="light dark" />
<title>BBQr JS Demo</title>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2.0.3/css/pico.css"
integrity="sha384-eMqr88t8LtEqdsA81XqlcxxMpxUHniTNNCXEEe9lW0356TLykUqw97i/Gxpw1GU2"
crossorigin="anonymous"
/>
<script
src="https://cdn.jsdelivr.net/npm/bbqr@1.0.0/dist/bbqr.iife.js"
integrity="sha256-cqy1Qgi1C2i4pYywJlbVCRTJ+kDIrVqLc0KH21cMicQ="
crossorigin="anonymous"
></script>
<style>
html {
height: 100%;
}
body {
height: 100%;
}
.hero {
width: fit-content;
margin: 0 auto;
font-weight: 700;
font-size: min(1.95vw, 1.2rem);
padding: 0.75em 1.5em;
line-height: 1;
}
.drag-over {
cursor: copy;
}
.drag-overlay {
display: none;
}
.drag-over .drag-overlay {
display: flex;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: hsla(0, 0%, 0%, 0.7);
}
.drag-overlay > * {
margin: auto;
color: hsla(0, 100%, 100%, 0.9);
font-weight: 600;
font-size: 2rem;
}
textarea {
width: 100%;
}
dialog img {
display: block;
margin: 0 auto;
}
dialog.busy .success {
display: none;
}
dialog.error .success {
display: none;
}
dialog:not(.busy) .busy {
display: none;
}
dialog:not(.error) .error {
display: none;
}
dialog img {
border: 1px solid hsla(0, 0%, 0%, 0.1);
border-radius: 0.5rem;
}
</style>
</head>
<body>
<header>
<pre class="hero">
BBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBB QQQQQQQQQ
B::::::::::::::::B B::::::::::::::::B QQ:::::::::QQ
B::::::BBBBBB:::::B B::::::BBBBBB:::::B QQ:::::::::::::QQ
BB:::::B B:::::BBB:::::B B:::::BQ:::::::QQQ:::::::Q
B::::B B:::::B B::::B B:::::BQ::::::O Q::::::Q rrrrr rrrrrrrrr
B::::B B:::::B B::::B B:::::BQ:::::O Q:::::Q r::::rrr:::::::::r
B::::BBBBBB:::::B B::::BBBBBB:::::B Q:::::O Q:::::Q r:::::::::::::::::r
B:::::::::::::BB B:::::::::::::BB Q:::::O Q:::::Q rr::::::rrrrr::::::r
B::::BBBBBB:::::B B::::BBBBBB:::::B Q:::::O Q:::::Q r:::::r r:::::r
B::::B B:::::B B::::B B:::::BQ:::::O Q:::::Q r:::::r rrrrrrr
B::::B B:::::B B::::B B:::::BQ:::::O QQQQ:::::Q r:::::r
B::::B B:::::B B::::B B:::::BQ::::::O Q::::::::Q r:::::r
BB:::::BBBBBB::::::BBB:::::BBBBBB::::::BQ:::::::QQ::::::::Q r:::::r
B:::::::::::::::::B B:::::::::::::::::B QQ::::::::::::::Q r:::::r
B::::::::::::::::B B::::::::::::::::B QQ:::::::::::Q r:::::r
BBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBB QQQQQQQQ::::QQ rrrrrrr
Q:::::Q
QQQQQQ</pre
>
</header>
<main class="container">
<p>
This page makes use of the
<a href="https://www.npmjs.com/package/bbqr" target="_blank" rel="noopener"
>BBQr JavaScript library</a
>
to apply BBQr encoding to PSBTs, raw Bitcoin transactions, or other types of data. Visit the
<a href="https://bbqr.org">BBQr home page</a> for more information.
</p>
<p><strong>How to use?</strong></p>
<ul>
<li>Option A: Drag and drop a file anywhere on this page.</li>
<li>Option B: Paste the contents of a file into the textbox below.</li>
</ul>
<p>
<em
>Note: PSBTs and Bitcoin transactions are supported in raw binary format as well as
encoded as Base64 or HEX.</em
>
</p>
<textarea id="text-input" rows="8" placeholder="Paste file contents here..."></textarea>
<dialog id="result-dialog">
<article class="success">
<h2>BBQr created</h2>
<div id="result-text"></div>
<footer>
<button>Dismiss</button>
</footer>
</article>
<article class="error">
<h2>Error!</h2>
<div id="error-text"></div>
<footer>
<button>Dismiss</button>
</footer>
</article>
<article class="busy">
<h2>Generating BBQr, please wait...</h2>
<footer>
<button>Cancel</button>
</footer>
</article>
</dialog>
</main>
<div class="drag-overlay">
<p>Drop your file here!</p>
</div>
<script>
const resultEl = document.querySelector('#result-dialog');
const resultContentEl = document.querySelector('#result-text');
const errorContentEl = document.querySelector('#error-text');
const inputEl = document.querySelector('#text-input');
function clearPrevious() {
const existingImgs = resultEl.querySelectorAll('img');
existingImgs.forEach((img) => {
// remove references to any old images
URL.revokeObjectURL(img.src);
});
resultContentEl.innerHTML = '';
resultEl.classList.remove('error');
}
let busy = false;
async function handleFileOrTextInput(e) {
if (busy) {
return;
}
busy = true;
resultEl.classList.add('busy');
resultEl.showModal();
try {
clearPrevious();
let input;
if (e instanceof DragEvent) {
e.preventDefault();
document.body.classList.remove('drag-over');
if (!e.dataTransfer) {
return;
}
const files = [];
for (const item of e.dataTransfer.items) {
if (item.kind === 'file') {
const file = item.getAsFile();
if (file) {
files.push(file);
}
}
}
if (files.length > 1) {
throw new Error('Only one file at a time, please!');
} else if (files.length === 1) {
inputEl.value = '';
input = files[0];
}
} else {
// paste event
input = e.clipboardData?.getData('text');
}
let resultMsg = '';
const { raw, fileType } = await BBQr.detectFileType(input);
resultMsg += `<ul><li>File type: <strong>${fileType}</strong><br></li>`;
const { parts, version } = await BBQr.splitQRs(raw, fileType);
const imgBuf = await BBQr.renderQRImage(parts, version);
resultMsg += `<li>Number of parts: <strong>${parts.length}</strong></li>`;
resultMsg += `<li>QR code version: <strong>${version}</strong></li></ul>`;
resultMsg += `<p>Click on the image to save it.</p>`;
resultContentEl.innerHTML = resultMsg;
const url = URL.createObjectURL(new Blob([imgBuf], { type: 'image/png' }));
const imgName = input instanceof File ? input.name + '.png' : 'bbqr.png';
resultContentEl.innerHTML += `<a class="bbqr" href="${url}" download="${imgName}"><img src="${url}" alt="Rendered BBQr" /></a>`;
} catch (err) {
errorContentEl.textContent = err.message;
resultEl.classList.add('error');
} finally {
resultEl.classList.remove('busy');
busy = false;
}
}
document.addEventListener('dragenter', () => {
document.body.classList.add('drag-over');
});
document.addEventListener('dragleave', (e) => {
e.preventDefault();
if (!e.relatedTarget) {
document.body.classList.remove('drag-over');
}
});
document.addEventListener('dragover', (e) => {
// prevent browser from opening the file when dropped
e.preventDefault();
});
document.addEventListener('drop', handleFileOrTextInput);
// detect paste in textarea
inputEl.addEventListener('paste', handleFileOrTextInput);
document.addEventListener('click', (e) => {
if (e.target !== resultEl) {
resultEl.close();
}
});
</script>
</body>
</html>