video-compressor/public/assets/scripts/Notifier.js

45 lines
1.1 KiB
JavaScript

export class Notifier {
constructor() {
this.checkbox = document.getElementById("notify-checkbox");
this.checkbox.checked = Notification.permission === "granted";
this.checkbox.addEventListener("change", () => this.onToggle());
navigator.permissions.query({ name: "notifications" })
.then(status => status.onchange = () => this.permissionChanged());
}
onToggle() {
if (!this.checkbox.checked) return;
if (Notification.permission === "denied") {
this.checkbox.checked = false;
alert("You have denied notification permissions for this website. Please change the permission in your browser settings before proceeding.");
return;
}
if (Notification.permission === "default") {
Notification.requestPermission();
}
}
permissionChanged() {
if (Notification.permission === "denied") {
alert("Permission was denied");
}
this.checkbox.checked = Notification.permission === "granted";
}
notifyFinished() {
if (!this.checkbox.checked) return;
const notification = new Notification("Video Compressor", { body: "Finished compressing video" });
notification.onclick = () => window.focus();
}
}