aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoslot <pines@null.net>2019-05-12 05:30:01 +0200
committerRoslot <pines@null.net>2019-05-12 05:30:01 +0200
commit2f566de8b10a38329b7ad67fdb8fa6d212156646 (patch)
tree312b0db7cd0a2323efc354ce46e71d38fa01b3ea
parentFixed audio on ff, chrome, edge (diff)
downloadhackchat-2f566de8b10a38329b7ad67fdb8fa6d212156646.tar.gz
hackchat-2f566de8b10a38329b7ad67fdb8fa6d212156646.zip
Fixed permission requesting, notifications off by default
-rw-r--r--client/client.js113
1 files changed, 65 insertions, 48 deletions
diff --git a/client/client.js b/client/client.js
index 1d4088c..f2b0632 100644
--- a/client/client.js
+++ b/client/client.js
@@ -71,15 +71,65 @@ var lastSentPos = 0;
/** Notification switch and local storage behavior **/
var notifySwitch = document.getElementById("notify-switch")
var notifySetting = localStorageGet("notify-api")
+var notifyPermissionExplained = 0; // 1 = granted msg shown, -1 = denied message shown
+
+// Inital request for notifications permission
+function RequestNotifyPermission() {
+ try {
+ var notifyPromise = Notification.requestPermission();
+ if (notifyPromise) {
+ notifyPromise.then(function (result) {
+ console.log("Hack.Chat notification permission: " + result);
+ if (result === "granted") {
+ if (notifyPermissionExplained === 0) {
+ pushMessage({
+ cmd: "chat",
+ nick: "*",
+ text: "Notifications permission granted.",
+ time: null
+ });
+ notifyPermissionExplained = 1;
+ }
+ return false;
+ } else {
+ if (notifyPermissionExplained === 0) {
+ pushMessage({
+ cmd: "chat",
+ nick: "*",
+ text: "Notifications permission denied, you won't be notified if someone @mentions you.",
+ time: null
+ });
+ notifyPermissionExplained = -1;
+ }
+ return true;
+ }
+ });
+ }
+ } catch (error) {
+ pushMessage({
+ cmd: "chat",
+ nick: "*",
+ text: "Unable to create a notification.",
+ time: null
+ });
+ console.error("An error occured trying to request notification permissions. This browser might not support desktop notifications.\nDetails:")
+ console.error(error)
+ return false;
+ }
+}
+
// Update localStorage with value of checkbox
-notifySwitch.addEventListener('change', () => {
+notifySwitch.addEventListener('change', (event) => {
+ if (event.target.checked) {
+ RequestNotifyPermission();
+ }
localStorageSet("notify-api", notifySwitch.checked)
})
-// Check if localStorage value is set, defaults to ON
+// Check if localStorage value is set, defaults to OFF
if (notifySetting === null) {
- localStorageSet("notify-api", "true")
- notifySwitch.checked = true
+ localStorageSet("notify-api", "false")
+ notifySwitch.checked = false
}
// Configure notifySwitch checkbox element
if (notifySetting === "true" || notifySetting === true) {
@@ -94,7 +144,7 @@ var soundSwitch = document.getElementById("sound-switch")
var notifySetting = localStorageGet("notify-sound")
// Update localStorage with value of checkbox
-soundSwitch.addEventListener('change', () => {
+soundSwitch.addEventListener('change', (event) => {
localStorageSet("notify-sound", soundSwitch.checked)
})
// Check if localStorage value is set, defaults to OFF
@@ -116,9 +166,7 @@ function spawnNotification(title, body) {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
console.error("This browser does not support desktop notification");
- }
- // Let's check whether notification permissions have already been granted
- else if (Notification.permission === "granted") {
+ } else if (Notification.permission === "granted") { // Check if notification permissions are already given
// If it's okay let's create a notification
var options = {
body: body,
@@ -128,50 +176,19 @@ function spawnNotification(title, body) {
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== "denied") {
- var notifyPromise = Notification.requestPermission();
- if (notifyPromise) {
- notifyPromise.then(function (permission) {
- // If the user accepts, let's create a notification
- if (permission === "granted") {
- var options = {
- body: body,
- icon: "/favicon-96x96.png"
- };
- var n = new Notification(title, options);
- }
- }).catch(function (error) {
- console.error("Problem creating notification:\n" + error);
- });
+ if (RequestNotifyPermission()) {
+ var options = {
+ body: body,
+ icon: "/favicon-96x96.png"
+ };
+ var n = new Notification(title, options);
}
+ } else if (Notification.permission == "denied") {
+ // At last, if the user has denied notifications, and you
+ // want to be respectful, there is no need to bother them any more.
}
- // At last, if the user has denied notifications, and you
- // want to be respectful, there is no need to bother them any more.
}
-// Inital request for notifications permission
-try {
- Notification.requestPermission().then(function (result) {
- console.log("Hack.Chat notification permission: " + result);
- if (result === "granted") {
- pushMessage({
- cmd: "chat",
- nick: "*",
- text: "Notifications enabled.",
- time: null
- });
- } else {
- pushMessage({
- cmd: "chat",
- nick: "*",
- text: "Notifications disabled. You will not be notified if someone @'s you.",
- time: null
- });
- }
- });
-} catch (error) {
- console.error("An error occured trying to request notification permissions. This browser might not support desktop notifications.\nDetails:")
- console.error(error)
-}
function notify(args) {
// Spawn notification if enabled