102 lines
3.8 KiB
JavaScript
102 lines
3.8 KiB
JavaScript
import(`https://storage.googleapis.com/workbox-cdn/releases/6.1.5/workbox-window.prod.mjs`).then(workboxWindow => {
|
|
if ('serviceWorker' in navigator) {
|
|
window.addEventListener('load', function () {
|
|
|
|
const wb = new workboxWindow.Workbox('/service-worker.js');
|
|
let registration;
|
|
|
|
const showSkipWaitingPrompt = (event) => {
|
|
if (orbit.intl.t('serviceWorker.newUpdate') != undefined) {
|
|
Swal.fire({
|
|
position: 'bottom-end',
|
|
backdrop: false,
|
|
title: "",
|
|
text: orbit.intl.t('serviceWorker.newUpdate') + ". " + orbit.intl.t('serviceWorker.wantToUpdate'),
|
|
imageUrl: orbit.client.logo || 'https://cdn.startinblox.com/logos/webp/startinblox.webp',
|
|
imageAlt: orbit.client.name,
|
|
showCancelButton: true,
|
|
confirmButtonClass: 'button text-xsmall text-bold text-center reversed color-secondary bordered icon icon-check icon-margin-right-xsmall no-background-image',
|
|
cancelButtonClass: 'button text-xsmall text-bold text-center reversed color-primary bordered icon icon-exclamation icon-margin-right-xsmall no-background-image',
|
|
confirmButtonText: orbit.intl.t('serviceWorker.yes'),
|
|
cancelButtonText: orbit.intl.t('serviceWorker.no')
|
|
}).then((result) => {
|
|
if (result.isConfirmed) {
|
|
wb.addEventListener('controlling', (event) => {
|
|
window.location.reload();
|
|
});
|
|
|
|
if (registration && registration.waiting) {
|
|
workboxWindow.messageSW(registration.waiting, {
|
|
type: 'SKIP_WAITING'
|
|
});
|
|
}
|
|
}
|
|
});
|
|
}
|
|
};
|
|
|
|
wb.addEventListener('waiting', showSkipWaitingPrompt);
|
|
wb.addEventListener('externalwaiting', showSkipWaitingPrompt);
|
|
|
|
wb.register().then((r) => registration = r);
|
|
|
|
function urlB64ToUint8Array(base64String) {
|
|
const padding = '='.repeat((4 - base64String.length % 4) % 4);
|
|
const base64 = (base64String + padding)
|
|
.replace(/\-/g, '+')
|
|
.replace(/_/g, '/');
|
|
|
|
const rawData = window.atob(base64);
|
|
const outputArray = new Uint8Array(rawData.length);
|
|
const outputData = outputArray.map((output, index) => rawData.charCodeAt(index));
|
|
|
|
return outputData;
|
|
}
|
|
|
|
const subscribe = async (reg) => {
|
|
const subscription = await reg.pushManager.getSubscription();
|
|
if (subscription) {
|
|
sendSubData(subscription);
|
|
return;
|
|
}
|
|
|
|
const options = {
|
|
userVisibleOnly: true,
|
|
// if key exists, create applicationServerKey property
|
|
...(orbit.client.vapid_key && {applicationServerKey: urlB64ToUint8Array(orbit.client.vapid_key)})
|
|
};
|
|
|
|
const sub = await reg.pushManager.subscribe(options);
|
|
sendSubData(sub)
|
|
};
|
|
const sendSubData = async (subscription) => {
|
|
const browser = navigator.userAgent.match(/(firefox|msie|chrome|safari|trident)/ig)[0].toLowerCase();
|
|
const data = {
|
|
status_type: 'subscribe',
|
|
subscription: subscription.toJSON(),
|
|
browser: browser,
|
|
};
|
|
|
|
const res = await fetch(orbit.client.server + "/webpush/save_information", {
|
|
method: 'POST',
|
|
body: JSON.stringify(data),
|
|
headers: {
|
|
'content-type': 'application/json'
|
|
},
|
|
credentials: "include"
|
|
});
|
|
|
|
handleResponse(res);
|
|
};
|
|
|
|
const handleResponse = (res) => {
|
|
console.log(res.status);
|
|
};
|
|
|
|
navigator.serviceWorker.getRegistration().then(function(reg) {
|
|
subscribe(reg);
|
|
});
|
|
});
|
|
}
|
|
});
|