diff --git a/src/addons/thunderbird/src/background.js b/src/addons/thunderbird/src/background.js index 1354d6cd..4e61284a 100644 --- a/src/addons/thunderbird/src/background.js +++ b/src/addons/thunderbird/src/background.js @@ -1,6 +1,24 @@ +import { buildMeetingMessage } from "./lib/meeting-message.js"; + console.log("[meeting-link] background loaded at", new Date().toISOString()); -browser.runtime.onMessage.addListener((msg, sender) => { - console.log("[meeting-link] background received bite:", msg); - return Promise.resolve({ ok: true }); -}); +// Hardcoded for Spike 1. Spike 2 replaces this with a fetch() to your API. +const STUB_MEETING_DATA = { + url: "https://meet.example.com/m/abc-123-xyz", + telephony: { + phone_number: "+33123456789", + pin_code: "1234567890", + }, +}; + +browser.runtime.onMessage.addListener(async (msg) => { + if (msg?.type === "GET_MEETING_MESSAGE") { + try { + const built = buildMeetingMessage(STUB_MEETING_DATA); + return { ok: true, ...built }; + } catch (err) { + console.error("[meeting-link] build failed", err); + return { ok: false, error: String(err.message || err) }; + } + } +}); \ No newline at end of file diff --git a/src/addons/thunderbird/src/manifest.json b/src/addons/thunderbird/src/manifest.json index a17d64b9..492da617 100644 --- a/src/addons/thunderbird/src/manifest.json +++ b/src/addons/thunderbird/src/manifest.json @@ -10,7 +10,8 @@ } }, "background": { - "scripts": ["background.js"] + "scripts": ["background.js"], + "type": "module" }, "compose_action": { "default_title": "Insert meeting link", diff --git a/src/addons/thunderbird/src/popup/popup.html b/src/addons/thunderbird/src/popup/popup.html index 35ed9d48..aac5c428 100644 --- a/src/addons/thunderbird/src/popup/popup.html +++ b/src/addons/thunderbird/src/popup/popup.html @@ -1,12 +1,18 @@ - - - Meeting link - - - -
- - - + + + Meeting link + + + + +
+ + + \ No newline at end of file diff --git a/src/addons/thunderbird/src/popup/popup.js b/src/addons/thunderbird/src/popup/popup.js index 1d53c187..33884219 100644 --- a/src/addons/thunderbird/src/popup/popup.js +++ b/src/addons/thunderbird/src/popup/popup.js @@ -1,6 +1,65 @@ -document.getElementById("go").addEventListener("click", async () => { - console.log("[meeting-link] popup button clicked"); - const reply = await browser.runtime.sendMessage({ type: "PING" }); - document.getElementById("status").textContent = - "background replied: " + JSON.stringify(reply); +const insertBtn = document.getElementById("insert"); +const statusEl = document.getElementById("status"); + +function setStatus(text, isError = false) { + statusEl.textContent = text; + statusEl.classList.toggle("error", isError); +} + +insertBtn.addEventListener("click", async () => { + insertBtn.disabled = true; + setStatus("Generating link…"); + + try { + // 1. Find the compose tab this popup belongs to. + // A compose_action popup is anchored to a compose window, so the + // "active tab in the current window" is the compose tab itself. + const [composeTab] = await browser.tabs.query({ + active: true, + currentWindow: true, + }); + if (!composeTab) throw new Error("No compose tab found"); + + // 2. Read the current compose state — we need to know if we're + // in HTML mode or plain-text mode, and we need the existing body + // so we can append rather than overwrite. + const details = await browser.compose.getComposeDetails(composeTab.id); + + // 3. Ask the background to produce the meeting message. + const reply = await browser.runtime.sendMessage({ + type: "GET_MEETING_MESSAGE", + }); + if (!reply?.ok) throw new Error(reply?.error || "Background error"); + + // 4. Append to the existing body in the right format. + if (details.isPlainText) { + const newBody = (details.plainTextBody || "") + "\n\n" + reply.text; + await browser.compose.setComposeDetails(composeTab.id, { + plainTextBody: newBody, + }); + } else { + const newBody = appendHtmlBeforeBodyEnd(details.body || "", reply.html); + await browser.compose.setComposeDetails(composeTab.id, { + body: newBody, + }); + } + + setStatus("Inserted ✓"); + setTimeout(() => window.close(), 600); + } catch (err) { + console.error("[meeting-link] popup insert failed", err); + setStatus("Failed: " + (err.message || err), true); + insertBtn.disabled = false; + } }); + +/** + * Append HTML right before , or fall back to concatenation if no + * tag is present (Thunderbird's compose body is usually a full + * HTML document, but be defensive). + */ +function appendHtmlBeforeBodyEnd(currentHtml, fragment) { + const idx = currentHtml.toLowerCase().lastIndexOf(""); + if (idx === -1) return currentHtml + fragment; + return currentHtml.slice(0, idx) + fragment + currentHtml.slice(idx); +} \ No newline at end of file