summaryrefslogtreecommitdiff
path: root/resume/resume.js
blob: 500b49c06072f5a815c19aa12d9b2e87dc3bae98 (plain)
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
(document => {
    'use strict';
    let timeout, pending = [];
    const flush = () => {
        clearTimeout(timeout);
        navigator.sendBeacon('https://www.alxu.ca/analytics', new Blob([JSON.stringify(pending)], {type: 'application/json'}));
        pending = [];
    };
    const queue = (kind, details) => {
        clearTimeout(timeout);
        pending.push({created_ts: Date.now(), kind: kind, details: details});
        timeout = setTimeout(flush, 2000);
    };
    queue('l', '');
    document.addEventListener('visibilitychange', () => {
        queue('v', document.visibilityState);
        if (document.visibilityState === 'hidden')
            flush();
    });
    document.addEventListener('click', e => {
        const a = e.target.closest('a');
        if (a)
            queue('c', a.href);
    });
    window.addEventListener('beforeprint', () => {
        for (let el of document.getElementsByTagName('a')) {
            const href = el.href.replace(/^javascript:location=(.*);void 0$/, '$1');
            el.setAttribute('data-href', el.href);
            if (href != el.href) el.href = eval(href);
            else el.href = 'https://www.alxu.ca/analytics?url=' + href.replace(/[%&;]/g, s=>'%'+s.charCodeAt(0).toString(16));
        }
    });
    window.addEventListener('afterprint', () => {
        for (let el of document.getElementsByTagName('a')) {
            el.href = el.getAttribute('data-href');
            el.removeAttribute('data-href');
        }
    });
    const css = document.styleSheets[0];
    if (window.safari) {
        css.insertRule('@media print{@page{margin:10mm}}', css.cssRules.length);
        css.insertRule('@media print{body{margin:-2mm 0 -2mm 0;padding:0 15mm 0 7mm}}', css.cssRules.length);
    }
    else if (window.chrome)
        // changing @page causes chrome to claim layout shift even in non-print
        css.insertRule('@media print{@page{margin-top:auto;margin-bottom:auto}}', css.cssRules.length);
})(document);