const extensionApi = globalThis.browser ?? globalThis.chrome; extensionApi.runtime.onMessage.addListener((message, sender, sendResponse) => { if (message.type !== "COOKIE") { sendResponse({ error: `Unknown message type ${message.type}` }); return false; } const details = { url: message.url, name: message.key }; if (globalThis.browser?.cookies?.get) { extensionApi.cookies.get(details) .then((cookie) => sendResponse(cookie)) .catch((error) => sendResponse({ error: error.message })); return true; } extensionApi.cookies.get(details, (cookie) => { const runtimeError = extensionApi.runtime.lastError; if (runtimeError) { sendResponse({ error: runtimeError.message }); return; } sendResponse(cookie); }); return true; });