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
|
(document => {
'use strict';
const mangleurls = () => {
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://alxu.ca/analytics?url=' + href.replace(/[%&;]/g, s=>'%'+s.charCodeAt(0).toString(16));
}
};
if (location.hostname === 'www.alxu.ca') {
let timeout, pending = [];
const flush = () => {
clearTimeout(timeout);
navigator.sendBeacon('/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', mangleurls);
window.addEventListener('afterprint', () => {
for (let el of document.getElementsByTagName('a')) {
el.href = el.getAttribute('data-href');
el.removeAttribute('data-href');
}
});
} else {
mangleurls();
}
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);
|