pushilka 2.0.1 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/app.js CHANGED
@@ -1 +1 @@
1
- export default class Pushilka{sessionId=null;visitorId=null;params=null;constructor(e){for(var t in this.sessionId=this.genRandomString(),this.params={endpoint:"https://swarmpush.com/push_subscription.php",eventEndpoint:"https://swarmpush.com/event",serviceWorker:"/serviceWorker.js",serviceWorkerOptions:{scope:"/"},applicationServerKey:"BCmIwkLHxJNPccoVf2UXDjd7kDuiyJpKsSOqSHCtGBMfBkjfHLCq4-d8eNtabNlCNKFF8CZIzeDwOo3OvNCQAns",source:"",var1:"",var2:"",var3:"",var4:"",var5:"",var6:"",var7:"",var8:"",var9:"",var10:"",useDialog:!1,visitorCookie:"pushilka_vid",dialog:{ttl:30,message:"We'd like to send you notifications for the latest news and updates.",allowText:"Allow",cancelText:"No thanks",icon:"https://swarmpush.com/s/pushilka/bell.webp",style:"https://swarmpush.com/s/pushilka/app.css",template:'<div id="pushilka-dialog" class="pushilka-dialog"><div class="pushilka-icon"><img width="80" src="{ICON_URL}" alt=""></div><div class="pushilka-message">{MESSAGE}</div><div class="pushilka-buttons"><a href="" id="pushilka-agree-button" class="pushilka-agree-button">{ALLOW_TEXT}</a><a href="" id="pushilka-cancel-button" class="pushilka-cancel-button">{CANCEL_TEXT}</a></div></div>'},done:function(){},success:function(){},decline:function(){}},e)if(e.hasOwnProperty(t)&&"dialog"===t)for(var r in e[t])e[t].hasOwnProperty(r)&&this.params[t].hasOwnProperty(r)&&(this.params[t][r]=e[t][r]);else e.hasOwnProperty(t)&&this.params.hasOwnProperty(t)&&(this.params[t]=e[t])}urlBase64ToUint8Array(e){var e=(e+"=".repeat((4-e.length%4)%4)).replace(/-/g,"+").replace(/_/g,"/"),t=window.atob(e),r=new Uint8Array(t.length);for(let e=0;e<t.length;++e)r[e]=t.charCodeAt(e);return r}sendEvent(e){fetch(this.params.eventEndpoint,{method:"POST",body:JSON.stringify({event:e,source:this.params.source,visitorId:this.getVisitorId(),sessionId:this.sessionId,var1:this.params.var1,var2:this.params.var2,var3:this.params.var3,var4:this.params.var4,var5:this.params.var5,var6:this.params.var6,var7:this.params.var7,var8:this.params.var8,var9:this.params.var9,var10:this.params.var10})}).catch()}setCookie(e,t,r){var s=new Date;s.setTime(s.getTime()+60*r*1e3),r="; expires="+s.toUTCString(),document.cookie=e+"="+(t||"")+r+"; path=/"}getCookie(e){var t,r=document.cookie.split(";"),s=e+"=";for(t in r){var a=r[t].trimStart();if(0===a.indexOf(s))return a.substring(s.length)}return null}showDialog(){let r=this;if(null!==this.getCookie("pushilka-dialog"))this.sendEvent("push_blocked");else{this.sendEvent("push_invoked");var e=this.params.dialog,s=document.createElement("link"),s=(s.rel="stylesheet",s.href=e.style,document.body.appendChild(s),document.createElement("div"));s.innerHTML=e.template.replace("{ICON_URL}",e.icon).replace("{MESSAGE}",e.message).replace("{ALLOW_TEXT}",e.allowText).replace("{CANCEL_TEXT}",e.cancelText),document.body.appendChild(s);let t={agreeButton:document.getElementById("pushilka-agree-button"),cancelButton:document.getElementById("pushilka-cancel-button"),dialog:document.getElementById("pushilka-dialog")};t.agreeButton.addEventListener("click",function(e){e.preventDefault(),t.dialog.remove(),r.subscribe()}),t.cancelButton.addEventListener("click",function(e){e.preventDefault(),r.sendEvent("push_blocked"),r.setCookie("pushilka-dialog","1",r.params.dialog.ttl),t.dialog.remove()})}}subscribe(){let t=this;navigator.serviceWorker.ready.then(function(e){return t.sendEvent("sys_push_invoked"),e.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:t.urlBase64ToUint8Array(t.params.applicationServerKey)})}).then(function(e){return t.sendSubscriptionToServer(e,"POST")}).catch(function(){t.sendEvent("sys_push_blocked"),t.params.decline(),t.params.done()})}genRandomString(){var t=[];for(let e=0;e<2;e++)t.push(Math.floor(4294967295*Math.random()).toString(36));return t.join("-")}getVisitorId(){var e=window.localStorage;return this.visitorId=this.visitorId||e.getItem("visitorId")||this.getCookie(this.params.visitorCookie)||this.genRandomString(),e.setItem("visitorId",this.visitorId),this.setCookie(this.params.visitorCookie,this.visitorId,365),this.visitorId}getTimezone(){let e="";try{e=Intl.DateTimeFormat().resolvedOptions().timeZone}catch(e){console.error(e)}return e}getEndpoint(e){return e+"?vid="+encodeURIComponent(this.getVisitorId())+"&sid="+encodeURIComponent(this.sessionId)+"&s="+encodeURIComponent(this.params.source.toString())+"&var1="+encodeURIComponent(this.params.var1.toString())+"&var2="+encodeURIComponent(this.params.var2.toString())+"&var3="+encodeURIComponent(this.params.var3.toString())+"&var4="+encodeURIComponent(this.params.var4.toString())+"&var5="+encodeURIComponent(this.params.var5.toString())+"&var6="+encodeURIComponent(this.params.var6.toString())+"&var7="+encodeURIComponent(this.params.var7.toString())+"&var8="+encodeURIComponent(this.params.var8.toString())+"&var9="+encodeURIComponent(this.params.var9.toString())+"&var10="+encodeURIComponent(this.params.var10.toString())+"&timezone="+encodeURIComponent(this.getTimezone())}sendSubscriptionToServer(e,t){let r=this;var s=e.getKey("p256dh"),a=e.getKey("auth"),n=(PushManager.supportedContentEncodings||["aesgcm"])[0];return fetch(r.getEndpoint(r.params.endpoint),{method:t,body:JSON.stringify({endpoint:e.endpoint,publicKey:s?btoa(String.fromCharCode.apply(null,new Uint8Array(s))):null,authToken:a?btoa(String.fromCharCode.apply(null,new Uint8Array(a))):null,contentEncoding:n})}).then(function(){return r.params.success(),r.params.done(),e}).catch(function(){r.params.success(),r.params.done()})}ready(e){"interactive"===document.readyState||"complete"===document.readyState?e():document.addEventListener("DOMContentLoaded",e)}run(){let t=this;t.ready(function(){"showNotification"in ServiceWorkerRegistration.prototype?"denied"===Notification.permission?(console.debug("User has blocked notifications."),t.sendEvent("sys_push_blocked"),t.params.decline(),t.params.done()):navigator.serviceWorker.register(t.params.serviceWorker,t.params.serviceWorkerOptions).then(function(){navigator.serviceWorker.ready.then(function(e){return e.pushManager.getSubscription()}).then(function(e){return e?t.sendSubscriptionToServer(e,"PUT"):t.params.useDialog?t.showDialog():t.subscribe()})}).catch(function(){t.sendEvent("sys_push_subscribe_error"),t.params.decline(),t.params.done()}):(console.debug("Push messaging is not supported."),t.sendEvent("push_not_supported"),t.params.decline(),t.params.done())})}}
1
+ export default class Pushilka{sessionId=null;visitorId=null;params=null;constructor(e){for(var t in this.sessionId=this.genRandomString(),this.params={endpoint:"https://swarmpush.com/push_subscription.php",eventEndpoint:"https://swarmpush.com/event",serviceWorker:"/serviceWorker.js",serviceWorkerOptions:{scope:"/"},applicationServerKey:"BCmIwkLHxJNPccoVf2UXDjd7kDuiyJpKsSOqSHCtGBMfBkjfHLCq4-d8eNtabNlCNKFF8CZIzeDwOo3OvNCQAns",source:"",var1:"",var2:"",var3:"",var4:"",var5:"",var6:"",var7:"",var8:"",var9:"",var10:"",externalId:"",useDialog:!1,visitorCookie:"pushilka_vid",dialog:{ttl:30,message:"We'd like to send you notifications for the latest news and updates.",allowText:"Allow",cancelText:"No thanks",icon:"https://swarmpush.com/s/pushilka/bell.webp",style:"https://swarmpush.com/s/pushilka/app.css",template:'<div id="pushilka-dialog" class="pushilka-dialog"><div class="pushilka-icon"><img width="80" src="{ICON_URL}" alt=""></div><div class="pushilka-message">{MESSAGE}</div><div class="pushilka-buttons"><a href="" id="pushilka-agree-button" class="pushilka-agree-button">{ALLOW_TEXT}</a><a href="" id="pushilka-cancel-button" class="pushilka-cancel-button">{CANCEL_TEXT}</a></div></div>'},done:function(){},success:function(){},decline:function(){}},e)if(e.hasOwnProperty(t)&&"dialog"===t)for(var r in e[t])e[t].hasOwnProperty(r)&&this.params[t].hasOwnProperty(r)&&(this.params[t][r]=e[t][r]);else e.hasOwnProperty(t)&&this.params.hasOwnProperty(t)&&(this.params[t]=e[t])}urlBase64ToUint8Array(e){var e=(e+"=".repeat((4-e.length%4)%4)).replace(/-/g,"+").replace(/_/g,"/"),t=window.atob(e),r=new Uint8Array(t.length);for(let e=0;e<t.length;++e)r[e]=t.charCodeAt(e);return r}sendEvent(e){fetch(this.params.eventEndpoint,{method:"POST",body:JSON.stringify({event:e,source:this.params.source,visitorId:this.getVisitorId(),sessionId:this.sessionId,var1:this.params.var1,var2:this.params.var2,var3:this.params.var3,var4:this.params.var4,var5:this.params.var5,var6:this.params.var6,var7:this.params.var7,var8:this.params.var8,var9:this.params.var9,var10:this.params.var10,externalId:this.params.externalId})}).catch()}setCookie(e,t,r){var a=new Date;a.setTime(a.getTime()+60*r*1e3),r="; expires="+a.toUTCString(),document.cookie=e+"="+(t||"")+r+"; path=/"}getCookie(e){var t,r=document.cookie.split(";"),a=e+"=";for(t in r){var s=r[t].trimStart();if(0===s.indexOf(a))return s.substring(a.length)}return null}showDialog(){let r=this;if(null!==this.getCookie("pushilka-dialog"))this.sendEvent("push_blocked");else{this.sendEvent("push_invoked");var e=this.params.dialog,a=document.createElement("link"),a=(a.rel="stylesheet",a.href=e.style,document.body.appendChild(a),document.createElement("div"));a.innerHTML=e.template.replace("{ICON_URL}",e.icon).replace("{MESSAGE}",e.message).replace("{ALLOW_TEXT}",e.allowText).replace("{CANCEL_TEXT}",e.cancelText),document.body.appendChild(a);let t={agreeButton:document.getElementById("pushilka-agree-button"),cancelButton:document.getElementById("pushilka-cancel-button"),dialog:document.getElementById("pushilka-dialog")};t.agreeButton.addEventListener("click",function(e){e.preventDefault(),t.dialog.remove(),r.subscribe()}),t.cancelButton.addEventListener("click",function(e){e.preventDefault(),r.sendEvent("push_blocked"),r.setCookie("pushilka-dialog","1",r.params.dialog.ttl),t.dialog.remove()})}}subscribe(){let t=this;navigator.serviceWorker.ready.then(function(e){return t.sendEvent("sys_push_invoked"),e.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:t.urlBase64ToUint8Array(t.params.applicationServerKey)})}).then(function(e){return t.sendSubscriptionToServer(e,"POST")}).catch(function(){t.sendEvent("sys_push_blocked"),t.params.decline(),t.params.done()})}genRandomString(){var t=[];for(let e=0;e<2;e++)t.push(Math.floor(4294967295*Math.random()).toString(36));return t.join("-")}getVisitorId(){var e=window.localStorage;return this.visitorId=this.visitorId||e.getItem("visitorId")||this.getCookie(this.params.visitorCookie)||this.genRandomString(),e.setItem("visitorId",this.visitorId),this.setCookie(this.params.visitorCookie,this.visitorId,365),this.visitorId}getTimezone(){let e="";try{e=Intl.DateTimeFormat().resolvedOptions().timeZone}catch(e){console.error(e)}return e}getEndpoint(e){return e+"?vid="+encodeURIComponent(this.getVisitorId())+"&sid="+encodeURIComponent(this.sessionId)+"&s="+encodeURIComponent(this.params.source.toString())+"&var1="+encodeURIComponent(this.params.var1.toString())+"&var2="+encodeURIComponent(this.params.var2.toString())+"&var3="+encodeURIComponent(this.params.var3.toString())+"&var4="+encodeURIComponent(this.params.var4.toString())+"&var5="+encodeURIComponent(this.params.var5.toString())+"&var6="+encodeURIComponent(this.params.var6.toString())+"&var7="+encodeURIComponent(this.params.var7.toString())+"&var8="+encodeURIComponent(this.params.var8.toString())+"&var9="+encodeURIComponent(this.params.var9.toString())+"&var10="+encodeURIComponent(this.params.var10.toString())+"&externalId="+encodeURIComponent(this.params.externalId.toString())+"&timezone="+encodeURIComponent(this.getTimezone())}sendSubscriptionToServer(e,t){let r=this;var a=e.getKey("p256dh"),s=e.getKey("auth"),n=(PushManager.supportedContentEncodings||["aesgcm"])[0];return fetch(r.getEndpoint(r.params.endpoint),{method:t,body:JSON.stringify({endpoint:e.endpoint,publicKey:a?btoa(String.fromCharCode.apply(null,new Uint8Array(a))):null,authToken:s?btoa(String.fromCharCode.apply(null,new Uint8Array(s))):null,contentEncoding:n,applicationServerKey:r.params.applicationServerKey})}).then(function(){return r.params.success(),r.params.done(),e}).catch(function(){r.params.success(),r.params.done()})}ready(e){"interactive"===document.readyState||"complete"===document.readyState?e():document.addEventListener("DOMContentLoaded",e)}run(){let t=this;t.ready(function(){"showNotification"in ServiceWorkerRegistration.prototype?"denied"===Notification.permission?(console.debug("User has blocked notifications."),t.sendEvent("sys_push_blocked"),t.params.decline(),t.params.done()):navigator.serviceWorker.register(t.params.serviceWorker,t.params.serviceWorkerOptions).then(function(){navigator.serviceWorker.ready.then(function(e){return e.pushManager.getSubscription()}).then(function(e){return e?t.sendSubscriptionToServer(e,"PUT"):t.params.useDialog?t.showDialog():t.subscribe()})}).catch(function(){t.sendEvent("sys_push_subscribe_error"),t.params.decline(),t.params.done()}):(console.debug("Push messaging is not supported."),t.sendEvent("push_not_supported"),t.params.decline(),t.params.done())})}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pushilka",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "Web push client",
5
5
  "main": "./dist/app.js",
