UNPKG

followanalytics-alpha

Version:

FollowAnalytics Web SDK - NPM package for the FollowAnalytics Web SDK.

1 lines 4.06 kB
!function(e){function n(r){if(t[r])return t[r].exports;var i=t[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}var t={};n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:r})},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},n.p="",n(n.s=0)}([function(e,n,t){function r(){return self.registration.pushManager.getSubscription().then(function(e){if(null===e)return o()})}function i(){return self.registration.pushManager.getSubscription().then(function(e){return c(e,h.Unsubscribe),null!==e&&e.unsubscribe()})}function o(){a().then(function(e){var n=p(e.public_key),t={applicationServerKey:n,userVisibleOnly:!0};return self.registration.pushManager.subscribe(t).then(function(e){return c(e,h.Subscribe)})})}function a(){return l().then(function(){var e={FAID:d().FAID,bundleId:d().bundleId,deviceId:d().deviceId,sdkPlatform:"Web"};return s(f("webpush_key"),e)})}function c(e,n){var t=u(e,n);return s(f("notification"),t)}function u(e,n){var t=e?e.endpoint:null,r=e?e.getKey("p256dh"):null,i=e?e.getKey("auth"):null,o=r?btoa(String.fromCharCode.apply(null,new Uint8Array(r))):null,a=i?btoa(String.fromCharCode.apply(null,new Uint8Array(i))):null;return{FAID:d().FAID,deviceId:d().deviceId,bundleId:d().bundleId,deviceToken:t,acceptBadge:Boolean(n),acceptSound:Boolean(n),acceptAlert:Boolean(n),sdkPlatform:"Web",publicKey:o,auth:a}}function s(e,n){return fetch(e,{method:"post",headers:{"Content-type":"application/json"},body:JSON.stringify(n)}).then(function(e){return 200!==e.status?void console.log("Fetch Service Error. Status Code: "+e.status):e.json().then(function(e){if(e.success)return e.result;console.log("Fetch Service Failure :: "+e.errorMessage)})}).catch(function(e){console.log("Fetch Service Resolve Error :: ",e)})}function l(){var e={FAID:d().FAID,sdkPlatform:"Web",sdkVersion:b};return s(f("deployment"),e).then(function(e){return v=e.service_domain})}function f(e){var n="deployment"===e?"auth":v;return d().envProtocol+"://"+n+(""===d().env?".":d().env+".")+d().envDomain+"/api/"+e}function d(){var e=self.location.search.substr(1),n=e.split("&"),t={};return n.forEach(function(e){var n=e.split("=");t[decodeURIComponent(n[0])]=decodeURIComponent(n[1])}),t}function p(e){for(var n="=".repeat((4-e.length%4)%4),t=(e+n).replace(/\-/g,"+").replace(/_/g,"/"),r=atob(t),i=new Uint8Array(r.length),o=0;o<r.length;++o)i[o]=r.charCodeAt(o);return i}var h,b="2.0.0-alpha";!function(e){e[e.Unsubscribe=0]="Unsubscribe",e[e.Subscribe=1]="Subscribe"}(h||(h={}));var v="";self.addEventListener("install",function(e){return e.waitUntil(self.skipWaiting())}),self.addEventListener("activate",function(e){return e.waitUntil(self.clients.claim())}),self.addEventListener("message",function(e){switch(e.data){case"registerForPushNotification":r();break;case"unregisterFromPushNotification":i()}}),self.addEventListener("push",function(e){if(self.Notification&&"granted"===self.Notification.permission){var n={};e.data&&(n=JSON.parse(e.data.text()));var t=n,r=t.title,i=t.body,o=t.image,a=t.action,c=t.data;if(c.hasOwnProperty("FA")&&c.FA.hasOwnProperty("id")){var u={action:a,id:c.FA.id},s=o&&o.trim().length>0?o:d().defaultIcon,l={action:a,body:i,icon:"null"===s?null:s,data:u},f=self.registration.showNotification(r,l);e.waitUntil(f)}}}),self.addEventListener("notificationclick",function(e){var n=e.notification.data,t=n.id,r=n.action;r=null===r?"":r;var i,o=r.includes("?")?"&":"?",a=""+r+o+"fa_cid="+t;try{i=new URL(a,self.location.origin).href}catch(e){i=new URL("fa_cid="+t,self.location.origin).href}e.notification.close();var c=self.clients.openWindow(i);e.waitUntil(c)}),self.addEventListener("pushsubscriptionchange",function(e){e.waitUntil(a().then(function(e){var n=p(e.public_key),t={applicationServerKey:n,userVisibleOnly:!0};return self.registration.pushManager.subscribe(t).then(function(e){return c(e,h.Subscribe)})}))})}]);