Server/Web: Support "copy" action button to copy a value to the clipboard

This commit is contained in:
binwiederhier
2026-02-08 14:20:03 -05:00
parent 65050ef4dc
commit 3f0a7b65ee
10 changed files with 384 additions and 22 deletions

View File

@@ -4,6 +4,7 @@ import { NavigationRoute, registerRoute } from "workbox-routing";
import { NetworkFirst } from "workbox-strategies";
import { clientsClaim } from "workbox-core";
import { dbAsync } from "../src/app/db";
import { ACTION_COPY, ACTION_HTTP, ACTION_VIEW } from "../src/app/actions";
import { badge, icon, messageWithSequenceId, notificationTag, toNotificationParams } from "../src/app/notificationUtils";
import initI18n from "../src/app/i18n";
import {
@@ -250,12 +251,32 @@ const handleClick = async (event) => {
}
};
if (action.action === "view") {
if (action.action === ACTION_VIEW) {
self.clients.openWindow(action.url);
if (action.clear) {
await clearNotification();
}
} else if (action.action === "http") {
} else if (action.action === ACTION_COPY) {
try {
// Service worker can't access the clipboard API directly, so we try to
// open a focused client and use it, or fall back to opening a window
const allClients = await self.clients.matchAll({ type: "window" });
const focusedClient = allClients.find((c) => c.focused) || allClients[0];
if (focusedClient) {
focusedClient.postMessage({ type: "copy", value: action.value });
}
if (action.clear) {
await clearNotification();
}
} catch (e) {
console.error("[ServiceWorker] Error performing copy action", e);
self.registration.showNotification(`${t("notifications_actions_failed_notification")}: ${action.label} (${action.action})`, {
body: e.message,
icon,
badge,
});
}
} else if (action.action === ACTION_HTTP) {
try {
const response = await fetch(action.url, {
method: action.method ?? "POST",