From 60cea36e4deda33142cb641534abe3e7a698ebde Mon Sep 17 00:00:00 2001 From: "Alex Xu (Hello71)" Date: Wed, 1 Jun 2022 20:59:06 -0400 Subject: update --- resume/resume.js | 67 ++++++++++++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 36 deletions(-) (limited to 'resume/resume.js') diff --git a/resume/resume.js b/resume/resume.js index 4f66f24..500b49c 100644 --- a/resume/resume.js +++ b/resume/resume.js @@ -1,46 +1,41 @@ (document => { 'use strict'; - const mangleurls = () => { + 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://alxu.ca/analytics?url=' + href.replace(/[%&;]/g, s=>'%'+s.charCodeAt(0).toString(16)); + else el.href = 'https://www.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(); - } + }); + 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); -- cgit v1.2.3-54-g00ecf