Skip to content

Instantly share code, notes, and snippets.

@regalstreak
Created April 8, 2024 21:19
Show Gist options
  • Save regalstreak/ceccb7513fe014fc8b39133408a6aad7 to your computer and use it in GitHub Desktop.
Save regalstreak/ceccb7513fe014fc8b39133408a6aad7 to your computer and use it in GitHub Desktop.
"use strict";(()=>{var ve=Object.defineProperty,Te=Object.defineProperties;var Ie=Object.getOwnPropertyDescriptors;var Y=Object.getOwnPropertySymbols;var fe=Object.prototype.hasOwnProperty,Se=Object.prototype.propertyIsEnumerable;var F=(e,t,n)=>t in e?ve(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,I=(e,t)=>{for(var n in t||(t={}))fe.call(t,n)&&F(e,n,t[n]);if(Y)for(var n of Y(t))Se.call(t,n)&&F(e,n,t[n]);return e},y=(e,t)=>Te(e,Ie(t));var d=(e,t)=>()=>(e&&(t=e(e=0)),t);var he=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports);var G=(e,t,n)=>new Promise((r,o)=>{var a=c=>{try{s(n.next(c))}catch(m){o(m)}},i=c=>{try{s(n.throw(c))}catch(m){o(m)}},s=c=>c.done?r(c.value):Promise.resolve(c.value).then(a,i);s((n=n.apply(e,t)).next())});function k(e){let t=new Blob([e],{type:"text/javascript"}),n=URL.createObjectURL(t),r=new Worker(n);return URL.revokeObjectURL(n),r}var j=d(()=>{});function C(){return k(`var E="http://sentinel-backend-prod.eba-xx7psrft.eu-central-1.elasticbeanstalk.com/api/events",b="http://sentinel-backend-prod.eba-xx7psrft.eu-central-1.elasticbeanstalk.com/api/event",D="lytixDb";var T=async(l=D)=>{function i(){return new Promise((r,e)=>{let o=indexedDB.open(l);o.onupgradeneeded=t=>{t.target.result.createObjectStore("events",{keyPath:"_idbKey",autoIncrement:!0})},o.onsuccess=t=>{let n=t.target.result;r(n)},o.onerror=t=>{e(t.type)}})}let s=await i();function v(r){return new Promise((e,o)=>{let t=s.transaction("events","readwrite").objectStore("events").add(r);t.onsuccess=function(n){e()},t.onerror=function(n){o()}})}async function u(r){return new Promise(o=>{let t=r.map(c=>c._idbKey),n=IDBKeyRange.bound(t[0],t[t.length-1]),a=s.transaction(["events"],"readwrite").objectStore("events");a.openCursor(n).onsuccess=function(c){let f=c.target.result;f?(f.delete(),f.continue()):o()}})}async function g(){return new Promise((e,o)=>{let d=s.transaction(["events"]).objectStore("events").getAll();d.onsuccess=function(a){let c=a.target.result;e(c)},d.onerror=function(a){o("[IDB]: Failed to get all values from indexeddb")}})}return{database:s,addToDb:v,removeFromDb:u,getAllEventsFromDb:g}},m=T;(async()=>{self.onmessage=e=>{u(e.data)},postMessage("hello from worker!");let{database:l,addToDb:i,getAllEventsFromDb:s,removeFromDb:v}=await m();async function u(e){try{await p(e)}catch(o){await i(e),r()}}let r=(()=>{let e=null,o=()=>{e||(e=setInterval(d,5e3))},t=()=>{clearInterval(e),e=null},n=!1;async function d(){if(n)return;let a=await s();if(!a.length){t();return}n=!0;try{await p(a)&&(v(a),t())}catch(c){}finally{n=!1}}return o})();r()})();async function p(l){let i=Array.isArray(l)?E:b,s=await fetch(i,{method:"POST",body:JSON.stringify(l),headers:{"Content-Type":"application/json"}});if(!s.ok)throw new Error(s.statusText);return s.ok}
`)}var X=d(()=>{"use strict";j()});function U(){if(!x&&(x=typeof crypto!="undefined"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!x))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return x(ye)}var x,ye,J=d(()=>{ye=new Uint8Array(16)});function $(e,t=0){return l[e[t+0]]+l[e[t+1]]+l[e[t+2]]+l[e[t+3]]+"-"+l[e[t+4]]+l[e[t+5]]+"-"+l[e[t+6]]+l[e[t+7]]+"-"+l[e[t+8]]+l[e[t+9]]+"-"+l[e[t+10]]+l[e[t+11]]+l[e[t+12]]+l[e[t+13]]+l[e[t+14]]+l[e[t+15]]}var l,z=d(()=>{l=[];for(let e=0;e<256;++e)l.push((e+256).toString(16).slice(1))});var xe,M,q=d(()=>{xe=typeof crypto!="undefined"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),M={randomUUID:xe}});function be(e,t,n){if(M.randomUUID&&!t&&!e)return M.randomUUID();e=e||{};let r=e.random||(e.rng||U)();if(r[6]=r[6]&15|64,r[8]=r[8]&63|128,t){n=n||0;for(let o=0;o<16;++o)t[n+o]=r[o];return t}return $(r)}var E,Z=d(()=>{q();J();z();E=be});var R=d(()=>{Z()});var g,f=d(()=>{"use strict";g=(u=>(u.BUTTON="button",u.ANCHOR="a",u.DIV="div",u.HEADING="heading",u.IMAGE="img",u.INPUT="input",u.TEXTAREA="textarea",u.SPAN="span",u.LI="li",u.TR="tr",u.radio="radio",u))(g||{})});var b,Q,ee,te,ne,P,re,L,v=d(()=>{"use strict";b=()=>{let e=navigator.userAgent;return/(tablet|ipad|playbook|silk)|(android(?!.*mobi))/i.test(e)?"tablet":/Mobile|Android|iP(hone|od)|IEMobile|BlackBerry|Kindle|Silk-Accelerated|(hpw|web)OS|Opera M(obi|ini)/.test(e)?"mobile":"desktop"},Q=()=>{let e=navigator.userAgent;return/Windows/.test(e)?"Windows":/Macintosh|Mac/.test(e)?"MacOS":/Linux/.test(e)?"Linux":/Android/.test(e)?"Android":/iOS|iPad|iPhone|iPod/.test(e)?"iOS":"Unknown"},ee=()=>{let e=navigator.userAgent;return/Firefox/.test(e)?"Firefox":/OPR|Opera/.test(e)?"Opera":/Edg|Edge/.test(e)?"Microsoft Edge":/Chrome/.test(e)&&!/Edg|Edge/.test(e)?"Google Chrome":/Safari/.test(e)&&!/Chrome/.test(e)?"Safari":/Trident|MSIE/.test(e)?"Internet Explorer":"Unknown"},te=()=>navigator.language||"Unknown",ne=()=>{try{return Intl.DateTimeFormat().resolvedOptions().timeZone||"Unknown"}catch(e){return"Unknown"}},P=e=>{let t=document.documentElement.scrollTop/(document.body.scrollHeight-document.documentElement.clientHeight);return e?Number(t.toFixed(e)):t},re=(e,t=300)=>{let n;return(...r)=>{clearTimeout(n),n=setTimeout(()=>{e.apply(r)},t)}},L=e=>{let t=[];for(;e&&e.nodeType==Node.ELEMENT_NODE;e=e.parentNode){let o=0;for(let s=e.previousSibling;s;s=s.previousSibling)s.nodeName==e.nodeName&&++o;let a=e.tagName.toLowerCase(),i=o>0?`[${o+1}]`:"";t.splice(0,0,`${a}${i}`)}let r=`/${t.join("/")}`.replace(/\/svg/g,'/*[name()="svg"]');return r=r.replace(/\/path/g,'/*[name()="path"]'),r=r.replace(/\/circle/g,'/*[name()="circle"]'),r=r.replace(/\/g/g,'/*[name()="g"]'),r=r.replace(/\[(\d+)\]/g,"[$1]"),r=r.replace(/\[(@\w+="[^"]*")\]/g,"[$1]"),r}});var B,T,oe,w,O=d(()=>{"use strict";B="_lxuid",T="_lxsid",oe="sentinel-token",w="LYTIX_ORG_ID"});var se,ie,Oe,_e,_,N=d(()=>{"use strict";R();O();v();f();se=()=>{let e=localStorage.getItem(B);if(e)return e;let t=E();return localStorage.setItem(B,t),t},ie=()=>{let e=Oe(),{id:t,expiry:n,timeoutId:r}=e;if(!t||!n||Date.now()>n){let s=_e();return localStorage.setItem(T,JSON.stringify(s)),s.id}clearTimeout(r);let o=n-Date.now(),a=setTimeout(()=>{localStorage.removeItem(T)},o),i={id:t,expiry:n,timeoutId:a};return localStorage.setItem(T,JSON.stringify(i)),t},Oe=()=>{let e=localStorage.getItem(T);return JSON.parse(e!=null?e:"null")||{id:null,expiry:null,timeoutId:null}},_e=()=>{let e=E(),n=b()==="mobile"?3e5:18e5,r=new Date().getTime()+n,o=setTimeout(()=>{localStorage.removeItem(T)},n);return{id:e,expiry:r,timeoutId:o}},_=({parent:e,localName:t})=>{let n=e.getElementsByTagName(t);return Array.from(n).filter(r=>t==="a"?!0:r.onclick!==null)}});var D,ae,W,ce,le,A=d(()=>{"use strict";R();f();v();N();O();D=()=>{let e=window[w];return{id:E(),userId:se(),sessionId:ie(),timestamp:Date.now(),deviceType:b(),operatingSystem:Q(),browser:ee(),deviceLanguage:te(),timezone:ne(),screenWidth:window.innerWidth,domainName:window.location.hostname,orgId:e}},ae=()=>I({eventType:"SessionStart"},D()),W=()=>{let e=window.location.href,t=window.location.pathname,n=document.title,r=document.referrer;return y(I({},D()),{eventType:"PageView",pageURL:e,pagePath:t,pageTitle:n,referrer:r})},ce=({elementType:e,nativeEvent:t,xPath:n})=>{var c,m,h,u,V,K;let r,o,a,i=t.currentTarget.id,s=i||null;switch(e){case"button":r=(c=t.currentTarget.innerText)!=null?c:null;break;case"a":r=(m=t.currentTarget.innerText)!=null?m:null;break;case"heading":r=(h=t.currentTarget.innerText)!=null?h:null;break;case"img":{let p=t.currentTarget;o=(u=p.src)!=null?u:null,a=(V=p.alt)!=null?V:null}break;case"input":break;default:{r=(K=t.currentTarget.innerText)!=null?K:null;break}}return y(I({},D()),{eventType:"Click",eventSource:e,pathName:window.location.pathname,imageSourceUrl:o,imageAltText:a,sourceText:r,xPath:n,htmlId:s})},le=()=>{var s;let e=window.location.href,t=window.location.pathname,n=document.title,r=Number(document.documentElement.scrollTop.toFixed(2)),o=document.documentElement.scrollHeight,a=document.documentElement.clientHeight,i=isNaN(P(2))?0:(s=P(2))!=null?s:0;return y(I({},D()),{eventType:"Scroll",pageURL:e,pagePath:t,pageTitle:n,scrollTop:r,scrollHeight:o,clientHeight:a,scrollPercenage:i})}});var ue,de=d(()=>{"use strict";f();A();N();v();ue=e=>{let t={},n=()=>{Object.values(g).forEach(i=>{t[i]=_({parent:document,localName:i}).map(s=>{let c=m=>{let h=L(m.currentTarget);r(m,i,h)};return{attach:()=>s.addEventListener("click",c),remove:()=>s.removeEventListener("click",c)}})})},r=(i,s,c)=>{if(i.LYTIX_IGNORE_BUBBLED_EVENT)return;let m=ce({elementType:s,nativeEvent:i,xPath:c});e.postMessage(m),i.LYTIX_IGNORE_BUBBLED_EVENT=!0},o=()=>{a(),n(),Object.values(t).forEach(i=>{i.forEach(s=>{s.attach()})})},a=()=>{Object.values(t).forEach(i=>{i.forEach(s=>{s.remove()})})};return{attachListeners:o,removeListeners:a,handleClick:r}}});var me,ge=d(()=>{"use strict";A();v();me=(e,t=500)=>{let n=re(()=>{e.postMessage(le())},t);return{attachListeners:()=>{window.addEventListener("scroll",n)},removeListeners:()=>{window.removeEventListener("scroll",n)}}}});var Me=he(Ee=>{X();A();f();de();ge();N();v();O();var S=C(),{attachListeners:pe,handleClick:Ne}=ue(S),{attachListeners:De}=me(S),Ae=()=>{S.postMessage(ae()),S.postMessage(W()),De(),pe()},ke=new URLSearchParams(window.location.search).get("lx-labelling-mode")==="true"||localStorage.getItem("lx-labelling-mode")==="true",Ce=()=>G(Ee,null,function*(){try{let t=yield(yield fetch("http://localhost:5555/dev/label")).text(),n=document.createElement("script");n.textContent=t,document.body.appendChild(n)}catch(e){}}),Ue=()=>{let e=location.href,t=new MutationObserver(r=>{if(location.href!==e){e=location.href,S.postMessage(W()),pe();return}for(let o of r)o.type=="childList"&&(o.addedNodes.forEach(a=>n(a)),o.removedNodes.forEach(a=>n(a,!0)))}),n=(r,o=!1)=>{r.nodeType!==1||["script"].includes(r.nodeName)||Object.values(g).forEach(a=>{_({parent:r,localName:a}).forEach(i=>{let s=c=>{let m=L(c.target);Ne(c,a,m)};o?i.removeEventListener("click",s):(i.removeEventListener("click",s),i.addEventListener("click",s))})})};t.observe(document,{subtree:!0,childList:!0})},H=()=>{var n;let e=document.querySelector(`[data-${oe}]`),t=(n=e==null?void 0:e.dataset)==null?void 0:n.sentinelToken;if(!e)return null;if(window[w]=t,ke){Ce();return}Ae(),Ue()};document.readyState!=="loading"?H():document.addEventListener("DOMContentLoaded",H);window.addEventListener("message",e=>{e.data==="LYTIX_INJECTOR"&&H()})});Me();})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment