From a93d44efbc5ee7e78523c544341325b71b347f2a Mon Sep 17 00:00:00 2001 From: "Alex Xu (Hello71)" Date: Tue, 5 Oct 2021 15:31:08 -0400 Subject: enable analytics for alxu.ca only, fix chrome CLS --- resume/resume.js | 86 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/resume/resume.js b/resume/resume.js index 3f33976..adcb7de 100644 --- a/resume/resume.js +++ b/resume/resume.js @@ -1,51 +1,55 @@ (function(w, d, a){ 'use strict'; + const b = () => { + for (let el of d.getElementsByTagName('a')) { + const h = el.href.replace(/javascript:location=(.*);void 0/, '$1'); + el.setAttribute('data-href', el.href); + if (h != el.href) el.href = eval(h); + else el.href = "https://alxu.ca/analytics?url=" + el.href.replace(/%/g, '%25').replace(/&/g, '%26').replace(/;/g, '%3B'); + } + }; + if (location.hostname === 'www.alxu.ca') { + let t, p = []; + const f = () => { + clearTimeout(t); + navigator.sendBeacon('/analytics', new Blob([JSON.stringify(p)], {type: 'application/json'})); + p = []; + }; + const q = (kind, details) => { + clearTimeout(t); + p.push({created_ts: Date.now(), kind: kind, details: details}); + t = setTimeout(f, 2000); + }; + q('l', ''); + d[a]('visibilitychange', () => { + const vs = d.visibilityState; + q('v', vs); + if (vs === 'hidden') + f(); + }); + d[a]('click', e => { + const a = e.target.closest('a'); + if (a) + q('c', a.href); + }); + w[a]('beforeprint', b); + w[a]('afterprint', function () { + for (let el of d.getElementsByTagName('a')) { + el.href = el.getAttribute('data-href'); + el.removeAttribute('data-href'); + } + }); + } else { + b(); + } if (d.documentMode) ie.style.display = "block"; - else - ie.parentNode.removeChild(ie); const css = d.styleSheets[0]; + // changing @page causes chrome to claim layout shift if (w.safari) { - css.insertRule("@page{margin:10mm}", css.cssRules.length); + 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 (w.chrome) - css.insertRule("@page{margin-top:auto;margin-bottom:auto}", css.cssRules.length); - let t, p = []; - const f = () => { - clearTimeout(t); - navigator.sendBeacon('/analytics', new Blob([JSON.stringify(p)], {type: 'application/json'})); - p = []; - }; - const q = (kind, details) => { - clearTimeout(t); - p.push({created_ts: Date.now(), kind: kind, details: details}); - t = setTimeout(f, 2000); - }; - q('l', ''); - d[a]('visibilitychange', () => { - const vs = d.visibilityState; - q('v', vs); - if (vs === 'hidden') - f(); - }); - d[a]('click', e => { - const a = e.target.closest('a'); - if (a) - q('c', a.href); - }); - w[a]('beforeprint', () => { - for (let el of d.getElementsByTagName('a')) { - const h = el.href.replace(/javascript:location=(.*);void 0/, '$1'); - el.setAttribute('data-href', el.href); - if (h != el.href) el.href = eval(h); - else el.href = "https://alxu.ca/analytics?url=" + el.href.replace(/%/g, '%25').replace(/&/g, '%26').replace(/;/g, '%3B'); - } - }); - w[a]('afterprint', function () { - for (let el of d.getElementsByTagName('a')) { - el.href = el.getAttribute('data-href'); - el.removeAttribute('data-href'); - } - }); + css.insertRule("@media print{@page{margin-top:auto;margin-bottom:auto}}", css.cssRules.length); }(window, document, 'addEventListener')); -- cgit v1.2.3-70-g09d2