-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
450 lines (443 loc) · 17.2 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
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
443
444
445
446
447
448
449
450
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style>
body {
font-family: "Skia", sans-serif;
color: #555;
}
@media screen and (orientation: portrait) {
body {
font-size: 200%;
}
}
.center {
margin: 0 auto;
text-align: center;
}
a {
text-decoration: inherit;
color: inherit;
}
a:hover {
text-decoration: underline;
}
ul {
list-style: none;
}
#card {
min-height: 100vh;
}
.headshot {
border-radius: 50%;
object-fit: cover;
width: 18vmin;
height: 18vmin;
}
#qrcode img {
padding: 10pt;
width: 50vmin;
height: 50vmin;
margin: 0 auto;
image-rendering: pixelated;
}
@media print {
.no-print,
.no-print * {
display: none !important;
}
}
h2 {
font-weight: inherit;
}
.hide {
display: none;
}
.float-right {
float: right;
}
svg {
width: 1em;
height: 1em;
}
</style>
<script
src="qrcode.min.js"
type="text/javascript"
integrity="sha384-3zSEDfvllQohrq0PHL1fOXJuC/jSOO34H46t6UQfobFOmxE5BpjjaIJY5F2/bMnU"
></script>
<script type="text/javascript" defer>
let flipped = false;
function upsidedown(flip) {
if (flip !== flipped) {
const body = document.getElementById("card");
body.style.transform = flip ? "rotate(180deg)" : null;
flipped = flip;
}
}
function handleDeviceOrientation(event) {
upsidedown(event.beta < -20 && event.gamma > -20 && event.gamma < 20);
}
function checkPermission() {
if (
window.DeviceOrientationEvent &&
DeviceOrientationEvent.requestPermission
) {
return DeviceOrientationEvent.requestPermission().then(
(state) => state === "granted"
);
}
return Promise.resolve(false);
}
function onAutoFlipChange(event) {
if (event.target.checked) {
checkPermission().then((granted) => {
if (granted) {
window.addEventListener(
"deviceorientation",
handleDeviceOrientation,
true
);
} else {
upsidedown(true);
}
});
} else {
window.removeEventListener(
"deviceorientation",
handleDeviceOrientation
);
upsidedown(false);
}
}
const INTERVAL = 30000;
const THUMB_SIZE = 64;
function createThumbnail(imgElement) {
const canvas = document.createElement("canvas");
canvas.width = THUMB_SIZE;
canvas.height = THUMB_SIZE;
const ctx = canvas.getContext("2d");
const ratio = imgElement.naturalHeight / imgElement.naturalWidth;
const dim = Math.min(imgElement.naturalWidth, imgElement.naturalHeight);
const scale = THUMB_SIZE / dim;
const dx = (imgElement.naturalWidth - dim) / 2;
const dy = (imgElement.naturalHeight - dim) / 2;
ctx.drawImage(
imgElement,
-dx * scale,
-dy * scale,
THUMB_SIZE / ratio,
THUMB_SIZE
);
return canvas.toDataURL("image/jpeg", 0.2);
}
function parseCard(vcard) {
const prefs = {};
const model = {};
const vcfRE =
/^(?:\w+\.)?(\w+)((?:;(?:[^";:]|".*?")*)*):(.+(\r?\n .+)*)$/gm;
const paramsRE = /;(\w+)(?:=((?:[^";:]*|".*?")(?:,[^";:]*|,".*?")*))?/g;
while ((match = vcfRE.exec(vcard)) !== null) {
let [, name, paramstr, value] = match;
// Property names are case-insensitive
name = name.toUpperCase();
// Parse the parameters (if any)
let pref = 0;
let mediatype = "";
let type = "";
while ((match = paramsRE.exec(paramstr)) !== null) {
const [, paramName, paramValue] = match;
// Parameter names are case-insensitive
switch (paramName.toUpperCase()) {
case "JPEG":
mediatype = "image/jpeg";
break;
case "MEDIATYPE":
mediatype = paramValue;
break;
case "TYPE":
if (paramValue === "pref") {
pref = 1;
} else {
type = paramValue.toLowerCase();
}
break;
case "PREF":
pref = paramValue | 0;
break;
}
}
// FIXME: unescape any ^n, ^^, ^' to \n, ^, ", respectively
// Only keep the highest preference for each property
const oldpref = prefs[name] || -1;
if (oldpref > pref) continue;
prefs[name] = pref;
switch (name) {
case "URL":
// FIXME: on MacOS, the type comes from X-ABLabel in the same group
model[`URL${type ? "-" + type : type}`] = value;
break;
case "PHOTO":
if (!mediatype) {
mediatype = `image/${type.toLowerCase() || "jpeg"}`;
}
model[name] = urlHasScheme(value)
? value
: `data:${mediatype};base64,` + value;
break;
case "N":
const nameParts = value.split(";");
// Only overwrite FN if it's not already set
// FIXME: check browser locale to decide correct order
model.FN = model.FN || `${nameParts[1]} ${nameParts[0]}`;
// fallthrough
case "EMAIL":
case "TITLE":
case "ORG":
case "FN":
case "TEL":
model[name] = value;
}
}
return model;
}
function timestamp() {
return new Date().toISOString().replace(/-|:|\.\d+/g, "");
}
function toVCardV3(model) {
const lines = [];
for (const name in model) {
if (model.hasOwnProperty(name)) {
let nameWithType = name.replace("-", ";TYPE=");
let value = model[name];
if (name === "PHOTO" && value.startsWith("data:")) {
nameWithType += ";ENCODING=b;TYPE=JPEG";
value = value.replace(/^data:image\/\w+;base64,/, "");
}
lines.push(`${nameWithType}:${value}`);
}
}
// FIXME: add UID, if it's not already there
// Sort the lines to make sure the order is consistent between runs
return [
"BEGIN:VCARD",
"VERSION:3.0",
`REV:${timestamp()}`,
...lines.sort(),
"END:VCARD",
].join("\r\n");
}
function urlHasScheme(url) {
return url.startsWith("data:") || url.startsWith("http");
}
function urlWithScheme(url, scheme = "https://") {
return urlHasScheme(url) ? url : scheme + url;
}
function urlWithoutScheme(url) {
return url.replace(/^https?:\/\//, "");
}
let showing = false;
function showCard(model) {
if (showing) return;
showing = true;
for (const name in model) {
if (!model.hasOwnProperty(name)) {
continue;
}
const value = model[name];
const element = document.getElementById(`v-${name}`);
if (!value || !element) {
continue;
}
element.classList.remove("hide");
switch (name) {
case "URL":
default:
element.href = urlWithScheme(value);
element.innerHTML += urlWithoutScheme(value);
break;
case "TEL":
element.href = "tel:" + value.replace(/[() -]+/g, "-");
element.innerText = value;
break;
case "EMAIL":
// FIXME: add vCards links
element.href = "mailto:" + value;
element.innerText = value;
break;
case "PHOTO":
// FIXME: detect image type
element.src = urlHasScheme(value)
? value
: "data:image/jpeg;base64," + value;
break;
case "FN":
case "ORG":
case "TITLE":
element.innerText = value;
}
}
}
function updateMyCard(vcard) {
const card = parseCard(vcard);
localStorage.setItem("vcard", vcard);
window.location.hash = "";
window.location.reload();
}
function onUploadChange(e) {
const file = e.target.files[0];
file.text().then(updateMyCard);
}
let model;
function onPhotoLoad(e) {
if (model && (!model.PHOTO || !model.PHOTO.startsWith("data:"))) {
// FIXME: only update the card if the photo is smaller
model.PHOTO = createThumbnail(e.target);
const card = toVCardV3(model);
window.location.hash = encodeURIComponent(card);
}
}
function onLoad() {
const upload = document.getElementById("upload");
upload.addEventListener("change", onUploadChange);
const autoFlip = document.getElementById("autoFlip");
autoFlip.addEventListener("change", onAutoFlipChange);
const photo = document.getElementById("v-PHOTO");
photo.addEventListener("load", onPhotoLoad);
window.addEventListener(
"deviceorientation",
handleDeviceOrientation,
true
);
let card = decodeURIComponent(window.location.hash.slice(1));
const mycard = localStorage.getItem("vcard");
if (card) {
model = parseCard(card);
showCard(model);
} else if (mycard) {
model = parseCard(mycard);
showCard(model);
// The default photo is likely too big for the QR code; delete it
delete model.PHOTO;
card = toVCardV3(model);
window.location.hash = encodeURIComponent(card);
} else {
// Nothing to show
return;
}
const vcard = document.getElementById("vcard");
vcard.classList.remove("hide");
vcard.setAttribute("download", `${model.FN || "card"}.vcf`);
vcard.setAttribute(
"href",
"data:text/vcard;charset=utf-8," + encodeURIComponent(card)
);
const qrcode = document.getElementById("qrcode");
qrcode.innerHTML = "";
const qr = new QRCode(qrcode, {
text: window.location.href,
correctLevel: QRCode.CorrectLevel.L,
width: 531,
height: 531,
});
// FIXME: update REV/timestamp in vcard
//setInterval(() => qr.makeCode(qrLink(link)), INTERVAL);
}
window.addEventListener("DOMContentLoaded", onLoad);
</script>
</head>
<body>
<div id="card">
<div class="center">
<p>
<img id="v-PHOTO" alt="Headshot" class="headshot" src="bust.png" />
</p>
<h1 id="v-FN">Scroll Down</h1>
<h2>
<span id="v-TITLE" class="hide">Title</span><br />
<a id="v-URL-work">
<span id="v-ORG" class="hide">Acme Labs</span>
</a>
</h2>
<p>
<a id="v-TEL" href="tel:+1-604-555-9599" class="hide">
+1 604-555-9599
</a>
</p>
<p>
<a id="v-EMAIL" href="mailto:[email protected]" class="hide">
</a>
</p>
<div id="qrcode">
<img
alt="https://noun.ly/qr"
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADoAAAA6AQMAAADbddhrAAAABlBMVEUAAAD///+l2Z/dAAAAt0lEQVQoz1XRMQrEQAgFUMF2wKsItoJXF6YdyFUGbAU3y2YTY/Uav6IAuLcynPWHBSyPamBxo8AXtoq84Y7RYSE0fjkXAEXkN+tCFU2r8gbIkZwNucwgz/Ybis6a0mFGG8EfGKdNcngQa2W5+YMDNem7zg3SAo7RgLFppzWoZgBhQ01WoaNjxJy7GpAPXsANVUDLOgBVXajhfMGhNhpYeDoM75g+v6drWMhjwwOLBZLQAFhh2/3GB9gMv7nUc7s2AAAAAElFTkSuQmCC"
/>
</div>
</div>
<ul class="social-media-list center">
<li>
<a
id="v-URL-linkedin"
href="https://linkedin.com/in/firstlast"
class="hide"
>
<span class="icon">
<svg viewBox="0 0 16 16">
<path
fill="#007bb6"
d="M 0.80160691,15.941393 C 0.63274456,15.888473 0.39867188,15.735782 0.28105673,15.601826 0.2287347,15.542234 0.14409248,15.405199 0.09296288,15.297302 L 0,15.101126 V 7.9834659 0.86580609 L 0.11302913,0.63710738 C 0.24164903,0.37686233 0.40760266,0.2118324 0.67052754,0.0827118 L 0.83895151,0 H 8.0000004 15.161049 l 0.168424,0.0827118 c 0.262924,0.1291206 0.428878,0.29415053 0.557498,0.55439558 L 16,0.86580609 V 7.9850199 15.104234 l -0.113029,0.228699 c -0.12862,0.260244 -0.294574,0.425274 -0.557498,0.554395 l -0.168424,0.08271 -7.1161049,0.0056 c -5.8515259,0.0046 -7.13871511,-0.0015 -7.24333719,-0.03426 z M 4.7490637,9.8127354 V 5.9775288 H 3.550562 2.3520601 V 9.8127354 13.647942 H 3.550562 4.7490637 Z m 3.8731322,1.6104876 c 0.012375,-2.4730956 0.00973,-2.4381621 0.216642,-2.8586691 0.1353217,-0.275013 0.355893,-0.4824795 0.619158,-0.5823698 0.1638518,-0.062167 0.2414873,-0.072067 0.5651561,-0.072067 0.36471,0 0.380888,0.00277 0.578067,0.09989 0.33297,0.1639605 0.513861,0.4512442 0.606855,0.96378 0.03194,0.1760227 0.041,0.7068188 0.04182,2.4494389 l 0.0011,2.224719 h 1.200279 1.20028 l -0.01198,-2.629213 C 13.627938,8.4772764 13.625381,8.3781684 13.562882,8.0495056 13.42115,7.3041251 13.220612,6.8708388 12.838961,6.4853846 12.416816,6.0590328 11.935116,5.8625381 11.147159,5.7952631 10.336868,5.7260831 9.7558202,5.8703876 9.1985116,6.2792178 9.0062694,6.4202433 8.7343036,6.7068971 8.6164816,6.8926833 L 8.5243449,7.0379508 V 6.5077398 5.9775288 H 7.370787 6.2172285 v 3.8352066 3.8352066 h 1.196916 1.1969169 z M 3.9673121,4.8720738 C 4.9549905,4.5646737 5.2589678,3.3117864 4.524153,2.5769712 4.2474712,2.3002896 3.9419375,2.1737499 3.550562,2.1737499 c -0.7812801,0 -1.3782771,0.5951637 -1.3782771,1.3740436 0,0.6169258 0.3753024,1.1386133 0.9516687,1.3228636 0.2334683,0.074632 0.6061013,0.07526 0.8433585,0.00143 z"
></path>
</svg>
</span>
</a>
</li>
<li>
<a id="v-URL-github" href="https://github.com/firstlast" class="hide">
<span class="icon">
<svg viewBox="0 0 16 16">
<path
fill="#161513"
d="M7.999,0.431c-4.285,0-7.76,3.474-7.76,7.761 c0,3.428,2.223,6.337,5.307,7.363c0.388,0.071,0.53-0.168,0.53-0.374c0-0.184-0.007-0.672-0.01-1.32 c-2.159,0.469-2.614-1.04-2.614-1.04c-0.353-0.896-0.862-1.135-0.862-1.135c-0.705-0.481,0.053-0.472,0.053-0.472 c0.779,0.055,1.189,0.8,1.189,0.8c0.692,1.186,1.816,0.843,2.258,0.645c0.071-0.502,0.271-0.843,0.493-1.037 C4.86,11.425,3.049,10.76,3.049,7.786c0-0.847,0.302-1.54,0.799-2.082C3.768,5.507,3.501,4.718,3.924,3.65 c0,0,0.652-0.209,2.134,0.796C6.677,4.273,7.34,4.187,8,4.184c0.659,0.003,1.323,0.089,1.943,0.261 c1.482-1.004,2.132-0.796,2.132-0.796c0.423,1.068,0.157,1.857,0.077,2.054c0.497,0.542,0.798,1.235,0.798,2.082 c0,2.981-1.814,3.637-3.543,3.829c0.279,0.24,0.527,0.713,0.527,1.437c0,1.037-0.01,1.874-0.01,2.129 c0,0.208,0.14,0.449,0.534,0.373c3.081-1.028,5.302-3.935,5.302-7.362C15.76,3.906,12.285,0.431,7.999,0.431z"
></path>
</svg>
</span>
</a>
</li>
<li>
<a
id="v-URL-mastodon"
rel="me"
href="https://mastodon.social/@firstlast"
class="hide"
>
<span class="icon">
<svg viewBox="0 0 16 16">
<path
fill="#6364ff"
d="M11.19 12.195c2.016-.24 3.77-1.475 3.99-2.603.348-1.778.32-4.339.32-4.339 0-3.47-2.286-4.488-2.286-4.488C12.062.238 10.083.017 8.027 0h-.05C5.92.017 3.942.238 2.79.765c0 0-2.285 1.017-2.285 4.488l-.002.662c-.004.64-.007 1.35.011 2.091.083 3.394.626 6.74 3.78 7.57 1.454.383 2.703.463 3.709.408 1.823-.1 2.847-.647 2.847-.647l-.06-1.317s-1.303.41-2.767.36c-1.45-.05-2.98-.156-3.215-1.928a3.614 3.614 0 0 1-.033-.496s1.424.346 3.228.428c1.103.05 2.137-.064 3.188-.189zm1.613-2.47H11.13v-4.08c0-.859-.364-1.295-1.091-1.295-.804 0-1.207.517-1.207 1.541v2.233H7.168V5.89c0-1.024-.403-1.541-1.207-1.541-.727 0-1.091.436-1.091 1.296v4.079H3.197V5.522c0-.859.22-1.541.66-2.046.456-.505 1.052-.764 1.793-.764.856 0 1.504.328 1.933.983L8 4.39l.417-.695c.429-.655 1.077-.983 1.934-.983.74 0 1.336.259 1.791.764.442.505.661 1.187.661 2.046v4.203z"
></path>
</svg>
</span>
</a>
</li>
<!-- FIXME: add twitter, facebook, instagram, … -->
</ul>
</div>
<div id="ui" class="no-print center">
<div>
<a class="hide" id="vcard" href="card.vcf">Download vCard</a>
</div>
<div>
<label for="upload">Upload vCard</label>
<input id="upload" class="hide" type="file" accept=".vcf,text/vcard" />
</div>
<div>
<label for="autoFlip">Auto-Flip</label>
<input id="autoFlip" class="hide" type="checkbox" />
</div>
</div>
</body>
</html>