// @ts-nocheck // Shared helpers for browser-cli extension command handlers. export async function getProfileAlias() { const { profileAlias } = await chrome.storage.local.get("profileAlias"); return profileAlias || "default"; } export function isErrorPageScriptError(error) { const message = String(error?.message || error || ""); return message.includes("error page") || message.includes("chrome-error://chromewebdata"); } export function isTransientScriptError(error) { const message = String(error?.message || error || ""); return message.includes("Frame with ID") || message.includes("No tab with id") || isErrorPageScriptError(error); } export async function executeScript(options, retries = 3) { for (let i = 0; i < retries; i++) { try { return await chrome.scripting.executeScript(options); } catch (e) { if (i < retries - 1 && isTransientScriptError(e)) { await new Promise(r => setTimeout(r, 300)); continue; } throw e; } } } export function tabInfo(t) { return { id: t.id, windowId: t.windowId, active: t.active, muted: Boolean(t.mutedInfo && t.mutedInfo.muted), title: t.title, url: t.url, }; } // ── Groups ──────────────────────────────────────────────────────────────────── export function isScriptableUrl(url) { if (!url) return false; return !url.startsWith("chrome://") && !url.startsWith("brave://") && !url.startsWith("about:") && !url.startsWith("edge://") && !url.startsWith("chrome-extension://"); } export async function getActiveTab() { const activeTabs = await chrome.tabs.query({ active: true }); if (!activeTabs.length) throw new Error("No active tab found"); const windows = await chrome.windows.getAll({ populate: false }); const focusedWindowIds = new Set(windows.filter(window => window.focused).map(window => window.id)); const chooseTab = (predicate) => activeTabs.find(predicate); const byFocusAndScriptable = tab => focusedWindowIds.has(tab.windowId) && isScriptableUrl(tab.url || tab.pendingUrl || ""); const byScriptable = tab => isScriptableUrl(tab.url || tab.pendingUrl || ""); const byFocus = tab => focusedWindowIds.has(tab.windowId); return chooseTab(byFocusAndScriptable) || chooseTab(byScriptable) || chooseTab(byFocus) || activeTabs[0]; } export async function resolveTabForDirectAction(tabId, actionName) { if (tabId != null) { return chrome.tabs.get(tabId); } const allTabs = await chrome.tabs.query({}); if (allTabs.length !== 1) { throw new Error( `Refusing to ${actionName} without explicit tab ID when ${allTabs.length} tabs are open` ); } return allTabs[0]; } export async function resolveGroupId(nameOrId) { const asInt = parseInt(nameOrId); if (!isNaN(asInt)) return asInt; const groups = await chrome.tabGroups.query({}); const match = groups.find(g => g.title && g.title.toLowerCase() === String(nameOrId).toLowerCase()); if (!match) throw new Error(`No tab group found with name '${nameOrId}'`); return match.id; } export function buildTabBlocks(tabs) { const blocks = []; for (const tab of tabs) { const normalizedGroupId = tab.groupId >= 0 ? tab.groupId : null; const lastBlock = blocks[blocks.length - 1]; if (lastBlock?.groupId === normalizedGroupId) { lastBlock.tabIds.push(tab.id); lastBlock.endIndex = tab.index; continue; } blocks.push({ groupId: normalizedGroupId, startIndex: tab.index, endIndex: tab.index, tabIds: [tab.id], }); } return blocks; } export function getSessionTabs(session) { if (!session) return []; if (Array.isArray(session.tabs)) { return session.tabs .map(entry => typeof entry === "string" ? { url: entry } : entry) .filter(entry => entry?.url); } if (Array.isArray(session.urls)) { return session.urls.filter(Boolean).map(url => ({ url })); } return []; } export function normalizeGroupColor(color) { const allowed = new Set(["grey", "blue", "red", "yellow", "green", "pink", "purple", "cyan", "orange"]); return allowed.has(color) ? color : "grey"; } export async function getAliases() { const { windowAliases } = await chrome.storage.local.get("windowAliases"); return windowAliases || {}; } export async function getSessions() { const { sessions } = await chrome.storage.local.get("sessions"); return sessions || {}; }