PushOwl Service Worker
function t(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function e(e){for(var r=1;r<arguments.length;r++){var i=null!=arguments[r]?arguments[r]:{};r%2?t(Object(i),!0).forEach((function(t){n(e,t,i[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(i)):t(Object(i)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(i,t))}))}return e}function n(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function r(t){return new Promise((function(e,n){t.oncomplete=t.onsuccess=function(){return e(t.result)},t.onabort=t.onerror=function(){return n(t.error)}}))}var i;function o(){return i||(i=function(t,e){var n=indexedDB.open(t);n.onupgradeneeded=function(){return n.result.createObjectStore(e)};var i=r(n);return function(t,n){return i.then((function(r){return n(r.transaction(e,t).objectStore(e))}))}}("keyval-store","keyval")),i}function a(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:o();return e("readonly",(function(e){return r(e.get(t))}))}function s(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:o();return n("readwrite",(function(n){return n.put(e,t),r(n.transaction)}))}self.PUSHOWL_SERVICE_WORKER_VERSION="2.4",new BroadcastChannel("pushowl").onmessage=t=>{var{subdomain:e,subscriberToken:n}=t.data;s("pushowl_subscriber_details",{subdomain:e,subscriberToken:n}).catch((t=>{u.logException(t)}))};var c,l,u=function(){var t=()=>{var t=navigator.userAgent.toLowerCase(),e=/(chrome|firefox|opera|msie|trident|edge(?=\/))\/?\s*(\d+)/i.exec(t)||[],[,n,r]=e;return{name:n,version:r}};function n(n){(n=n||{}).poServiceWorkerVersion=self.PUSHOWL_SERVICE_WORKER_VERSION;var r=(new Date).toISOString().split(".")[0];return{event_id:"xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx".replace(/[xy]/g,(function(t){var e=16*Math.random()|0;return("x"===t?e:3&e|8).toString(16)})),logger:"cdn-service-worker",platform:"javascript",timestamp:r,extra:n,release:"3e7b0e8",contexts:{browser:e(e({},t()),{},{type:"browser"})}}}function r(t){return fetch("https://sentry.io/api/1891871/store/?sentry_version=7&sentry_key=0df575aa94e3419782416c33a46d9dd7",{method:"post",mode:"no-cors",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)})}function i(t,e){(e=e||{}).errorDump={str:String(t),stack:t&&t.stack},e.userAgent=navigator.userAgent;var i=n(e);return i.exception=function(t){var e=(t.stack||"").split("\n").map((t=>t.trim())).filter((t=>t.startsWith("at"))).map((function(t){var e="",n=0,r=0,i="",o=t.split(/[ ]+/);if("at"===o[0].trim()&&o.length>1){var a="";o.length>2?(e=o[1],a=o[2]):a=o[1];var s=(a=a.replace("(","").replace(")","")).split(":");s.length>1&&(n=s[s.length-1],r=s[s.length-2],i=s.slice(0,s.length-2).join(":"))}return{in_app:!0,function:e,colno:Number(n)||n,lineno:Number(r)||r,filename:i}}));return e.reverse(),{values:[{type:t.name||"Error",value:t.message||String(t),stacktrace:{frames:e}}]}}(t),r(i)}return{log:function(t,e){var i=n(e);return i.message=t,r(i)},logException:i,withErrorReporting:function(t){return function(e){if(!e||!e.waitUntil)throw new Error("withErrorReporting should only be used for handlers that receive ExtendableEvent");try{return t(e)}catch(t){var n=i(t,{eventData:e&&e.data&&e.data.json()});e.waitUntil(n)}}}}}(),f=function(t){return new Promise(((t,e)=>{t()}))};c=console,l=new class{constructor(t){var{maxNetworkRetries:e}=t;this.maxNetworkRetries=e}payloadTransformation(t){var e=t.title,n=[],r=t.actions;if(r)for(var i=0;i<r.length;i++){var o={action:"action"+i,title:r[i].title};n.push(o)}var a=!("require_interaction"in t)||t.require_interaction,s={body:t.description||"",tag:t.tag||t.id,actions:n,requireInteraction:a,data:t};return["icon","badge","image"].forEach((e=>{t[e]&&(s[e]=t[e])})),{title:e,config:s}}processPush(t){if(this.payload=t.data.json().data,!this.payload||"pushowl"!==this.payload.app)return Promise.resolve();var{title:e,config:n}=this.payloadTransformation(this.payload),r=this.displayNotification(e,n),i=this.payload.delivery_acknowledgement_url,o=this.update(i);return Promise.all([o,r])}displayNotification(t,e){return self.registration.showNotification(t,e)}update(t){var e=1;return navigator.connection&&navigator.connection.effectiveType,new Promise(((n,r)=>{var i=()=>{fetch(t,{method:"POST",headers:{"Content-Type":"text/plain"}}).then((t=>{var r=Math.min(8e3,2**e*1e3);200===t.status&&f(),(429==t.status||t.status>=500)&&e++<=this.maxNetworkRetries?t.text().then((e=>{f(0,t.status),setTimeout(i,r)})):n(t)})).catch((t=>{if(e++<=this.maxNetworkRetries)return f(0,t.message),void i();f(0,t.message),r(t)}))};i()}))}openLink(t){t&&clients.matchAll({type:"window"}).then((function(e){for(var n=0;n<e.length;n++){var r=e[n];if(r.url===t&&"focus"in r)return r.focus()}if(clients.openWindow)return Promise.all([clients.openWindow(t),f()])}))}static get version(){return self.PUSHOWL_SERVICE_WORKER_VERSION}}({maxNetworkRetries:5}),self.addEventListener("install",(function(t){t.waitUntil(f()),t.waitUntil(self.skipWaiting())})),self.addEventListener("activate",(function(t){t.waitUntil(Promise.all([f(),self.clients.claim()]))})),self.addEventListener("push",u.withErrorReporting((function(t){var e=[];if(e.push(f(0,t&&t.data&&t.data.json())),self.Notification&&"granted"===self.Notification.permission){if(!t.data)throw new Error("Empty event data.");var n=l.processPush(t);e.push(n),t.waitUntil(Promise.all(e))}}))),self.addEventListener("notificationclick",(function(t){t.waitUntil(f(0,t&&t.data&&t.data.json()));var e,n,r,i=t.notification,o=i.data;try{e=o.redirect_url,n=o.click_acknowledgement_url,r="body",t.action&&(t.action.includes("action0")?(e=o.actions[0].redirect_url,r="cta1"):t.action.includes("action1")&&(e=o.actions[1].redirect_url,r="cta2"))}catch(e){t.waitUntil(Promise.all([f(0,e.message),u.logException(e,i?{notification:{title:i.title,data:i.data,actions:i.actions}}:void 0)]))}var a=new Promise(((n,r)=>{t.notification.close(),Promise.all([f(),l.openLink(e)]).then(n)}));n+="&clicked_component="+r;var s=Promise.all([a,l.update(n)]);t.waitUntil(s)})),self.addEventListener("message",(function(t){var{title:e,config:n}=l.payloadTransformation(t.data);e&&t.waitUntil(l.displayNotification(e,n))})),self.addEventListener("notificationclose",(function(t){t.waitUntil(Promise.all([f(),new Promise(((e,n)=>{var r=t.notification.data.close_acknowledgement_url;l.update(r).then((()=>{e()})).catch((()=>{}))}))]))})),self.addEventListener("pushsubscriptionchange",(t=>{var e=Promise.all([self.registration.pushManager.getSubscription(),a("subscriber_refresh_endpoint"),a("pushowl_subscriber_details"),a("pushowl_sw_subscription"),f(0,JSON.stringify(t))]).then((t=>{var[e,n,r,i,o]=t;if(i&&JSON.parse(i).endpoint!==e.endpoint)return fetch(n,{method:"post",headers:{"Content-type":"application/json"},body:JSON.stringify({previous_token:r.subscriberToken,previous_subscription:i,current_subscription:e})}).then((t=>{s("pushowl_sw_subscriber_token",t.result.token),s("pushowl_sw_subscription",JSON.stringify(e))}))}));t.waitUntil(e)})),self.addEventListener("error",(function(t){c.error(t.filename,t.lineno,t.colno,t.message),t.waitUntil(Promise.all([f(),u.logException(t.error)]))}));