-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
477d5ad
commit e0759b3
Showing
8 changed files
with
209 additions
and
56 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
let notifications = []; | ||
let notificationsWork = false; | ||
async function checkNotifications() | ||
{ | ||
notificationsWork = false; | ||
//console.log(window.location.protocol); | ||
if (window.location.protocol != "https:" && window.location.protocol != "http:" | ||
&& window.location.protocol != "localhost:") | ||
return; | ||
|
||
if (!settingsObj["notification"]["allow-notifications"]) | ||
return; | ||
|
||
if (!("Notification" in window)) | ||
return; | ||
|
||
if (Notification.permission === "granted") | ||
{ | ||
notificationsWork = true; | ||
return; | ||
} | ||
|
||
if (Notification.permission !== "denied") | ||
{ | ||
if (await Notification.requestPermission()) | ||
notificationsWork = true; | ||
} | ||
} | ||
|
||
const windowHasFocus = function () { | ||
if (document.hasFocus()) return true | ||
let hasFocus = false | ||
|
||
window.addEventListener('focus', function () { | ||
hasFocus = true | ||
}) | ||
window.focus() | ||
|
||
return hasFocus | ||
} | ||
|
||
function clearNotifications() | ||
{ | ||
if (!notificationsWork) | ||
return; | ||
|
||
//console.log(notifications) | ||
for (let not of notifications) { | ||
//console.log(`> Closing notification: ${not.body}`); | ||
not.close(); | ||
} | ||
|
||
notifications = []; | ||
} | ||
|
||
function windowVisible() | ||
{ | ||
return (windowHasFocus() || document.visibilityState == "visible") | ||
} | ||
function canNotify() | ||
{ | ||
if (!settingsObj["notification"]["allow-notifications"]) | ||
return false; | ||
if (!notificationsWork) | ||
return false; | ||
if (windowVisible()) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
function showNotification(title, msg, callback) | ||
{ | ||
if (!canNotify()) | ||
return; | ||
|
||
//console.log(`> Showing notification: ${msg}`); | ||
|
||
const notification = new Notification(title, {body: msg, silent: true}); | ||
notifications.push(notification); | ||
|
||
notification.onclick = (ev) => | ||
{ | ||
clearNotifications(); | ||
|
||
if (typeof callback == "function") | ||
callback(ev); | ||
window.focus(); | ||
}; | ||
} | ||
|
||
window.addEventListener('focus', () => | ||
{ | ||
clearNotifications(); | ||
}); | ||
|
||
window.addEventListener('visibilitychange', () => | ||
{ | ||
if (document.visibilityState == "visible") | ||
clearNotifications(); | ||
}); | ||
|
||
|
||
let msgSound = new Audio("./assets/audio/not.wav"); | ||
function playNotificationSound() | ||
{ | ||
if (!settingsObj["notification"]["allow-sound"]) | ||
return; | ||
|
||
msgSound.play().then(); | ||
} | ||
|
||
checkNotifications().then(); |
Oops, something went wrong.