- Show service worker notifications and focus the app when clicked - Bump the package version to 1.0.8
312 lines
8.4 KiB
JavaScript
312 lines
8.4 KiB
JavaScript
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
|
|
};
|