summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Xu (Hello71) <alex_y_xu@yahoo.ca>2021-10-05 15:31:08 -0400
committerAlex Xu (Hello71) <alex_y_xu@yahoo.ca>2021-10-05 15:31:08 -0400
commita93d44efbc5ee7e78523c544341325b71b347f2a (patch)
tree02b3ca71921480b47324751c01eece323b52b2fc
parent7dccf6e8684f767b6e377d627b36ccf493947f1e (diff)
downloadalxu.ca-a93d44efbc5ee7e78523c544341325b71b347f2a.tar.xz
alxu.ca-a93d44efbc5ee7e78523c544341325b71b347f2a.zip
enable analytics for alxu.ca only, fix chrome CLS
-rw-r--r--resume/resume.js86
1 files 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'));