Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
510daa9c9a | ||
|
|
2b7460d976 | ||
|
|
02f5b05265 |
No files matched your search
@@ -0,0 +1,281 @@
|
||||
const path = require('node:path');
|
||||
|
||||
const {
|
||||
app,
|
||||
dialog,
|
||||
ipcMain,
|
||||
shell
|
||||
} = require('electron');
|
||||
|
||||
const TOAST_TIMEOUT_MS = 5000;
|
||||
|
||||
const TOAST_CSS = `
|
||||
#wad-toast-container {
|
||||
position: fixed;
|
||||
right: 24px;
|
||||
bottom: 24px;
|
||||
z-index: 2147483647;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-end;
|
||||
gap: 10px;
|
||||
pointer-events: none;
|
||||
font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
|
||||
}
|
||||
|
||||
#wad-toast-container .wad-toast {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 14px;
|
||||
box-sizing: border-box;
|
||||
width: 600px;
|
||||
max-width: calc(100vw - 48px);
|
||||
height: 84px;
|
||||
padding: 0 18px;
|
||||
border-radius: 14px;
|
||||
background: #2f7b24;
|
||||
box-shadow: 0 10px 28px rgba(0, 0, 0, 0.35);
|
||||
color: #fff;
|
||||
overflow: hidden;
|
||||
pointer-events: auto;
|
||||
line-height: normal;
|
||||
animation: wad-toast-in 180ms ease-out;
|
||||
transition: opacity 160ms ease, transform 160ms ease;
|
||||
}
|
||||
|
||||
#wad-toast-container .wad-toast--leaving {
|
||||
opacity: 0;
|
||||
transform: translateY(8px);
|
||||
}
|
||||
|
||||
#wad-toast-container .wad-toast__badge {
|
||||
display: grid;
|
||||
flex: none;
|
||||
place-items: center;
|
||||
width: 44px;
|
||||
height: 44px;
|
||||
border-radius: 50%;
|
||||
background: #fff;
|
||||
color: #2f7b24;
|
||||
}
|
||||
|
||||
#wad-toast-container .wad-toast__divider {
|
||||
flex: none;
|
||||
width: 1px;
|
||||
height: 44px;
|
||||
background: rgba(255, 255, 255, 0.4);
|
||||
}
|
||||
|
||||
#wad-toast-container .wad-toast__text {
|
||||
display: flex;
|
||||
flex: 1 1 auto;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
#wad-toast-container .wad-toast__title {
|
||||
font-size: 14.5px;
|
||||
font-weight: 700;
|
||||
line-height: 1.15;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
#wad-toast-container .wad-toast__file {
|
||||
overflow: hidden;
|
||||
color: rgba(255, 255, 255, 0.82);
|
||||
font-size: 12.5px;
|
||||
line-height: 1.15;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
#wad-toast-container .wad-toast__actions {
|
||||
display: flex;
|
||||
flex: none;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
#wad-toast-container .wad-toast__pill {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 7px;
|
||||
height: 36px;
|
||||
padding: 0 14px;
|
||||
border: 1.5px solid rgba(255, 255, 255, 0.9);
|
||||
border-radius: 999px;
|
||||
background: transparent;
|
||||
color: #fff;
|
||||
font: inherit;
|
||||
font-size: 12.5px;
|
||||
font-weight: 500;
|
||||
line-height: 1;
|
||||
white-space: nowrap;
|
||||
appearance: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
#wad-toast-container .wad-toast__pill:hover {
|
||||
background: rgba(255, 255, 255, 0.12);
|
||||
}
|
||||
|
||||
#wad-toast-container .wad-toast__pill:active {
|
||||
background: rgba(255, 255, 255, 0.2);
|
||||
}
|
||||
|
||||
#wad-toast-container .wad-toast__close {
|
||||
display: grid;
|
||||
flex: none;
|
||||
place-items: center;
|
||||
width: 26px;
|
||||
height: 26px;
|
||||
padding: 0;
|
||||
border: 0;
|
||||
border-radius: 50%;
|
||||
background: transparent;
|
||||
color: rgba(255, 255, 255, 0.85);
|
||||
appearance: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
#wad-toast-container .wad-toast__close:hover {
|
||||
background: rgba(255, 255, 255, 0.14);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
#wad-toast-container button:focus-visible {
|
||||
outline: 2px solid #fff;
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
@keyframes wad-toast-in {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateY(12px);
|
||||
}
|
||||
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: none;
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
#wad-toast-container .wad-toast {
|
||||
animation: none;
|
||||
transition: none;
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const downloads = new Map(); // id -> saved file path
|
||||
|
||||
let mainWindow = null;
|
||||
let nextId = 1;
|
||||
let ipcRegistered = false;
|
||||
|
||||
function setWindow(win) {
|
||||
mainWindow = win;
|
||||
}
|
||||
|
||||
function isMainWindow(sender) {
|
||||
return Boolean(
|
||||
mainWindow &&
|
||||
!mainWindow.isDestroyed() &&
|
||||
sender === mainWindow.webContents
|
||||
);
|
||||
}
|
||||
|
||||
function injectStyles(webContents) {
|
||||
return webContents.insertCSS(TOAST_CSS);
|
||||
}
|
||||
|
||||
function show(filePath) {
|
||||
if (!filePath || !mainWindow || mainWindow.isDestroyed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const id = nextId;
|
||||
nextId += 1;
|
||||
|
||||
downloads.set(id, filePath);
|
||||
|
||||
const home = app.getPath('home');
|
||||
const displayPath = filePath.startsWith(home)
|
||||
? `~${filePath.slice(home.length)}`
|
||||
: filePath;
|
||||
|
||||
mainWindow.webContents.send('download-toast:show', {
|
||||
id,
|
||||
name: path.basename(filePath),
|
||||
location: displayPath,
|
||||
timeout: TOAST_TIMEOUT_MS
|
||||
});
|
||||
}
|
||||
|
||||
function closeAll() {
|
||||
downloads.clear();
|
||||
|
||||
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||
mainWindow.webContents.send('download-toast:clear');
|
||||
}
|
||||
}
|
||||
|
||||
function registerIpc() {
|
||||
if (ipcRegistered) {
|
||||
return;
|
||||
}
|
||||
|
||||
ipcRegistered = true;
|
||||
|
||||
ipcMain.on('download-toast:action', (event, message) => {
|
||||
if (!isMainWindow(event.sender)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const id = message ? message.id : undefined;
|
||||
const action = message ? message.action : undefined;
|
||||
const filePath = downloads.get(id);
|
||||
|
||||
if (!filePath) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (action === 'folder') {
|
||||
downloads.delete(id);
|
||||
shell.showItemInFolder(filePath);
|
||||
return;
|
||||
}
|
||||
|
||||
if (action === 'open') {
|
||||
downloads.delete(id);
|
||||
|
||||
shell.openPath(filePath).then((error) => {
|
||||
if (error) {
|
||||
dialog.showErrorBox('Kan bestand niet openen', error);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (action === 'dismiss') {
|
||||
downloads.delete(id);
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.on('download-toast:closed', (event, id) => {
|
||||
if (!isMainWindow(event.sender)) {
|
||||
return;
|
||||
}
|
||||
|
||||
downloads.delete(id);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
show,
|
||||
closeAll,
|
||||
registerIpc,
|
||||
setWindow,
|
||||
injectStyles
|
||||
};
|
||||
@@ -2,15 +2,30 @@
|
||||
const {
|
||||
app,
|
||||
BrowserWindow,
|
||||
dialog,
|
||||
shell,
|
||||
session
|
||||
} = require('electron');
|
||||
|
||||
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/';
|
||||
const WHATSAPP_ORIGIN = new URL(WHATSAPP_URL).origin;
|
||||
const PARTITION = 'persist:whatsapp';
|
||||
|
||||
let mainWindow = null;
|
||||
|
||||
// Launching the app again (e.g. `whatsapp` in a terminal) should focus the
|
||||
// window we already have instead of starting a second client.
|
||||
if (!app.requestSingleInstanceLock()) {
|
||||
app.quit();
|
||||
}
|
||||
|
||||
function isWhatsAppUrl(rawUrl) {
|
||||
try {
|
||||
const url = new URL(rawUrl);
|
||||
@@ -33,6 +48,89 @@ function isExternalUrl(rawUrl) {
|
||||
}
|
||||
}
|
||||
|
||||
// WhatsApp Web decrypts media in the page and hands it to the download manager
|
||||
// as a blob: URL that belongs to the WhatsApp origin.
|
||||
function isWhatsAppBlobUrl(rawUrl) {
|
||||
try {
|
||||
const url = new URL(rawUrl);
|
||||
|
||||
return (
|
||||
url.protocol === 'blob:' &&
|
||||
url.origin === WHATSAPP_ORIGIN
|
||||
);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Regular attachments are served from the whatsapp.net CDN.
|
||||
function isWhatsAppCdnUrl(rawUrl) {
|
||||
try {
|
||||
const url = new URL(rawUrl);
|
||||
|
||||
return (
|
||||
url.protocol === 'https:' &&
|
||||
(url.hostname === 'whatsapp.net' ||
|
||||
url.hostname.endsWith('.whatsapp.net'))
|
||||
);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Save next to existing files instead of silently overwriting them.
|
||||
function nextAvailableDownloadPath(filename) {
|
||||
const directory = app.getPath('downloads');
|
||||
fs.mkdirSync(directory, { recursive: true });
|
||||
|
||||
const name = path.basename(filename || 'download') || 'download';
|
||||
const extension = path.extname(name);
|
||||
const base = path.basename(name, extension);
|
||||
|
||||
let candidate = path.join(directory, name);
|
||||
let counter = 1;
|
||||
|
||||
while (fs.existsSync(candidate)) {
|
||||
candidate = path.join(directory, `${base} (${counter})${extension}`);
|
||||
counter += 1;
|
||||
}
|
||||
|
||||
return candidate;
|
||||
}
|
||||
|
||||
function handleDownload(event, item, webContents) {
|
||||
const itemUrl = item.getURL();
|
||||
const startedByWhatsApp = webContents
|
||||
? isWhatsAppUrl(webContents.getURL())
|
||||
: false;
|
||||
|
||||
// Blob URLs can only be created by the WhatsApp origin itself, so they are
|
||||
// trusted even when Electron reports no initiating webContents.
|
||||
if (!isWhatsAppBlobUrl(itemUrl) &&
|
||||
!(startedByWhatsApp && isWhatsAppCdnUrl(itemUrl))) {
|
||||
event.preventDefault();
|
||||
return;
|
||||
}
|
||||
|
||||
const filename = item.getFilename();
|
||||
|
||||
item.setSavePath(nextAvailableDownloadPath(filename));
|
||||
|
||||
item.once('done', (_doneEvent, state) => {
|
||||
if (state === 'completed') {
|
||||
downloadToast.show(item.getSavePath());
|
||||
return;
|
||||
}
|
||||
|
||||
if (state === 'interrupted') {
|
||||
dialog.showErrorBox(
|
||||
'Download mislukt',
|
||||
`"${filename}" kon niet worden opgeslagen in je downloadmap.`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function createWindow() {
|
||||
const win = new BrowserWindow({
|
||||
width: 1200,
|
||||
@@ -45,6 +143,7 @@ function createWindow() {
|
||||
backgroundColor: '#111b21',
|
||||
webPreferences: {
|
||||
partition: PARTITION,
|
||||
preload: path.join(__dirname, 'toast-preload.js'),
|
||||
nodeIntegration: false,
|
||||
contextIsolation: true,
|
||||
sandbox: true,
|
||||
@@ -62,6 +161,42 @@ function createWindow() {
|
||||
`Chrome/${chromeVersion} Safari/537.36`
|
||||
);
|
||||
|
||||
// Keep the version visible in the window title and in the page itself.
|
||||
const windowTitle = `WhatsApp Desktop v${app.getVersion()}`;
|
||||
|
||||
win.setTitle(windowTitle);
|
||||
|
||||
win.on('page-title-updated', (event) => {
|
||||
event.preventDefault();
|
||||
win.setTitle(windowTitle);
|
||||
});
|
||||
|
||||
// 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(() => {});
|
||||
downloadToast.injectStyles(win.webContents).catch(() => {});
|
||||
});
|
||||
|
||||
win.webContents.on('did-navigate-in-page', () => {
|
||||
// version.injectVersionBadge(win.webContents).catch(() => {});
|
||||
});
|
||||
|
||||
downloadToast.setWindow(win);
|
||||
|
||||
mainWindow = win;
|
||||
|
||||
// Keep WhatsApp notifying while another app has focus.
|
||||
notifications.install(win);
|
||||
|
||||
win.on('closed', () => {
|
||||
if (mainWindow === win) {
|
||||
mainWindow = null;
|
||||
}
|
||||
|
||||
downloadToast.closeAll();
|
||||
});
|
||||
|
||||
// Block navigation outside WhatsApp Web.
|
||||
win.webContents.on('will-navigate', (event, url) => {
|
||||
if (!isWhatsAppUrl(url)) {
|
||||
@@ -82,17 +217,26 @@ function createWindow() {
|
||||
return { action: 'deny' };
|
||||
});
|
||||
|
||||
// Disable downloads by default.
|
||||
win.webContents.session.on('will-download', (event) => {
|
||||
event.preventDefault();
|
||||
});
|
||||
|
||||
// Load WhatsApp Web.
|
||||
win.loadURL(WHATSAPP_URL);
|
||||
|
||||
return win;
|
||||
}
|
||||
|
||||
app.on('second-instance', () => {
|
||||
if (!mainWindow || mainWindow.isDestroyed()) {
|
||||
createWindow();
|
||||
return;
|
||||
}
|
||||
|
||||
if (mainWindow.isMinimized()) {
|
||||
mainWindow.restore();
|
||||
}
|
||||
|
||||
mainWindow.show();
|
||||
mainWindow.focus();
|
||||
});
|
||||
|
||||
app.whenReady().then(() => {
|
||||
const whatsappSession = session.fromPartition(PARTITION);
|
||||
|
||||
@@ -119,10 +263,18 @@ app.whenReady().then(() => {
|
||||
}
|
||||
);
|
||||
|
||||
createWindow();
|
||||
// Allow WhatsApp media downloads; block downloads from anywhere else.
|
||||
whatsappSession.on('will-download', handleDownload);
|
||||
|
||||
downloadToast.registerIpc();
|
||||
notifications.registerIpc();
|
||||
|
||||
if (!mainWindow) {
|
||||
createWindow();
|
||||
}
|
||||
|
||||
app.on('activate', () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) {
|
||||
if (!mainWindow) {
|
||||
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.0",
|
||||
"version": "1.1.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "whatsapp-desktop",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.0",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"electron": "latest",
|
||||
|
||||
+10
-4
@@ -1,16 +1,16 @@
|
||||
|
||||
{
|
||||
"name": "whatsapp-desktop",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.0",
|
||||
"description": "Unofficial WhatsApp Web desktop client for Linux",
|
||||
"private": true,
|
||||
"author": {
|
||||
"name": "Zoe",
|
||||
"email": "jouw-email@example.com"
|
||||
"email": "zoe@guido-it.nl"
|
||||
},
|
||||
"homepage": "https://web.whatsapp.com/",
|
||||
"license": "MIT",
|
||||
"main": "main.js",
|
||||
"desktopName": "nl.guido-it.whatsappdesktop",
|
||||
"scripts": {
|
||||
"start": "electron .",
|
||||
"build": "electron-builder --linux deb AppImage",
|
||||
@@ -22,7 +22,7 @@
|
||||
"electron-builder": "latest"
|
||||
},
|
||||
"build": {
|
||||
"appId": "nl.zoe.whatsappdesktop",
|
||||
"appId": "nl.guido-it.whatsappdesktop",
|
||||
"productName": "WhatsApp Desktop",
|
||||
"asar": true,
|
||||
"directories": {
|
||||
@@ -31,12 +31,18 @@
|
||||
},
|
||||
"files": [
|
||||
"main.js",
|
||||
"download-toast.js",
|
||||
"notifications.js",
|
||||
"toast-preload.js",
|
||||
"version.js",
|
||||
"package.json",
|
||||
"build/icon.png"
|
||||
],
|
||||
"linux": {
|
||||
"category": "Network;InstantMessaging",
|
||||
"executableName": "whatsapp",
|
||||
"icon": "build/icons",
|
||||
"syncDesktopName": true,
|
||||
"target": [
|
||||
"deb",
|
||||
"AppImage"
|
||||
|
||||
@@ -0,0 +1,235 @@
|
||||
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';
|
||||
const LEAVE_MS = 160;
|
||||
const DEFAULT_TIMEOUT_MS = 5000;
|
||||
|
||||
const timers = new Map(); // toast element -> timeout id
|
||||
|
||||
function icon(size, viewBox, paths) {
|
||||
const svg = document.createElementNS(SVG_NS, 'svg');
|
||||
|
||||
svg.setAttribute('width', String(size));
|
||||
svg.setAttribute('height', String(size));
|
||||
svg.setAttribute('viewBox', viewBox);
|
||||
svg.setAttribute('aria-hidden', 'true');
|
||||
svg.setAttribute('focusable', 'false');
|
||||
|
||||
for (const path of paths) {
|
||||
const node = document.createElementNS(SVG_NS, 'path');
|
||||
|
||||
node.setAttribute('d', path.d);
|
||||
node.setAttribute('fill', 'none');
|
||||
node.setAttribute('stroke', 'currentColor');
|
||||
node.setAttribute('stroke-width', path.width || '1.8');
|
||||
node.setAttribute('stroke-linecap', 'round');
|
||||
node.setAttribute('stroke-linejoin', 'round');
|
||||
svg.appendChild(node);
|
||||
}
|
||||
|
||||
return svg;
|
||||
}
|
||||
|
||||
function element(tag, className, text) {
|
||||
const node = document.createElement(tag);
|
||||
|
||||
if (className) {
|
||||
node.className = className;
|
||||
}
|
||||
|
||||
if (text) {
|
||||
node.textContent = text;
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
function getContainer() {
|
||||
let container = document.getElementById(CONTAINER_ID);
|
||||
|
||||
if (container) {
|
||||
return container;
|
||||
}
|
||||
|
||||
container = element('div');
|
||||
container.id = CONTAINER_ID;
|
||||
document.body.appendChild(container);
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
function whenBodyReady(callback) {
|
||||
if (document.body) {
|
||||
callback();
|
||||
return;
|
||||
}
|
||||
|
||||
window.addEventListener('DOMContentLoaded', callback, { once: true });
|
||||
}
|
||||
|
||||
function clearTimer(toast) {
|
||||
const timer = timers.get(toast);
|
||||
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
timers.delete(toast);
|
||||
}
|
||||
}
|
||||
|
||||
function dismiss(toast, id) {
|
||||
clearTimer(toast);
|
||||
|
||||
if (!toast.isConnected || toast.classList.contains('wad-toast--leaving')) {
|
||||
return;
|
||||
}
|
||||
|
||||
toast.classList.add('wad-toast--leaving');
|
||||
|
||||
setTimeout(() => {
|
||||
toast.remove();
|
||||
|
||||
const container = document.getElementById(CONTAINER_ID);
|
||||
|
||||
if (container && !container.childElementCount) {
|
||||
container.remove();
|
||||
}
|
||||
|
||||
if (id !== undefined) {
|
||||
ipcRenderer.send('download-toast:closed', id);
|
||||
}
|
||||
}, LEAVE_MS);
|
||||
}
|
||||
|
||||
function pill(action, label, iconNode) {
|
||||
const button = element('button', 'wad-toast__pill');
|
||||
|
||||
button.type = 'button';
|
||||
button.dataset.action = action;
|
||||
button.appendChild(iconNode);
|
||||
button.appendChild(document.createTextNode(label));
|
||||
|
||||
return button;
|
||||
}
|
||||
|
||||
function buildToast(data) {
|
||||
const container = getContainer();
|
||||
const toast = element('div', 'wad-toast');
|
||||
|
||||
toast.setAttribute('role', 'status');
|
||||
toast.setAttribute('aria-live', 'polite');
|
||||
toast.dataset.id = String(data.id);
|
||||
|
||||
const badge = element('div', 'wad-toast__badge');
|
||||
badge.appendChild(icon(22, '0 0 24 24', [
|
||||
{ d: 'M12 3v12M6.5 10.5L12 16l5.5-5.5M5 20h14', width: '2' }
|
||||
]));
|
||||
|
||||
const divider = element('div', 'wad-toast__divider');
|
||||
|
||||
const text = element('div', 'wad-toast__text');
|
||||
text.appendChild(element('div', 'wad-toast__title', 'Bestand gedownload!'));
|
||||
|
||||
const file = element('div', 'wad-toast__file', data.name || 'Bestand');
|
||||
|
||||
if (data.location) {
|
||||
file.title = data.location;
|
||||
}
|
||||
|
||||
text.appendChild(file);
|
||||
|
||||
const actions = element('div', 'wad-toast__actions');
|
||||
actions.appendChild(pill('folder', 'Map openen', icon(15, '0 0 24 24', [
|
||||
{
|
||||
d: 'M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V7z'
|
||||
}
|
||||
])));
|
||||
actions.appendChild(pill('open', 'Bestand openen', icon(15, '0 0 24 24', [
|
||||
{ d: 'M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8z' },
|
||||
{ d: 'M14 3v5h5' }
|
||||
])));
|
||||
|
||||
const close = element('button', 'wad-toast__close');
|
||||
|
||||
close.type = 'button';
|
||||
close.dataset.action = 'dismiss';
|
||||
close.setAttribute('aria-label', 'Notificatie sluiten');
|
||||
close.title = 'Notificatie sluiten';
|
||||
close.appendChild(icon(13, '0 0 12 12', [
|
||||
{ d: 'M1 1l10 10M11 1L1 11', width: '1.6' }
|
||||
]));
|
||||
|
||||
toast.append(badge, divider, text, actions, close);
|
||||
container.appendChild(toast);
|
||||
|
||||
const timeout = Number(data.timeout) > 0
|
||||
? Number(data.timeout)
|
||||
: DEFAULT_TIMEOUT_MS;
|
||||
|
||||
timers.set(toast, setTimeout(() => dismiss(toast, data.id), timeout));
|
||||
|
||||
toast.addEventListener('click', (event) => {
|
||||
// Alleen echte gebruikersklikken; pagina-scripts kunnen dit niet triggeren.
|
||||
if (!event.isTrusted) {
|
||||
return;
|
||||
}
|
||||
|
||||
const target = event.target instanceof Element ? event.target : null;
|
||||
const button = target ? target.closest('button[data-action]') : null;
|
||||
|
||||
if (!button) {
|
||||
return;
|
||||
}
|
||||
|
||||
const action = button.dataset.action;
|
||||
|
||||
if (action === 'dismiss') {
|
||||
dismiss(toast, data.id);
|
||||
return;
|
||||
}
|
||||
|
||||
ipcRenderer.send('download-toast:action', { id: data.id, action });
|
||||
dismiss(toast, data.id);
|
||||
});
|
||||
}
|
||||
|
||||
function createToast(data) {
|
||||
if (!data || data.id === undefined || data.id === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
whenBodyReady(() => {
|
||||
const existing = document.querySelector(
|
||||
`.wad-toast[data-id="${String(data.id)}"]`
|
||||
);
|
||||
|
||||
if (existing) {
|
||||
return;
|
||||
}
|
||||
|
||||
buildToast(data);
|
||||
});
|
||||
}
|
||||
|
||||
ipcRenderer.on('download-toast:show', (_event, data) => {
|
||||
createToast(data);
|
||||
});
|
||||
|
||||
ipcRenderer.on('download-toast:clear', () => {
|
||||
for (const timer of timers.values()) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
|
||||
timers.clear();
|
||||
|
||||
const container = document.getElementById(CONTAINER_ID);
|
||||
|
||||
if (container) {
|
||||
container.remove();
|
||||
}
|
||||
});
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
const { app } = require('electron');
|
||||
|
||||
const BADGE_ID = 'whatsapp-desktop-version';
|
||||
|
||||
const BADGE_CSS = `
|
||||
#${BADGE_ID} {
|
||||
position: fixed;
|
||||
left: 10px;
|
||||
bottom: 8px;
|
||||
z-index: 2147483647;
|
||||
padding: 1px 7px;
|
||||
border: 1px solid rgba(134, 150, 160, 0.28);
|
||||
border-radius: 999px;
|
||||
background: rgba(17, 27, 33, 0.55);
|
||||
color: rgba(233, 237, 239, 0.62);
|
||||
font: 500 10px/1.6 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
|
||||
letter-spacing: 0.3px;
|
||||
pointer-events: none;
|
||||
user-select: none;
|
||||
}
|
||||
`;
|
||||
|
||||
function badgeScript(version) {
|
||||
return `
|
||||
(() => {
|
||||
const id = ${JSON.stringify(BADGE_ID)};
|
||||
const existing = document.getElementById(id);
|
||||
|
||||
if (existing) {
|
||||
existing.remove();
|
||||
}
|
||||
|
||||
if (!document.body) {
|
||||
return;
|
||||
}
|
||||
|
||||
const badge = document.createElement('div');
|
||||
badge.id = id;
|
||||
badge.textContent = ${JSON.stringify(`v${version}`)};
|
||||
document.body.appendChild(badge);
|
||||
})();
|
||||
`;
|
||||
}
|
||||
|
||||
async function injectVersionStyles(webContents) {
|
||||
await webContents.insertCSS(BADGE_CSS);
|
||||
}
|
||||
|
||||
async function injectVersionBadge(webContents, version = app.getVersion()) {
|
||||
await webContents.executeJavaScript(badgeScript(version));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
BADGE_ID,
|
||||
injectVersionStyles,
|
||||
injectVersionBadge
|
||||
};
|
||||
Reference in new issue
Block a user