Add background WhatsApp notifications
- Show service worker notifications and focus the app when clicked - Bump the package version to 1.0.8
This commit is contained in:
1 parent
02f5b05265
commit
2b7460d976
5 files changed
+329
-8
No files matched your search
@@ -11,6 +11,7 @@ const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
|
||||
const downloadToast = require('./download-toast');
|
||||
const notifications = require('./notifications');
|
||||
const version = require('./version');
|
||||
|
||||
const WHATSAPP_URL = 'https://web.whatsapp.com/';
|
||||
@@ -164,17 +165,20 @@ function createWindow() {
|
||||
|
||||
// Re-apply after every full page load (e.g. after logging in).
|
||||
win.webContents.on('dom-ready', () => {
|
||||
version.injectVersionStyles(win.webContents).catch(() => {});
|
||||
version.injectVersionBadge(win.webContents).catch(() => {});
|
||||
// version.injectVersionStyles(win.webContents).catch(() => {});
|
||||
// version.injectVersionBadge(win.webContents).catch(() => {});
|
||||
downloadToast.injectStyles(win.webContents).catch(() => {});
|
||||
});
|
||||
|
||||
win.webContents.on('did-navigate-in-page', () => {
|
||||
version.injectVersionBadge(win.webContents).catch(() => {});
|
||||
// version.injectVersionBadge(win.webContents).catch(() => {});
|
||||
});
|
||||
|
||||
downloadToast.setWindow(win);
|
||||
|
||||
// Keep WhatsApp notifying while another app has focus.
|
||||
notifications.install(win);
|
||||
|
||||
win.on('closed', () => {
|
||||
downloadToast.closeAll();
|
||||
});
|
||||
@@ -235,6 +239,7 @@ app.whenReady().then(() => {
|
||||
whatsappSession.on('will-download', handleDownload);
|
||||
|
||||
downloadToast.registerIpc();
|
||||
notifications.registerIpc();
|
||||
|
||||
createWindow();
|
||||
|
||||
|
||||
@@ -0,0 +1,311 @@
|
||||
const {
|
||||
app,
|
||||
BrowserWindow,
|
||||
ipcMain
|
||||
} = require('electron');
|
||||
|
||||
const SHIM_KEY = '__wadSetWindowHidden';
|
||||
const FOCUS_CHANNEL = 'whatsapp:focus-window';
|
||||
|
||||
let ipcRegistered = false;
|
||||
|
||||
// Two things are patched inside the page:
|
||||
//
|
||||
// 1. Visibility. WhatsApp Web behaves like a background tab when the page
|
||||
// reports itself as hidden. Electron keeps reporting "visible" when another
|
||||
// window gets focus (only minimising hides the page), so we pretend the page
|
||||
// is hidden while the window is unfocused.
|
||||
//
|
||||
// 2. Service worker notifications. Electron accepts
|
||||
// ServiceWorkerRegistration.showNotification() but never displays the
|
||||
// result ("Electron doesn't support persistent notifications"). WhatsApp
|
||||
// uses that API for its "send test notification" button and for
|
||||
// notifications with buttons. Re-dispatch those as normal page
|
||||
// notifications, which Electron does show, and forward clicks back into
|
||||
// WhatsApp's own banner bookkeeping.
|
||||
const PAGE_SHIM = `
|
||||
(() => {
|
||||
const key = ${JSON.stringify(SHIM_KEY)};
|
||||
|
||||
if (window[key]) {
|
||||
return;
|
||||
}
|
||||
|
||||
const originalGetter = (property) => {
|
||||
for (let proto = Document.prototype; proto; proto = Object.getPrototypeOf(proto)) {
|
||||
const descriptor = Object.getOwnPropertyDescriptor(proto, property);
|
||||
|
||||
if (descriptor && typeof descriptor.get === 'function') {
|
||||
return descriptor.get;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
const hidden = originalGetter('hidden');
|
||||
const visibilityState = originalGetter('visibilityState');
|
||||
const webkitHidden = originalGetter('webkitHidden');
|
||||
const webkitVisibilityState = originalGetter('webkitVisibilityState');
|
||||
|
||||
if (!hidden || !visibilityState) {
|
||||
return;
|
||||
}
|
||||
|
||||
let forcedHidden = false;
|
||||
|
||||
const isHidden = () => forcedHidden || hidden.call(document);
|
||||
const state = () => {
|
||||
if (forcedHidden) {
|
||||
return 'hidden';
|
||||
}
|
||||
|
||||
return visibilityState.call(document);
|
||||
};
|
||||
|
||||
const define = (property, getter) => {
|
||||
Object.defineProperty(document, property, {
|
||||
configurable: true,
|
||||
get: getter
|
||||
});
|
||||
};
|
||||
|
||||
define('hidden', isHidden);
|
||||
define('visibilityState', state);
|
||||
|
||||
if (webkitHidden) {
|
||||
define('webkitHidden', isHidden);
|
||||
}
|
||||
|
||||
if (webkitVisibilityState) {
|
||||
define('webkitVisibilityState', state);
|
||||
}
|
||||
|
||||
const update = (value) => {
|
||||
const next = Boolean(value);
|
||||
|
||||
if (next === forcedHidden) {
|
||||
return;
|
||||
}
|
||||
|
||||
forcedHidden = next;
|
||||
document.dispatchEvent(new Event('visibilitychange'));
|
||||
};
|
||||
|
||||
// Fallback for when the main process focus events are not available.
|
||||
window.addEventListener('blur', () => update(true));
|
||||
window.addEventListener('focus', () => update(false));
|
||||
|
||||
window[key] = update;
|
||||
|
||||
// Clicking a notification should raise the app window as well. The page's
|
||||
// own window.focus() does not restore a minimised window, and window
|
||||
// managers often refuse focus to a background app, so ask the main
|
||||
// process to bring the window forward too.
|
||||
const requestFocus = () => {
|
||||
try {
|
||||
window.__wadFocusWindow?.();
|
||||
} catch {
|
||||
// The preload bridge is not available; fall back to window.focus().
|
||||
}
|
||||
|
||||
try {
|
||||
window.focus();
|
||||
} catch {
|
||||
// Ignore.
|
||||
}
|
||||
};
|
||||
|
||||
// Notifications that WhatsApp creates from the page itself get the focus
|
||||
// request as an extra click listener; their own onclick still runs.
|
||||
const NativeNotification = window.Notification;
|
||||
|
||||
if (typeof NativeNotification === 'function') {
|
||||
try {
|
||||
window.Notification = new Proxy(NativeNotification, {
|
||||
construct(target, args) {
|
||||
const notification = Reflect.construct(target, args);
|
||||
|
||||
notification.addEventListener('click', requestFocus);
|
||||
|
||||
return notification;
|
||||
}
|
||||
});
|
||||
} catch {
|
||||
// A non-configurable constructor should not break notifications.
|
||||
}
|
||||
}
|
||||
|
||||
// WhatsApp registers its banners in the page and then asks the service
|
||||
// worker to show them. Electron drops those, so show them as page
|
||||
// notifications instead.
|
||||
const showNotification = ServiceWorkerRegistration.prototype.showNotification;
|
||||
|
||||
if (typeof showNotification === 'function') {
|
||||
const bannerRegistry = () => {
|
||||
try {
|
||||
return window.require('WAWebSwNotificationBannerRegistry');
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
ServiceWorkerRegistration.prototype.showNotification =
|
||||
function (title, options) {
|
||||
const settings = options || {};
|
||||
const data = settings.data || {};
|
||||
const bannerKey =
|
||||
typeof data.notificationBannerKey === 'string'
|
||||
? data.notificationBannerKey
|
||||
: null;
|
||||
|
||||
try {
|
||||
const notificationOptions = {
|
||||
body: settings.body,
|
||||
icon: settings.icon,
|
||||
data
|
||||
};
|
||||
|
||||
const tag = settings.tag || bannerKey;
|
||||
|
||||
if (tag) {
|
||||
notificationOptions.tag = tag;
|
||||
}
|
||||
|
||||
// Chromium rejects renotify without a tag.
|
||||
if (settings.renotify && tag) {
|
||||
notificationOptions.renotify = true;
|
||||
}
|
||||
|
||||
if (settings.requireInteraction) {
|
||||
notificationOptions.requireInteraction = true;
|
||||
}
|
||||
|
||||
if (settings.silent) {
|
||||
notificationOptions.silent = true;
|
||||
}
|
||||
|
||||
if (settings.dir) {
|
||||
notificationOptions.dir = settings.dir;
|
||||
}
|
||||
|
||||
if (settings.lang) {
|
||||
notificationOptions.lang = settings.lang;
|
||||
}
|
||||
|
||||
const notification = new Notification(title, notificationOptions);
|
||||
|
||||
if (!bannerKey) {
|
||||
notification.onclick = () => {
|
||||
notification.close();
|
||||
requestFocus();
|
||||
};
|
||||
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
let clicked = false;
|
||||
|
||||
notification.onclick = () => {
|
||||
if (clicked) {
|
||||
return;
|
||||
}
|
||||
|
||||
clicked = true;
|
||||
|
||||
const registry = bannerRegistry();
|
||||
|
||||
if (registry) {
|
||||
registry.handleSwNotificationBannerClick(bannerKey);
|
||||
}
|
||||
|
||||
requestFocus();
|
||||
};
|
||||
|
||||
notification.onclose = () => {
|
||||
if (clicked) {
|
||||
return;
|
||||
}
|
||||
|
||||
const registry = bannerRegistry();
|
||||
|
||||
if (registry) {
|
||||
registry.handleSwNotificationBannerClose(bannerKey, true);
|
||||
}
|
||||
};
|
||||
} catch {
|
||||
// Permission problems or unsupported options should never break
|
||||
// the caller.
|
||||
}
|
||||
|
||||
// Callers await this promise; Electron never shows the persistent
|
||||
// notification, so resolving is enough.
|
||||
return Promise.resolve();
|
||||
};
|
||||
}
|
||||
})();
|
||||
`;
|
||||
|
||||
function setHidden(win, hidden) {
|
||||
if (!win || win.isDestroyed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
win.webContents
|
||||
.executeJavaScript(`window.${SHIM_KEY}?.(${hidden ? 'true' : 'false'})`)
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
// Bring the window to the front after a notification click. Restoring and
|
||||
// showing first matters when the window is minimised or hidden.
|
||||
function focusWindow(win) {
|
||||
if (!win || win.isDestroyed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (win.isMinimized()) {
|
||||
win.restore();
|
||||
}
|
||||
|
||||
// show() raises the window and gives it focus; restore() above handles the
|
||||
// minimised case where some window managers do not unminimise on show().
|
||||
win.show();
|
||||
win.moveTop();
|
||||
win.focus();
|
||||
|
||||
if (process.platform === 'darwin') {
|
||||
app.focus({ steal: true });
|
||||
}
|
||||
}
|
||||
|
||||
function registerIpc() {
|
||||
if (ipcRegistered) {
|
||||
return;
|
||||
}
|
||||
|
||||
ipcRegistered = true;
|
||||
|
||||
ipcMain.on(FOCUS_CHANNEL, (event) => {
|
||||
focusWindow(BrowserWindow.fromWebContents(event.sender));
|
||||
});
|
||||
}
|
||||
|
||||
function install(win) {
|
||||
win.webContents.on('dom-ready', () => {
|
||||
win.webContents.executeJavaScript(PAGE_SHIM).catch(() => {});
|
||||
setHidden(win, !win.isFocused());
|
||||
});
|
||||
|
||||
win.on('focus', () => setHidden(win, false));
|
||||
win.on('blur', () => setHidden(win, true));
|
||||
win.on('minimize', () => setHidden(win, true));
|
||||
win.on('restore', () => setHidden(win, !win.isFocused()));
|
||||
win.on('show', () => setHidden(win, !win.isFocused()));
|
||||
win.on('hide', () => setHidden(win, true));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
install,
|
||||
registerIpc,
|
||||
setHidden
|
||||
};
|
||||
Generated
+2
-2
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "whatsapp-desktop",
|
||||
"version": "1.0.6",
|
||||
"version": "1.0.8",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "whatsapp-desktop",
|
||||
"version": "1.0.6",
|
||||
"version": "1.0.8",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"electron": "latest",
|
||||
|
||||
+2
-2
@@ -1,7 +1,6 @@
|
||||
|
||||
{
|
||||
"name": "whatsapp-desktop",
|
||||
"version": "1.0.6",
|
||||
"version": "1.0.8",
|
||||
"description": "Unofficial WhatsApp Web desktop client for Linux",
|
||||
"private": true,
|
||||
"author": {
|
||||
@@ -33,6 +32,7 @@
|
||||
"files": [
|
||||
"main.js",
|
||||
"download-toast.js",
|
||||
"notifications.js",
|
||||
"toast-preload.js",
|
||||
"version.js",
|
||||
"package.json",
|
||||
|
||||
+6
-1
@@ -1,4 +1,9 @@
|
||||
const { ipcRenderer } = require('electron');
|
||||
const { contextBridge, ipcRenderer } = require('electron');
|
||||
|
||||
// Used by the page shim in notifications.js when a notification is clicked.
|
||||
contextBridge.exposeInMainWorld('__wadFocusWindow', () => {
|
||||
ipcRenderer.send('whatsapp:focus-window');
|
||||
});
|
||||
|
||||
const SVG_NS = 'http://www.w3.org/2000/svg';
|
||||
const CONTAINER_ID = 'wad-toast-container';
|
||||
|
||||
Reference in new issue
Block a user