Files
2026-10-21 20:40:39 -04:00

59 lines
1.7 KiB
JavaScript

"use strict";
async function getTarget() {
const { targetPath } = await browser.storage.local.get("targetPath");
return targetPath || null;
}
async function resetButton(title) {
await browser.action.setIcon({ path: "icons/fallback.svg" });
await browser.action.setTitle({ title });
}
async function refreshButton() {
const targetPath = await getTarget();
if (!targetPath) {
return resetButton("Quick Profile: choose a profile");
}
let profiles;
try {
profiles = await listProfiles();
} catch (e) {
return resetButton(`Quick Profile: ${e.message}`);
}
const profile = profiles.find(p => p.path === targetPath);
if (!profile) {
return resetButton("Quick Profile: configured profile no longer exists");
}
try {
const [i16, i32] = await Promise.all([renderIcon(profile, 16), renderIcon(profile, 32)]);
await browser.action.setIcon({ imageData: { 16: i16, 32: i32 } });
} catch (e) {
await browser.action.setIcon({ path: "icons/fallback.svg" });
}
await browser.action.setTitle({ title: `Open ${profile.name}` });
}
browser.action.onClicked.addListener(async () => {
const targetPath = await getTarget();
if (!targetPath) {
return browser.runtime.openOptionsPage();
}
try {
await callHost({ cmd: "launch", path: targetPath });
} catch (e) {
await browser.action.setTitle({ title: `Quick Profile: ${e.message}` });
await browser.action.setBadgeText({ text: "!" });
setTimeout(() => browser.action.setBadgeText({ text: "" }), 3000);
}
});
browser.storage.onChanged.addListener((changes, area) => {
if (area === "local" && "targetPath" in changes) {
refreshButton();
}
});
browser.runtime.onStartup.addListener(refreshButton);
browser.runtime.onInstalled.addListener(refreshButton);