12345678910import { createSummonCapability } from "./messages";1112const PING_TIMEOUT_MS = 150;1314async function pingContentScript(tabId: number): Promise<boolean> {15 return new Promise((resolve) => {16 const timer = setTimeout(() => resolve(false), PING_TIMEOUT_MS);17 try {18 chrome.tabs.sendMessage(tabId, { type: "PING" }, (response) => {19 clearTimeout(timer);20 if (chrome.runtime.lastError) {21 resolve(false);22 } else {23 resolve(response?.ok === true);24 }25 });26 } catch {27 clearTimeout(timer);28 resolve(false);29 }30 });31}3233async function openPopup(): Promise<void> {34 try {35 36 37 await (chrome.action as { openPopup?: () => Promise<void> }).openPopup?.();38 } catch {39 40 }41}4243export function setupCommands(): void {44 chrome.commands.onCommand.addListener(async (command) => {45 if (command !== "summon-inline") return;4647 const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });48 if (!tab?.id || !tab.url) return;4950 51 if (!/^https?:\/\//i.test(tab.url)) {52 await openPopup();53 return;54 }5556 const tabId = tab.id;5758 59 const alive = await pingContentScript(tabId);60 if (alive) {61 62 63 64 const origin = new URL(tab.url).origin;65 const capability = createSummonCapability(tabId, origin, 0);66 chrome.tabs.sendMessage(67 tabId,68 { type: "SUMMON_AUTOFILL", source: "shortcut", capability },69 { frameId: 0 },70 ).catch(() => {});71 return;72 }7374 75 await openPopup();76 });77}