Add WhatsApp media downloads and completion toasts

- Save WhatsApp media downloads without overwriting existing files
- Show download notifications with open and folder actions
- Display the app version and update Linux package metadata
This commit is contained in:
JustZoe101 committed 2026-11-10 10:59:28 +01:00
1 parent 78e64c836f
commit 02f5b05265
6 files changed
+700 -10

No files matched your search

+281
View File
@@ -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
};
+122 -5
View File
@@ -2,13 +2,19 @@
const {
app,
BrowserWindow,
dialog,
shell,
session
} = require('electron');
const fs = require('node:fs');
const path = require('node:path');
const downloadToast = require('./download-toast');
const version = require('./version');
const WHATSAPP_URL = 'https://web.whatsapp.com/';
const WHATSAPP_ORIGIN = new URL(WHATSAPP_URL).origin;
const PARTITION = 'persist:whatsapp';
function isWhatsAppUrl(rawUrl) {
@@ -33,6 +39,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 +134,7 @@ function createWindow() {
backgroundColor: '#111b21',
webPreferences: {
partition: PARTITION,
preload: path.join(__dirname, 'toast-preload.js'),
nodeIntegration: false,
contextIsolation: true,
sandbox: true,
@@ -62,6 +152,33 @@ 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);
win.on('closed', () => {
downloadToast.closeAll();
});
// Block navigation outside WhatsApp Web.
win.webContents.on('will-navigate', (event, url) => {
if (!isWhatsAppUrl(url)) {
@@ -82,11 +199,6 @@ 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);
@@ -119,6 +231,11 @@ app.whenReady().then(() => {
}
);
// Allow WhatsApp media downloads; block downloads from anywhere else.
whatsappSession.on('will-download', handleDownload);
downloadToast.registerIpc();
createWindow();
app.on('activate', () => {
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "whatsapp-desktop",
"version": "1.0.0",
"version": "1.0.6",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "whatsapp-desktop",
"version": "1.0.0",
"version": "1.0.6",
"license": "MIT",
"devDependencies": {
"electron": "latest",
+8 -3
View File
@@ -1,16 +1,17 @@
{
"name": "whatsapp-desktop",
"version": "1.0.0",
"version": "1.0.6",
"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 +23,7 @@
"electron-builder": "latest"
},
"build": {
"appId": "nl.zoe.whatsappdesktop",
"appId": "nl.guido-it.whatsappdesktop",
"productName": "WhatsApp Desktop",
"asar": true,
"directories": {
@@ -31,12 +32,16 @@
},
"files": [
"main.js",
"download-toast.js",
"toast-preload.js",
"version.js",
"package.json",
"build/icon.png"
],
"linux": {
"category": "Network;InstantMessaging",
"icon": "build/icons",
"syncDesktopName": true,
"target": [
"deb",
"AppImage"
+230
View File
@@ -0,0 +1,230 @@
const { ipcRenderer } = require('electron');
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
View File
@@ -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
};