1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-03 15:46:23 -07:00
gochan/frontend/ts/notifications.ts

33 lines
697 B
TypeScript
Raw Normal View History

2022-07-01 11:16:13 -07:00
import $ from "jquery";
const noteCloseTime = 4*1000; // 4 seconds
2022-07-01 11:16:13 -07:00
const noteIcon = webroot + "/favicon.png";
function canNotify() {
return (location.protocol === "https:")
&& (typeof Notification !== "undefined");
}
2023-05-13 23:46:41 -07:00
export function notify(title: string, body: string, img = noteIcon) {
2023-05-23 12:32:46 -07:00
const n = new Notification(title, {
body: body,
image: img,
2022-07-01 11:16:13 -07:00
icon: noteIcon
});
setTimeout(() => {
n.close();
}, noteCloseTime);
}
2023-05-23 12:32:46 -07:00
$(() => {
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}`);
});
});