initial commit
This commit is contained in:
commit
97dd8da178
11 files changed
+480
No files matched your search
@@ -0,0 +1,58 @@
|
||||
"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);
|
||||
@@ -0,0 +1,54 @@
|
||||
"use strict";
|
||||
|
||||
const HOST = "quick_profile";
|
||||
|
||||
async function callHost(msg) {
|
||||
const reply = await browser.runtime.sendNativeMessage(HOST, msg);
|
||||
if (!reply || !reply.ok) {
|
||||
throw new Error(reply?.error || "no reply from native host");
|
||||
}
|
||||
return reply;
|
||||
}
|
||||
|
||||
async function listProfiles() {
|
||||
return (await callHost({ cmd: "list" })).profiles;
|
||||
}
|
||||
|
||||
// Firefox's avatar SVGs paint the badge circle with context-fill and the
|
||||
// glyph with context-stroke; chrome CSS supplies those from the profile
|
||||
// theme. Outside chrome we substitute the colors directly.
|
||||
function avatarDataURL(profile, size) {
|
||||
const svg = profile.images?.[size];
|
||||
if (!svg) {
|
||||
return profile.images?.custom || null;
|
||||
}
|
||||
const colored = svg
|
||||
.replaceAll("context-fill", profile.themeBg)
|
||||
.replaceAll("context-stroke", profile.themeFg);
|
||||
return "data:image/svg+xml;charset=utf-8," + encodeURIComponent(colored);
|
||||
}
|
||||
|
||||
function loadImage(src) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const img = new Image();
|
||||
img.onload = () => resolve(img);
|
||||
img.onerror = () => reject(new Error("failed to load avatar image"));
|
||||
img.src = src;
|
||||
});
|
||||
}
|
||||
|
||||
async function renderIcon(profile, px) {
|
||||
const canvas = document.createElement("canvas");
|
||||
canvas.width = canvas.height = px;
|
||||
const ctx = canvas.getContext("2d");
|
||||
const isCustom = !profile.images?.["16"];
|
||||
const img = await loadImage(avatarDataURL(profile, px <= 16 ? "16" : "48"));
|
||||
if (isCustom) {
|
||||
// Custom avatars are plain images: clip to a circle like Firefox does.
|
||||
ctx.beginPath();
|
||||
ctx.arc(px / 2, px / 2, px / 2, 0, 2 * Math.PI);
|
||||
ctx.clip();
|
||||
}
|
||||
ctx.drawImage(img, 0, 0, px, px);
|
||||
return ctx.getImageData(0, 0, px, px);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
|
||||
<circle cx="8" cy="8" r="8" fill="#5b5b66"/>
|
||||
<circle cx="8" cy="6" r="2.5" fill="#fff"/>
|
||||
<path d="M3.5 13a4.5 4.5 0 0 1 9 0z" fill="#fff"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 237 B |
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"manifest_version": 3,
|
||||
"name": "Quick Profile",
|
||||
"version": "1.0.0",
|
||||
"description": "One-click toolbar button that opens a chosen Firefox profile.",
|
||||
"browser_specific_settings": {
|
||||
"gecko": {
|
||||
"id": "quick-profile@timothykim",
|
||||
"strict_min_version": "138.0"
|
||||
}
|
||||
},
|
||||
"permissions": ["nativeMessaging", "storage"],
|
||||
"background": {
|
||||
"scripts": ["common.js", "background.js"]
|
||||
},
|
||||
"action": {
|
||||
"default_title": "Quick Profile: choose a profile",
|
||||
"default_icon": "icons/fallback.svg"
|
||||
},
|
||||
"icons": {
|
||||
"48": "icons/fallback.svg",
|
||||
"96": "icons/fallback.svg"
|
||||
},
|
||||
"options_ui": {
|
||||
"page": "options.html",
|
||||
"open_in_tab": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Quick Profile</title>
|
||||
<style>
|
||||
:root { color-scheme: light dark; font: message-box; }
|
||||
body { margin: 12px; }
|
||||
h1 { font-size: 1.1em; margin: 0 0 8px; }
|
||||
p { margin: 0 0 12px; opacity: 0.8; }
|
||||
ul { list-style: none; margin: 0; padding: 0; }
|
||||
label {
|
||||
display: flex; align-items: center; gap: 10px;
|
||||
padding: 6px 8px; border-radius: 6px; cursor: pointer;
|
||||
}
|
||||
label:hover { background: color-mix(in srgb, currentColor 8%, transparent); }
|
||||
img { width: 24px; height: 24px; border-radius: 50%; }
|
||||
.path { font-size: 0.85em; opacity: 0.6; }
|
||||
#status { margin-top: 12px; min-height: 1.2em; }
|
||||
.error { color: #d70022; }
|
||||
code { font-family: monospace; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Profile to open</h1>
|
||||
<p>Clicking the toolbar button opens (or focuses) this profile.</p>
|
||||
<ul id="profiles"></ul>
|
||||
<div id="status"></div>
|
||||
<script src="common.js"></script>
|
||||
<script src="options.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,64 @@
|
||||
"use strict";
|
||||
|
||||
const list = document.getElementById("profiles");
|
||||
const status = document.getElementById("status");
|
||||
|
||||
function showError(text) {
|
||||
status.className = "error";
|
||||
status.textContent = text;
|
||||
}
|
||||
|
||||
async function init() {
|
||||
let profiles;
|
||||
try {
|
||||
profiles = await listProfiles();
|
||||
} catch (e) {
|
||||
showError(
|
||||
`Cannot reach the native host (${e.message}). Run host/install.sh, then reload this page.`
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (!profiles.length) {
|
||||
showError("No profiles found in ~/.mozilla/firefox/Profile Groups.");
|
||||
return;
|
||||
}
|
||||
|
||||
const { targetPath } = await browser.storage.local.get("targetPath");
|
||||
|
||||
for (const profile of profiles) {
|
||||
const li = document.createElement("li");
|
||||
const label = document.createElement("label");
|
||||
|
||||
const radio = document.createElement("input");
|
||||
radio.type = "radio";
|
||||
radio.name = "target";
|
||||
radio.value = profile.path;
|
||||
radio.checked = profile.path === targetPath;
|
||||
radio.addEventListener("change", async () => {
|
||||
await browser.storage.local.set({ targetPath: profile.path });
|
||||
status.className = "";
|
||||
status.textContent = `Saved: ${profile.name}`;
|
||||
});
|
||||
|
||||
const img = document.createElement("img");
|
||||
img.alt = "";
|
||||
const src = avatarDataURL(profile, "48");
|
||||
if (src) {
|
||||
img.src = src;
|
||||
}
|
||||
|
||||
const text = document.createElement("div");
|
||||
const name = document.createElement("div");
|
||||
name.textContent = profile.name;
|
||||
const path = document.createElement("div");
|
||||
path.className = "path";
|
||||
path.textContent = profile.path;
|
||||
text.append(name, path);
|
||||
|
||||
label.append(radio, img, text);
|
||||
li.append(label);
|
||||
list.append(li);
|
||||
}
|
||||
}
|
||||
|
||||
init();
|
||||
Reference in new issue
Block a user