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