pushilka 2.0.3 → 2.0.4
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.esm.js +1 -0
- package/dist/app.js +1 -1
- package/gulpfile.js +28 -7
- package/package.json +11 -2
package/dist/app.esm.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
class e{sessionId=null;visitorId=null;params=null;constructor(e){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(){}};for(let t in e)if(e.hasOwnProperty(t)&&"dialog"===t)for(let 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){let t=(e+"=".repeat((4-e.length%4)%4)).replace(/-/g,"+").replace(/_/g,"/"),r=window.atob(t),n=new Uint8Array(r.length);for(let e=0;e<r.length;++e)n[e]=r.charCodeAt(e);return n}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){let n=new Date;n.setTime(n.getTime()+60*r*1e3),r="; expires="+n.toUTCString(),document.cookie=e+"="+(t||"")+r+"; path=/"}getCookie(e){let t=document.cookie.split(";"),r=e+"=";for(let e in t){let n=t[e].trimStart();if(0===n.indexOf(r))return n.substring(r.length)}return null}showDialog(){let e=this;if(null!==this.getCookie("pushilka-dialog"))return void this.sendEvent("push_blocked");this.sendEvent("push_invoked");let t=this.params.dialog,r=document.createElement("link");r.rel="stylesheet",r.href=t.style,document.body.appendChild(r);let n=document.createElement("div");n.innerHTML=t.template.replace("{ICON_URL}",t.icon).replace("{MESSAGE}",t.message).replace("{ALLOW_TEXT}",t.allowText).replace("{CANCEL_TEXT}",t.cancelText),document.body.appendChild(n);let a={agreeButton:document.getElementById("pushilka-agree-button"),cancelButton:document.getElementById("pushilka-cancel-button"),dialog:document.getElementById("pushilka-dialog")};a.agreeButton.addEventListener("click",function(t){t.preventDefault(),a.dialog.remove(),e.subscribe()}),a.cancelButton.addEventListener("click",function(t){t.preventDefault(),e.sendEvent("push_blocked"),e.setCookie("pushilka-dialog","1",e.params.dialog.ttl),a.dialog.remove()})}subscribe(){let e=this;navigator.serviceWorker.ready.then(function(t){return e.sendEvent("sys_push_invoked"),t.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:e.urlBase64ToUint8Array(e.params.applicationServerKey)})}).then(function(t){return e.sendSubscriptionToServer(t,"POST")}).catch(function(){e.sendEvent("sys_push_blocked"),e.params.decline(),e.params.done()})}genRandomString(){let e=[];for(let t=0;t<2;t++)e.push(Math.floor(4294967295*Math.random()).toString(36));return e.join("-")}getVisitorId(){let 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,n=e.getKey("p256dh"),a=e.getKey("auth"),s=(PushManager.supportedContentEncodings||["aesgcm"])[0];return fetch(r.getEndpoint(r.params.endpoint),{method:t,body:JSON.stringify({endpoint:e.endpoint,publicKey:n?btoa(String.fromCharCode.apply(null,new Uint8Array(n))):null,authToken:a?btoa(String.fromCharCode.apply(null,new Uint8Array(a))):null,contentEncoding:s,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 e=this;e.ready(function(){return"showNotification"in ServiceWorkerRegistration.prototype?"denied"===Notification.permission?(console.debug("User has blocked notifications."),e.sendEvent("sys_push_blocked"),e.params.decline(),void e.params.done()):void navigator.serviceWorker.register(e.params.serviceWorker,e.params.serviceWorkerOptions).then(function(){navigator.serviceWorker.ready.then(function(e){return e.pushManager.getSubscription()}).then(function(t){return t?e.sendSubscriptionToServer(t,"PUT"):e.params.useDialog?e.showDialog():e.subscribe()})}).catch(function(){e.sendEvent("sys_push_subscribe_error"),e.params.decline(),e.params.done()}):(console.debug("Push messaging is not supported."),e.sendEvent("push_not_supported"),e.params.decline(),void e.params.done())})}}export{e as default};
|
package/dist/app.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
var Pushilka=function(){"use strict";return class{sessionId=null;visitorId=null;params=null;constructor(e){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(){}};for(let t in e)if(e.hasOwnProperty(t)&&"dialog"===t)for(let 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){let t=(e+"=".repeat((4-e.length%4)%4)).replace(/-/g,"+").replace(/_/g,"/"),r=window.atob(t),n=new Uint8Array(r.length);for(let e=0;e<r.length;++e)n[e]=r.charCodeAt(e);return n}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){let n=new Date;n.setTime(n.getTime()+60*r*1e3),r="; expires="+n.toUTCString(),document.cookie=e+"="+(t||"")+r+"; path=/"}getCookie(e){let t=document.cookie.split(";"),r=e+"=";for(let e in t){let n=t[e].trimStart();if(0===n.indexOf(r))return n.substring(r.length)}return null}showDialog(){let e=this;if(null!==this.getCookie("pushilka-dialog"))return void this.sendEvent("push_blocked");this.sendEvent("push_invoked");let t=this.params.dialog,r=document.createElement("link");r.rel="stylesheet",r.href=t.style,document.body.appendChild(r);let n=document.createElement("div");n.innerHTML=t.template.replace("{ICON_URL}",t.icon).replace("{MESSAGE}",t.message).replace("{ALLOW_TEXT}",t.allowText).replace("{CANCEL_TEXT}",t.cancelText),document.body.appendChild(n);let s={agreeButton:document.getElementById("pushilka-agree-button"),cancelButton:document.getElementById("pushilka-cancel-button"),dialog:document.getElementById("pushilka-dialog")};s.agreeButton.addEventListener("click",function(t){t.preventDefault(),s.dialog.remove(),e.subscribe()}),s.cancelButton.addEventListener("click",function(t){t.preventDefault(),e.sendEvent("push_blocked"),e.setCookie("pushilka-dialog","1",e.params.dialog.ttl),s.dialog.remove()})}subscribe(){let e=this;navigator.serviceWorker.ready.then(function(t){return e.sendEvent("sys_push_invoked"),t.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:e.urlBase64ToUint8Array(e.params.applicationServerKey)})}).then(function(t){return e.sendSubscriptionToServer(t,"POST")}).catch(function(){e.sendEvent("sys_push_blocked"),e.params.decline(),e.params.done()})}genRandomString(){let e=[];for(let t=0;t<2;t++)e.push(Math.floor(4294967295*Math.random()).toString(36));return e.join("-")}getVisitorId(){let 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,n=e.getKey("p256dh"),s=e.getKey("auth"),a=(PushManager.supportedContentEncodings||["aesgcm"])[0];return fetch(r.getEndpoint(r.params.endpoint),{method:t,body:JSON.stringify({endpoint:e.endpoint,publicKey:n?btoa(String.fromCharCode.apply(null,new Uint8Array(n))):null,authToken:s?btoa(String.fromCharCode.apply(null,new Uint8Array(s))):null,contentEncoding:a,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 e=this;e.ready(function(){return"showNotification"in ServiceWorkerRegistration.prototype?"denied"===Notification.permission?(console.debug("User has blocked notifications."),e.sendEvent("sys_push_blocked"),e.params.decline(),void e.params.done()):void navigator.serviceWorker.register(e.params.serviceWorker,e.params.serviceWorkerOptions).then(function(){navigator.serviceWorker.ready.then(function(e){return e.pushManager.getSubscription()}).then(function(t){return t?e.sendSubscriptionToServer(t,"PUT"):e.params.useDialog?e.showDialog():e.subscribe()})}).catch(function(){e.sendEvent("sys_push_subscribe_error"),e.params.decline(),e.params.done()}):(console.debug("Push messaging is not supported."),e.sendEvent("push_not_supported"),e.params.decline(),void e.params.done())})}}}();
|
package/gulpfile.js
CHANGED
|
@@ -1,9 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
const gulp = require('gulp');
|
|
2
|
+
const uglify = require('gulp-uglify');
|
|
3
|
+
const uglifycss = require('gulp-uglifycss');
|
|
4
|
+
const { rollup } = require('rollup');
|
|
5
|
+
const terser = require('@rollup/plugin-terser');
|
|
4
6
|
|
|
5
|
-
function
|
|
6
|
-
|
|
7
|
+
async function appBundles() {
|
|
8
|
+
const bundle = await rollup({
|
|
9
|
+
input: 'src/app.js',
|
|
10
|
+
plugins: [terser()],
|
|
11
|
+
});
|
|
12
|
+
await Promise.all([
|
|
13
|
+
bundle.write({
|
|
14
|
+
file: 'dist/app.js',
|
|
15
|
+
format: 'iife',
|
|
16
|
+
name: 'Pushilka',
|
|
17
|
+
}),
|
|
18
|
+
bundle.write({
|
|
19
|
+
file: 'dist/app.esm.js',
|
|
20
|
+
format: 'es',
|
|
21
|
+
}),
|
|
22
|
+
]);
|
|
23
|
+
await bundle.close();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function otherScripts() {
|
|
27
|
+
return gulp.src(['src/*.js', '!src/app.js'])
|
|
7
28
|
.pipe(uglify())
|
|
8
29
|
.pipe(gulp.dest('dist/'));
|
|
9
30
|
}
|
|
@@ -11,7 +32,7 @@ function scripts() {
|
|
|
11
32
|
function css() {
|
|
12
33
|
return gulp.src('src/*.css')
|
|
13
34
|
.pipe(uglifycss())
|
|
14
|
-
.pipe(gulp.dest('dist/'))
|
|
35
|
+
.pipe(gulp.dest('dist/'));
|
|
15
36
|
}
|
|
16
37
|
|
|
17
38
|
function images() {
|
|
@@ -19,4 +40,4 @@ function images() {
|
|
|
19
40
|
.pipe(gulp.dest('dist/'));
|
|
20
41
|
}
|
|
21
42
|
|
|
22
|
-
exports.default = gulp.series(
|
|
43
|
+
exports.default = gulp.series(appBundles, otherScripts, css, images);
|
package/package.json
CHANGED
|
@@ -1,12 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pushilka",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
4
4
|
"description": "Web push client",
|
|
5
5
|
"main": "./dist/app.js",
|
|
6
|
+
"module": "./dist/app.esm.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/app.esm.js",
|
|
10
|
+
"default": "./dist/app.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
6
13
|
"devDependencies": {
|
|
14
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
7
15
|
"gulp": "^5.0.0",
|
|
8
16
|
"gulp-uglify": "^3.0.2",
|
|
9
|
-
"gulp-uglifycss": "^1.1.0"
|
|
17
|
+
"gulp-uglifycss": "^1.1.0",
|
|
18
|
+
"rollup": "^4.0.0"
|
|
10
19
|
},
|
|
11
20
|
"scripts": {
|
|
12
21
|
"test": "echo \"Error: no test specified\" && exit 1"
|