// Firefox owns shortcut preferences; never copy them into extension storage. export async function initializeShortcut({ commands, document, window }) { const field = (id) => document.getElementById(id); let refreshId = 0; function render(shortcut) { field("shortcut-intro").textContent = shortcut ? ` Or press ${shortcut} to use your selection. With nothing selected, the shortcut opens a blank chat.` : ""; field("shortcut-hint").textContent = shortcut ? `${shortcut} runs your default action even when the context menu is off.` : "The keyboard shortcut runs your default action independently of the context menu."; field("shortcut-current").textContent = shortcut === null ? "Could not read the keyboard shortcut. Check Manage Extension Shortcuts in Add-ons." : shortcut ? `Current shortcut: ${shortcut}` : "No keyboard shortcut assigned."; } async function refresh() { const requestId = ++refreshId; try { const all = await commands.getAll(); const command = all.find(({ name }) => name === "ask-chatgpt"); if (requestId === refreshId) render(command?.shortcut ?? ""); } catch { if (requestId === refreshId) render(null); } } const button = field("change-shortcut"); button.hidden = typeof commands?.openShortcutSettings !== "function"; button.addEventListener("click", async () => { try { await commands.openShortcutSettings(); } catch { field("shortcut-current").textContent = "Open Add-ons, then the gear menu → Manage Extension Shortcuts to change your shortcut."; } }); commands?.onChanged?.addListener(({ name }) => { if (name === "ask-chatgpt") void refresh(); }); window.addEventListener("focus", refresh); document.addEventListener("visibilitychange", () => { if (document.visibilityState === "visible") void refresh(); }); await refresh(); }