javascript harder

This commit is contained in:
Roberto Hidalgo 2023-10-02 19:30:56 -06:00
parent a8d18d8812
commit 2c276d8fcd

View File

@ -321,54 +321,70 @@ window.addEventListener("load", async function() {
pnb.style.display = "none" pnb.style.display = "none"
} }
pnb.addEventListener('click', handleSubscribeToggle)
})
async function handleSubscribeToggle(evt) {
const pnb = document.querySelector("#push-notifications")
evt.preventDefault()
let reg
try {
reg = await navigator.serviceWorker.getRegistration()
} catch(err) {
console.error("Could not get sw registration", err)
return
}
console.info("got registration")
pnb.addEventListener('click', async evt =>{
let sub let sub
try { try {
sub = await reg.pushManager.getSubscription() sub = await reg.pushManager.getSubscription()
} catch(err) { } catch(err) {
console.error("Could not get pushmanager subscription", err) console.error("Could not get pushmanager subscription", err)
return
} }
console.info("got subscription: ", sub)
if (!pnb.classList.contains("subscribed")) { if (!pnb.classList.contains("subscribed")) {
if (await createPushSubscription()) { console.info("creating subscription")
try {
await createPushSubscription(reg)
pnb.classList.add("subscribed") pnb.classList.add("subscribed")
pnb.innerHTML = "🔕" pnb.innerHTML = "🔕"
} catch (err) {
console.error("could not subscribe", err)
} }
} else { } else {
if (await deletePushSubscription()) { console.info("deleting subscription")
try {
await deletePushSubscription(reg, sub)
pnb.classList.remove("subscribed") pnb.classList.remove("subscribed")
pnb.innerHTML = "🔔" pnb.innerHTML = "🔔"
} catch(err) {
console.error("could not unsubscribe", err)
} }
} }
}
if (sub) {
pnb.classList.add("subscribed")
pnb.innerHTML = "🔕"
} else {
pnb.classList.remove("subscribed")
pnb.innerHTML = "🔔"
}
})
})
async function createPushSubscription() { async function createPushSubscription(registration) {
const registration = await navigator.serviceWorker.getRegistration() const result = await new Promise((res, rej) => Notification.requestPermission().then(res).catch(rej))
const result = await Notification.requestPermission();
if (result == "denied") { if (result == "denied") {
console.error("permission to subscribe denied")
return false return false
} }
console.info("allowed to subscribe")
const subscription = await registration.pushManager.subscribe({ const subscription = await registration.pushManager.subscribe({
userVisibleOnly: true, userVisibleOnly: true,
applicationServerKey: urlB64ToUint8Array(window._PushKey) applicationServerKey: urlB64ToUint8Array(window._PushKey)
}) })
console.info("subscribed to push notification service")
return await CreateSubscription(JSON.stringify(subscription.toJSON())) return await CreateSubscription(JSON.stringify(subscription.toJSON()))
} }
async function deletePushSubscription() { async function deletePushSubscription(registration, subscription) {
const registration = await navigator.serviceWorker.getRegistration()
const subscription = await registration.pushManager.getSubscription()
if (await subscription.unsubscribe()) { if (await subscription.unsubscribe()) {
return await DeleteSubscription(JSON.stringify(subscription.toJSON())) return await DeleteSubscription(JSON.stringify(subscription.toJSON()))
} }