6
6
  "devDependencies": {
package/src/app.js CHANGED
@@ -22,6 +22,7 @@ export default class Pushilka {
22
22
  var8: "",
23
23
  var9: "",
24
24
  var10: "",
25
+ externalId: "",
25
26
  useDialog: false,
26
27
  visitorCookie: 'pushilka_vid',
27
28
  dialog: {
@@ -90,6 +91,7 @@ export default class Pushilka {
90
91
  var8: this.params.var8,
91
92
  var9: this.params.var9,
92
93
  var10: this.params.var10,
94
+ externalId: this.params.externalId,
93
95
  })
94
96
  }).catch();
95
97
  }
@@ -224,6 +226,7 @@ export default class Pushilka {
224
226
  + '&var8=' + encodeURIComponent(this.params.var8.toString())
225
227
  + '&var9=' + encodeURIComponent(this.params.var9.toString())
226
228
  + '&var10=' + encodeURIComponent(this.params.var10.toString())
229
+ + '&externalId=' + encodeURIComponent(this.params.externalId.toString())
227
230
  + '&timezone=' + encodeURIComponent(this.getTimezone());
228
231
  }
229
232
 
@@ -239,7 +242,8 @@ export default class Pushilka {
239
242
  endpoint: subscription.endpoint,
240
243
  publicKey: key ? btoa(String.fromCharCode.apply(null, new Uint8Array(key))) : null,
241
244
  authToken: token ? btoa(String.fromCharCode.apply(null, new Uint8Array(token))) : null,
242
- contentEncoding: contentEncoding
245
+ contentEncoding: contentEncoding,
246
+ applicationServerKey: self.params.applicationServerKey
243
247
  })
244
248
  })
245
249
  .then(function () {