aboutsummaryrefslogtreecommitdiff
path: root/p/scripts/extra.js
diff options
context:
space:
mode:
Diffstat (limited to 'p/scripts/extra.js')
-rw-r--r--p/scripts/extra.js32
1 files changed, 31 insertions, 1 deletions
diff --git a/p/scripts/extra.js b/p/scripts/extra.js
index 43bbd89f5..fb1df94db 100644
--- a/p/scripts/extra.js
+++ b/p/scripts/extra.js
@@ -1,6 +1,6 @@
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0
'use strict';
-/* globals context, openNotification, xmlHttpRequestJson */
+/* globals context, notifs_html5_is_supported, openNotification, xmlHttpRequestJson */
// <crypto form (Web login)>
function poormanSalt() { // If crypto.getRandomValues is not available
@@ -581,6 +581,35 @@ function init_user_stats() {
document.querySelectorAll('tr[data-need-ajax]').forEach(row => observer.observe(row));
}
+function init_enable_notify_button() {
+ const notify_button = document.getElementById('html5_enable_notif');
+ if (!notify_button) return;
+ // it means unsupported in browser
+ if (!notifs_html5_is_supported()) {
+ notify_button.checked = false;
+ return;
+ }
+
+ // Not granted, uncheck even if it is saved in server so browser asks for permission
+ if (Notification.permission !== 'granted') {
+ notify_button.checked = false;
+ }
+
+ notify_button.addEventListener('change', async function () {
+ if (this.checked) {
+ const permission = await Notification.requestPermission();
+ context.notifs_html5_permission = permission;
+ // Uncheck if user denied
+ if (permission !== 'granted') {
+ notify_button.checked = false;
+ }
+ } else {
+ // User disabled notifications
+ context.notifs_html5_permission = 'denied';
+ }
+ });
+}
+
function init_extra_afterDOM() {
if (!window.context) {
if (window.console) {
@@ -602,6 +631,7 @@ function init_extra_afterDOM() {
init_update_feed();
init_details_attributes();
init_user_stats();
+ init_enable_notify_button();
data_auto_leave_validation(document.body);