feat: add advanced context actions and optional translation

This commit is contained in:
golem
2026-10-05 15:38:16 -06:00
parent 33c4dad00f
commit 5308932bf1
34 changed files with 1579 additions and 159 deletions
+48
View File
@@ -0,0 +1,48 @@
// 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();
}