From 2dda28d748b564938d60821e2fa8208899bc7943 Mon Sep 17 00:00:00 2001 From: Aman Date: Thu, 22 Oct 2020 20:15:18 +0530 Subject: [PATCH] Clicking on notification should show the window now --- src/index.js | 4 +++ src/package.json | 23 ++++++------ src/windows/main/main.js | 5 +++ src/windows/main/util/toggleNotifications.js | 28 +++++++-------- src/windows/main/whatsapp.js | 38 ++++++++++++++++++++ 5 files changed, 71 insertions(+), 27 deletions(-) diff --git a/src/index.js b/src/index.js index 87fffc78..bfe09f01 100644 --- a/src/index.js +++ b/src/index.js @@ -899,6 +899,10 @@ if (!singleInstanceLock) { mainWindow.webContents.send("themes-changed", true) ); + ipcMain.on("activate-window-and-tab", (_, tabid) => + mainWindow.webContents.send("activate-window-and-tab", tabid) + ); + // Set global settings whenever they are changed ipcMain.on("settings-changed", () => setGlobalSettings()); diff --git a/src/package.json b/src/package.json index 449e3f80..c762539a 100644 --- a/src/package.json +++ b/src/package.json @@ -1,6 +1,6 @@ { "name": "altus", - "version": "3.17.0", + "version": "3.18.0", "description": "Electron-based desktop wrapper for WhatsApp Web", "homepage": "https://altus.amanharwara.xyz", "repository": { @@ -53,8 +53,7 @@ "background": "build/background.png", "icon": "build/icon.icns", "iconSize": 100, - "contents": [ - { + "contents": [{ "x": 380, "y": 280, "type": "link", @@ -77,15 +76,13 @@ "allowToChangeInstallationDirectory": "true" }, "win": { - "target": [ - { - "target": "nsis", - "arch": [ - "x64", - "ia32" - ] - } - ], + "target": [{ + "target": "nsis", + "arch": [ + "x64", + "ia32" + ] + }], "icon": "build/icon.ico" }, "linux": { @@ -100,4 +97,4 @@ "resolutions": { "minimist": ">=0.2.1" } -} +} \ No newline at end of file diff --git a/src/windows/main/main.js b/src/windows/main/main.js index 4b536d8c..23417dea 100644 --- a/src/windows/main/main.js +++ b/src/windows/main/main.js @@ -248,3 +248,8 @@ ipcRenderer.on("previous-tab", () => { tabs.toggle(tabItem.previousSibling.querySelector("a")); } }); + +ipcRenderer.on("activate-window-and-tab", (e, tabid) => { + remote.getCurrentWindow().show(); + tabs.toggle(document.querySelector(`[data-tab-id="${tabid}"]`)); +}); diff --git a/src/windows/main/util/toggleNotifications.js b/src/windows/main/util/toggleNotifications.js index 6d2edcd2..3c2c483c 100644 --- a/src/windows/main/util/toggleNotifications.js +++ b/src/windows/main/util/toggleNotifications.js @@ -5,20 +5,20 @@ * @param {boolean} firstStart Whether the function is being run at the start of the app */ function toggleNotifications(whatsAppElement, setting, firstStart) { - let whatsapp = whatsAppElement; - if (firstStart) { - whatsapp.addEventListener('dom-ready', () => { - if (!setting) { - whatsapp.executeJavaScript(`window.Notification = ''`); - } - }); - } else { - if (!setting) { - whatsapp.executeJavaScript(`window.Notification = ''`); - } - } + let whatsapp = whatsAppElement; + if (firstStart) { + whatsapp.addEventListener("dom-ready", () => { + remote.webContents + .fromId(whatsapp.getWebContentsId()) + .send("toggle-notification", setting); + }); + } else { + remote.webContents + .fromId(whatsapp.getWebContentsId()) + .send("toggle-notification", setting); + } } module.exports = { - toggleNotifications -} \ No newline at end of file + toggleNotifications, +}; diff --git a/src/windows/main/whatsapp.js b/src/windows/main/whatsapp.js index 3b93cc50..0b09ffc4 100644 --- a/src/windows/main/whatsapp.js +++ b/src/windows/main/whatsapp.js @@ -691,3 +691,41 @@ function confirm_remove_quick_reply(tab_id, reply_id) { ipcRenderer.on("format-text", (_, wrapper) => { format_selected_text(wrapper); }); + +const NotificationProxy = window.Notification; + +ipcRenderer.on("toggle-notification", (_, setting) => { + if (!setting) { + window.Notification = ""; + } else { + let NativeNotification = Notification; + Notification = function (title, options) { + var notification = new NativeNotification(title, options); + + notification.addEventListener("click", function () { + ipcRenderer.send( + "activate-window-and-tab", + document.body.dataset.tabid + ); + }); + + notification.addEventListener = function () { + return true; + }; + notification.attachEvent = function () { + return true; + }; + notification.addListener = function () { + return true; + }; + + return notification; + }; + + Notification.prototype = NativeNotification.prototype; + Notification.permission = NativeNotification.permission; + Notification.requestPermission = NativeNotification.requestPermission.bind( + Notification + ); + } +});