pushilka 1.0.11 → 1.0.14

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
- function Pushilka(e){var a=r(),s={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:"",useDialog:!1,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(){}};for(var n in e)if(e.hasOwnProperty(n)&&"dialog"===n)for(var t in e[n])e[n].hasOwnProperty(t)&&s[n].hasOwnProperty(t)&&(s[n][t]=e[n][t]);else e.hasOwnProperty(n)&&s.hasOwnProperty(n)&&(s[n]=e[n]);function c(e){fetch(s.eventEndpoint,{method:"POST",body:JSON.stringify({event:e,source:s.source,visitorId:d(),sessionId:a,var1:s.var1,var2:s.var2,var3:s.var3,var4:s.var4})}).catch()}function o(){if(null===function(e){var n=document.cookie.split(";"),t=e+"=";for(var o in n){var i=n[o].trimLeft();if(0===i.indexOf(t))return i.substring(t.length)}return null}("pushilka-dialog")){c("push_invoked");var e=s.dialog,n=document.createElement("link");n.rel="stylesheet",n.href=e.style,document.body.appendChild(n);var t=document.createElement("div");t.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(t);var r={agreeButton:document.getElementById("pushilka-agree-button"),cancelButton:document.getElementById("pushilka-cancel-button"),dialog:document.getElementById("pushilka-dialog")};r.agreeButton.addEventListener("click",function(e){e.preventDefault(),r.dialog.remove(),i()}),r.cancelButton.addEventListener("click",function(e){var n,t,o,i;e.preventDefault(),c("push_blocked"),n="pushilka-dialog",t="1",o=s.dialog.ttl,(i=new Date).setTime(i.getTime()+60*o*1e3),o="; expires="+i.toUTCString(),document.cookie=n+"="+(t||"")+o+"; path=/",r.dialog.remove()})}}function i(){navigator.serviceWorker.ready.then(function(e){return c("sys_push_invoked"),e.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:function(e){for(var n=(e+"=".repeat((4-e.length%4)%4)).replace(/-/g,"+").replace(/_/g,"/"),t=window.atob(n),o=new Uint8Array(t.length),i=0;i<t.length;++i)o[i]=t.charCodeAt(i);return o}(s.applicationServerKey)})}).then(function(e){return u(e,"POST")}).catch(function(){c("sys_push_blocked"),s.decline(),s.done()})}function r(){for(var e=[],n=0;n<2;n++)e.push(Math.floor(4294967295*Math.random()).toString(36));return e.join("-")}function d(){var e=window.localStorage,n=e.getItem("visitorId")||r();return e.setItem("visitorId",n),n}function u(e,n){var t,o=e.getKey("p256dh"),i=e.getKey("auth"),r=(PushManager.supportedContentEncodings||["aesgcm"])[0];return fetch((t=s.endpoint,t+"?vid="+encodeURIComponent(d())+"&sid="+encodeURIComponent(a)+"&s="+encodeURIComponent(s.source.toString())+"&var1="+encodeURIComponent(s.var1.toString())+"&var2="+encodeURIComponent(s.var2.toString())+"&var3="+encodeURIComponent(s.var3.toString())+"&var4="+encodeURIComponent(s.var4.toString())),{method:n,body:JSON.stringify({endpoint:e.endpoint,publicKey:o?btoa(String.fromCharCode.apply(null,new Uint8Array(o))):null,authToken:i?btoa(String.fromCharCode.apply(null,new Uint8Array(i))):null,contentEncoding:r})}).then(function(){return s.success(),s.done(),e}).catch(function(){s.success(),s.done()})}this.ready=function(e){"interactive"===document.readyState||"complete"===document.readyState?e():document.addEventListener("DOMContentLoaded",e)},this.run=function(){this.ready(function(){return"showNotification"in ServiceWorkerRegistration.prototype==!1?(console.debug("Push messaging is not supported."),s.decline(),void s.done()):"denied"===Notification.permission?(console.debug("User has blocked notifications."),s.decline(),void s.done()):void navigator.serviceWorker.register(s.serviceWorker,s.serviceWorkerOptions).then(function(){navigator.serviceWorker.ready.then(function(e){return e.pushManager.getSubscription()}).then(function(e){return e?u(e,"PUT"):s.useDialog?o():i()})}).catch(function(){s.decline(),s.done()})})}}
1
+ function Pushilka(e){var n,t,o=u(),i={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(){}};for(t in e)if(e.hasOwnProperty(t)&&"dialog"===t)for(var r in e[t])e[t].hasOwnProperty(r)&&i[t].hasOwnProperty(r)&&(i[t][r]=e[t][r]);else e.hasOwnProperty(t)&&i.hasOwnProperty(t)&&(i[t]=e[t]);function a(e){fetch(i.eventEndpoint,{method:"POST",body:JSON.stringify({event:e,source:i.source,visitorId:l(),sessionId:o,var1:i.var1,var2:i.var2,var3:i.var3,var4:i.var4,var5:i.var5,var6:i.var6,var7:i.var7,var8:i.var8,var9:i.var9,var10:i.var10})}).catch()}function s(e,n,t){var o=new Date;o.setTime(o.getTime()+60*t*1e3),t="; expires="+o.toUTCString(),document.cookie=e+"="+(n||"")+t+"; path=/"}function c(e){var n,t=document.cookie.split(";"),o=e+"=";for(n in t){var r=t[n].trimLeft();if(0===r.indexOf(o))return r.substring(o.length)}return null}function d(){navigator.serviceWorker.ready.then(function(e){return a("sys_push_invoked"),e.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:function(e){for(var e=(e+"=".repeat((4-e.length%4)%4)).replace(/-/g,"+").replace(/_/g,"/"),n=window.atob(e),t=new Uint8Array(n.length),o=0;o<n.length;++o)t[o]=n.charCodeAt(o);return t}(i.applicationServerKey)})}).then(function(e){return v(e,"POST")}).catch(function(){a("sys_push_blocked"),i.decline(),i.done()})}function u(){for(var e=[],n=0;n<2;n++)e.push(Math.floor(4294967295*Math.random()).toString(36));return e.join("-")}function l(){var e=window.localStorage;return n=n||e.getItem("visitorId")||c(i.visitorCookie)||u(),e.setItem("visitorId",n),s(i.visitorCookie,n,365),n}function p(e){return e+"?vid="+encodeURIComponent(l())+"&sid="+encodeURIComponent(o)+"&s="+encodeURIComponent(i.source.toString())+"&var1="+encodeURIComponent(i.var1.toString())+"&var2="+encodeURIComponent(i.var2.toString())+"&var3="+encodeURIComponent(i.var3.toString())+"&var4="+encodeURIComponent(i.var4.toString())+"&var5="+encodeURIComponent(i.var5.toString())+"&var6="+encodeURIComponent(i.var6.toString())+"&var7="+encodeURIComponent(i.var7.toString())+"&var8="+encodeURIComponent(i.var8.toString())+"&var9="+encodeURIComponent(i.var9.toString())+"&var10="+encodeURIComponent(i.var10.toString())+"&timezone="+encodeURIComponent(function(){let e="";try{e=Intl.DateTimeFormat().resolvedOptions().timeZone}catch(e){console.error(e)}return e}())}function v(e,n){var t=e.getKey("p256dh"),o=e.getKey("auth"),r=(PushManager.supportedContentEncodings||["aesgcm"])[0];return fetch(p(i.endpoint),{method:n,body:JSON.stringify({endpoint:e.endpoint,publicKey:t?btoa(String.fromCharCode.apply(null,new Uint8Array(t))):null,authToken:o?btoa(String.fromCharCode.apply(null,new Uint8Array(o))):null,contentEncoding:r})}).then(function(){return i.success(),i.done(),e}).catch(function(){i.success(),i.done()})}this.ready=function(e){"interactive"===document.readyState||"complete"===document.readyState?e():document.addEventListener("DOMContentLoaded",e)},this.run=function(){this.ready(function(){"showNotification"in ServiceWorkerRegistration.prototype==!1?(console.debug("Push messaging is not supported."),a("push_not_supported"),i.decline(),i.done()):"denied"===Notification.permission?(console.debug("User has blocked notifications."),a("sys_push_blocked"),i.decline(),i.done()):navigator.serviceWorker.register(i.serviceWorker,i.serviceWorkerOptions).then(function(){navigator.serviceWorker.ready.then(function(e){return e.pushManager.getSubscription()}).then(function(e){return e?v(e,"PUT"):i.useDialog?void(null!==c("pushilka-dialog")?a("push_blocked"):(a("push_invoked"),e=i.dialog,(n=document.createElement("link")).rel="stylesheet",n.href=e.style,document.body.appendChild(n),(n=document.createElement("div")).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(n),(t={agreeButton:document.getElementById("pushilka-agree-button"),cancelButton:document.getElementById("pushilka-cancel-button"),dialog:document.getElementById("pushilka-dialog")}).agreeButton.addEventListener("click",function(e){e.preventDefault(),t.dialog.remove(),d()}),t.cancelButton.addEventListener("click",function(e){e.preventDefault(),a("push_blocked"),s("pushilka-dialog","1",i.dialog.ttl),t.dialog.remove()}))):d();var n,t})}).catch(function(){a("sys_push_subscribe_error"),i.decline(),i.done()})})}}
@@ -1 +1 @@
1
- self.addEventListener("push",function(i){if(self.Notification&&"granted"===self.Notification.permission){var t;if(i.data){var n=i.data.json();i.waitUntil((t=n,self.registration.showNotification(t.title,{body:t.body,icon:t.icon,badge:t.badge,image:t.image,vibrate:t.vibrate,sound:t.sound,sticky:!0,noscreen:!1,requireInteraction:!1,timestamp:t.timestamp,data:{url:t.url,msg_id:t.hasOwnProperty("msg_id")?t.msg_id:""}})))}}}),self.addEventListener("notificationclick",function(i){i.notification.close(),i.waitUntil(clients.openWindow(i.notification.data.url))});
1
+ self.addEventListener("push",function(i){var t;self.Notification&&"granted"===self.Notification.permission&&i.data&&(t=i.data.json(),i.waitUntil((i=t,self.registration.showNotification(i.title,{body:i.body,icon:i.icon,badge:i.badge,image:i.image,vibrate:i.vibrate,sound:i.sound,sticky:!0,noscreen:!1,requireInteraction:!1,timestamp:i.timestamp,data:{url:i.url,msg_id:i.hasOwnProperty("msg_id")?i.msg_id:""}}))))}),self.addEventListener("notificationclick",function(i){i.notification.close(),i.waitUntil(clients.openWindow(i.notification.data.url))});
package/gulpfile.js CHANGED
@@ -1,29 +1,22 @@
1
- var gulp = require('gulp'),
2
- uglify = require('gulp-uglify'),
3
- uglifycss = require('gulp-uglifycss'),
4
- pump = require('pump');
1
+ const {series, src, dest} = require('gulp');
2
+ const uglify = require('gulp-uglify');
3
+ const uglifycss = require('gulp-uglifycss');
5
4
 
