1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-07 06:06:23 -07:00
gochan/frontend/js/notifications.js
2022-07-01 11:16:13 -07:00

35 lines
No EOL
698 B
JavaScript

/* global webroot */
import $ from "jquery";
const noteCloseTime = 4*1000; // 4 seconds
const noteIcon = webroot + "/favicon.png";
function canNotify() {
return (location.protocol == "https:")
&& (typeof Notification !== "undefined");
}
export function notify(title, body, img = noteIcon) {
let n = new Notification(title, {
body: body,
image: img,
icon: noteIcon
});
setTimeout(() => {
n.close();
}, noteCloseTime);
}
$(() => {
if(!canNotify())
return;
Notification.requestPermission().then(granted => {
if(granted != "granted")
return Promise.reject("denied");
}).catch(err => {
if(err != "denied")
console.log(`Error starting notifications: ${err}`);
});
});