UNPKG

@zywave/zywave-api-toolkit-bundle

Version:

2 lines (1 loc) 3.63 kB
const t="zapi:bf";function e(t,e,i,n){const r=new Date;r.setTime(r.getTime()+24*i*60*60*1e3),n=!n||n.startsWith(".")?n:`.${n}`;let a=`${t}=${e}; expires=${r.toUTCString()};path=/;Secure;SameSite=Strict`;n&&window.location.hostname.endsWith(n)&&(a+=`; domain=${n}`),document.cookie=a}function i(t,e,i){const n=Date.now()+24*i*60*60*1e3,r=JSON.stringify({value:e,expires:n});localStorage.setItem(t,r)}function n(t){return window.matchMedia(`(${t})`).matches}const r=class{static get instance(){return this.#t??=new this}static#t;#e;#i;#n=1;constructor(){}async getAttributes(t){return this.#r(t.storageTtlDays,t.cookieDomain),await this.#a(),{canvas:`v${this.#n}|${this.#e}`,storage:this.#i,screenWidth:window.screen.width,screenHeight:window.screen.height,timeZone:Intl.DateTimeFormat().resolvedOptions().timeZone,prefersReducedMotion:n("prefers-reduced-motion: reduce"),prefersColorScheme:n("prefers-color-scheme: dark")?"dark":"light",gpc:navigator.globalPrivacyControl??null,doNotTrack:navigator.doNotTrack??null}}#r(n,r){if(this.#i)return;let a=function(t,i,n){const r=`; ${document.cookie}`.split(`; ${t}=`),a=2===r.length?r.pop()?.split(";").shift():null;return a&&e(t,a,i,n),a}(t,n,r);a||(a=function(t,e){const n=localStorage.getItem(t);if(n){const r=JSON.parse(n);return r.expires<Date.now()?(localStorage.removeItem(t),null):(i(t,r.value,e),r.value)}return null}(t,n),a&&e(t,a,n,r)),a||(a=function(){if(crypto?.randomUUID)return crypto.randomUUID();const t=new Uint32Array(4);return crypto.getRandomValues(t),"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(e){const i=(t[0]+16*Math.random())%16|0;return t[0]=Math.floor(t[0]/16),("x"===e?i:3&i|8).toString(16)}))}(),i(t,a,n),e(t,a,n,r)),this.#i=a}async#a(){if(!this.#e)try{const t=document.createElement("canvas"),e=t.getContext("2d");if(!e)return;const i="ZYWAVE";e.textBaseline="top",e.textBaseline="top",e.font="14px 'Arial'",e.textBaseline="alphabetic",e.fillStyle="#f60",e.fillRect(125,1,62,20),e.fillStyle="#069",e.fillText(i,2,15),e.fillStyle="rgba(102, 204, 0, 0.7)",e.fillText(i,4,17);const n=t.toDataURL().split(",")[1];this.#e=await async function(t){const e=(new TextEncoder).encode(t),i=await crypto.subtle.digest("SHA-256",e);return[...new Uint8Array(i)].map((t=>t.toString(16).padStart(2,"0"))).join("")}(n)}catch(t){}}}.instance,a=!!window.navigation;const s=class{static get instance(){return this.#t??=new this}static#t;constructor(){this.#s=[],this.#o=!1,this.isImpersonated=!1}#s;#o;#c;#g;async connect(t){this.isImpersonated=t.isImpersonated,this.#c||(this.#c=await r.getAttributes({cookieDomain:t.cookieDomain,storageTtlDays:365})),this.#o||(this.#s.push(this.#h()),this.#d()),this.#o=!0}disconnect(){this.#s=[],this.#o=!1,this.#g&&(a?window.navigation.removeEventListener("currententrychange",this.#g):(window.removeEventListener("zapiPushState",this.#g),window.removeEventListener("zapiReplaceState",this.#g)))}async retrieveActivity(){return new Promise((t=>{const e=[...this.#s];this.#s=[],t(e)}))}#d(){a?(this.#g=this.#u.bind(this),window.navigation.addEventListener("currententrychange",this.#g)):(window.addEventListener("zapiPushState",this.#u.bind(this)),window.addEventListener("zapiReplaceState",this.#u.bind(this)))}#u(){this.#s.push(this.#h())}#h(){return{type:"Navigation",location:window.location.href,eventDateTime:new Date,isImpersonated:this.isImpersonated,bf:this.#c?.storage,sw:this.#c?.screenWidth,sh:this.#c?.screenHeight,cf:this.#c?.canvas,tz:this.#c?.timeZone,dnt:this.#c?.doNotTrack,gpc:this.#c?.gpc,prm:this.#c?.prefersReducedMotion,pcs:this.#c?.prefersColorScheme}}}.instance;export{s as ActivityTracker};