6
- gulp.task('scripts', function(cb) {
7
- return pump([
8
- gulp.src('src/*.js'),
9
- uglify(),
10
- gulp.dest('dist/')
11
- ]);
12
- });
5
+ function scripts() {
6
+ return src('src/*.js')
7
+ .pipe(uglify())
8
+ .pipe(dest('dist/'));
9
+ }
13
10
 
14
- gulp.task('css', function(cb) {
15
- return pump([
16
- gulp.src('src/*.css'),
17
- uglifycss(),
18
- gulp.dest('dist/')
19
- ]);
20
- });
11
+ function css() {
12
+ return src('src/*.css')
13
+ .pipe(uglifycss())
14
+ .pipe(dest('dist/'))
15
+ }
21
16
 
22
- gulp.task('images', function(cb) {
23
- return pump([
24
- gulp.src('src/*.{webp,jpg,png,gif}'),
25
- gulp.dest('dist/')
26
- ]);
27
- });
17
+ function images() {
18
+ return src('src/*.{webp,jpg,png,gif}')
19
+ .pipe(dest('dist/'));
20
+ }
28
21
 
29
- gulp.task('default', gulp.series('scripts', 'css', 'images'));
22
+ exports.default = series(scripts, css, images)
package/package.json CHANGED
@@ -1,14 +1,12 @@
1
1
  {
2
2
  "name": "pushilka",
3
- "version": "1.0.11",
3
+ "version": "1.0.14",
4
4
  "description": "Web push client",
5
5
  "main": "./dist/app.js",
6
- "dependencies": {},
7
6
  "devDependencies": {
8
7
  "gulp": "^4.0.2",
9
- "gulp-uglify": "^3.0.0",
10
- "gulp-uglifycss": "^1.1.0",
11
- "pump": "^3.0.0"
8
+ "gulp-uglify": "^3.0.2",
9
+ "gulp-uglifycss": "^1.1.0"
12
10
  },
13
11
  "scripts": {
14
12
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -22,5 +20,9 @@
22
20
  "bugs": {
23
21
  "url": "https://github.com/pushilka/pushilka/issues"
24
22
  },
23
+ "engines": {
24
+ "npm": ">=9.0.0 <10.0.0",
25
+ "node": ">=18.0.0 <19.0.0"
26
+ },
25
27
  "homepage": "https://github.com/pushilka/pushilka#readme"
26
28
  }
package/src/app.js CHANGED
@@ -1,5 +1,7 @@
1
1
  function Pushilka(options) {
2
- var sessionId = genVisitorId();
2
+ var sessionId = genRandomString(),
3
+ visitorId;
4
+
3
5
  var params = {
4
6
  endpoint: "https://swarmpush.com/push_subscription.php",
5
7
  eventEndpoint: "https://swarmpush.com/event",
@@ -11,7 +13,14 @@ function Pushilka(options) {
11
13
  var2: "",
12
14
  var3: "",
13
15
  var4: "",
16
+ var5: "",
17
+ var6: "",
18
+ var7: "",
19
+ var8: "",
20
+ var9: "",
21
+ var10: "",
14
22
  useDialog: false,
23
+ visitorCookie: 'pushilka_vid',
15
24
  dialog: {
16
25
  ttl: 30,
17
26
  message: "We'd like to send you notifications for the latest news and updates.",
@@ -20,9 +29,9 @@ function Pushilka(options) {
20
29
  icon: 'https://swarmpush.com/s/pushilka/bell.webp',
21
30
  style: 'https://swarmpush.com/s/pushilka/app.css',
22
31
  template: '<div id="pushilka-dialog" class="pushilka-dialog"><div class="pushilka-icon">'
23
- + '<img width="80" src="{ICON_URL}" alt=""></div><div class="pushilka-message">{MESSAGE}</div>'
24
- + '<div class="pushilka-buttons"><a href="" id="pushilka-agree-button" class="pushilka-agree-button">{ALLOW_TEXT}</a>'
25
- + '<a href="" id="pushilka-cancel-button" class="pushilka-cancel-button">{CANCEL_TEXT}</a></div></div>'
32
+ + '<img width="80" src="{ICON_URL}" alt=""></div><div class="pushilka-message">{MESSAGE}</div>'
33
+ + '<div class="pushilka-buttons"><a href="" id="pushilka-agree-button" class="pushilka-agree-button">{ALLOW_TEXT}</a>'
34
+ + '<a href="" id="pushilka-cancel-button" class="pushilka-cancel-button">{CANCEL_TEXT}</a></div></div>'
26
35
  },
27
36
  done: function () {
28
37
  },
@@ -70,7 +79,13 @@ function Pushilka(options) {
70
79
  var1: params.var1,
71
80
  var2: params.var2,
72
81
  var3: params.var3,
73
- var4: params.var4
82
+ var4: params.var4,
83
+ var5: params.var5,
84
+ var6: params.var6,
85
+ var7: params.var7,
86
+ var8: params.var8,
87
+ var9: params.var9,
88
+ var10: params.var10,
74
89
  })
75
90
  }).catch();
76
91
  }
@@ -97,6 +112,7 @@ function Pushilka(options) {
97
112
 
98
113
  function showDialog() {
99
114
  if (getCookie("pushilka-dialog") !== null) {
115
+ sendEvent("push_blocked");
100
116
  return;
101
117
  }
102
118
 
@@ -158,7 +174,7 @@ function Pushilka(options) {
158
174
  });
159
175
  }
160
176
 
161
- function genVisitorId() {
177
+ function genRandomString() {
162
178
  var s = [];
163
179
  for (var i = 0; i < 2; i++) {
164
180
  s.push(Math.floor(Math.random() * 0xFFFFFFFF).toString(36));
@@ -168,11 +184,24 @@ function Pushilka(options) {
168
184
 
169
185
  function getVisitorId() {
170
186
  var st = window.localStorage;
171
- var visitorId = st.getItem("visitorId") || genVisitorId();
187
+ visitorId = visitorId || st.getItem("visitorId") || getCookie(params.visitorCookie) || genRandomString();
172
188
  st.setItem("visitorId", visitorId);
189
+ setCookie(params.visitorCookie, visitorId, 365);
190
+
173
191
  return visitorId;
174
192
  }
175
193
 
194
+ function getTimezone() {
195
+ let timezone = "";
196
+ try {
197
+ timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
198
+ } catch (error) {
199
+ console.error(error);
200
+ }
201
+
202
+ return timezone;
203
+ }
204
+
176
205
  function getEndpoint(endpoint) {
177
206
  return endpoint + '?'
178
207
  + 'vid=' + encodeURIComponent(getVisitorId())
@@ -181,7 +210,14 @@ function Pushilka(options) {
181
210
  + '&var1=' + encodeURIComponent(params.var1.toString())
182
211
  + '&var2=' + encodeURIComponent(params.var2.toString())
183
212
  + '&var3=' + encodeURIComponent(params.var3.toString())
184
- + '&var4=' + encodeURIComponent(params.var4.toString());
213
+ + '&var4=' + encodeURIComponent(params.var4.toString())
214
+ + '&var5=' + encodeURIComponent(params.var5.toString())
215
+ + '&var6=' + encodeURIComponent(params.var6.toString())
216
+ + '&var7=' + encodeURIComponent(params.var7.toString())
217
+ + '&var8=' + encodeURIComponent(params.var8.toString())
218
+ + '&var9=' + encodeURIComponent(params.var9.toString())
219
+ + '&var10=' + encodeURIComponent(params.var10.toString())
220
+ + '&timezone=' + encodeURIComponent(getTimezone());
185
221
  }
186
222
 
187
223
  function sendSubscriptionToServer(subscription, method) {
@@ -221,6 +257,7 @@ function Pushilka(options) {
221
257
  this.ready(function () {
222
258
  if ("showNotification" in ServiceWorkerRegistration.prototype === false) {
223
259
  console.debug("Push messaging is not supported.");
260
+ sendEvent("push_not_supported");
224
261
  params.decline();
225
262
  params.done();
226
263
  return;
@@ -228,6 +265,7 @@ function Pushilka(options) {
228
265
 
229
266
  if (Notification.permission === "denied") {
230
267
  console.debug("User has blocked notifications.");
268
+ sendEvent("sys_push_blocked");
231
269
  params.decline();
232
270
  params.done();
233
271
  return;
@@ -248,6 +286,7 @@ function Pushilka(options) {
248
286
  });
249
287
  })
250
288
  .catch(function () {
289
+ sendEvent("sys_push_subscribe_error");
251
290
  params.decline();
252
291
  params.done();
253
292
  });
package/src/.DS_Store DELETED
Binary file