Files
coms/client/public/notifications.js
2026-03-11 01:20:31 -04:00

49 lines
1.3 KiB
JavaScript

function supportsNotifications() {
return typeof Notification !== "undefined";
}
function stripHtml(input) {
const div = document.createElement("div");
div.innerHTML = input;
return div.textContent || div.innerText || "";
}
function primeNotifications() {
if (!supportsNotifications()) return;
if (Notification.permission === "default") {
Notification.requestPermission();
}
}
function showNotification(title, body) {
if (!supportsNotifications()) return;
const options = { body };
if (Notification.permission === "granted") {
new Notification(title, options);
} else if (Notification.permission === "default") {
Notification.requestPermission().then((perm) => {
if (perm === "granted") new Notification(title, options);
});
}
}
function notifyNotice(content) {
const text = stripHtml(content);
if (!text) return;
showNotification(`COMS`, text);
}
/**
* Notify the user that someone replied directly to their thread.
* @param {string} authorName
* @param {string} content
*/
function notifyDirectReply(authorName, content) {
const text = stripHtml(content);
if (!text) return;
const name = authorName || "Anon";
showNotification(`${name} replied`, text);
}
export { primeNotifications, notifyNotice, notifyDirectReply };