Skip to content

Commit 60fc132

Browse files
committed
Fix action click fallback to open side panel or dashboard
1 parent 0b8e386 commit 60fc132

1 file changed

Lines changed: 40 additions & 3 deletions

File tree

background/service-worker.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,19 @@ async function openPanelForAllWindows() {
341341
return true;
342342
}
343343

344+
async function openPanelForWindow(windowId) {
345+
if (!chrome.sidePanel?.open || typeof windowId !== "number") {
346+
return false;
347+
}
348+
349+
try {
350+
await chrome.sidePanel.open({ windowId });
351+
return true;
352+
} catch {
353+
return false;
354+
}
355+
}
356+
344357
async function initializeSidePanelBehavior() {
345358
if (!chrome.sidePanel?.setPanelBehavior) {
346359
return;
@@ -383,9 +396,33 @@ chrome.runtime.onStartup.addListener(() => {
383396
});
384397

385398
chrome.action.onClicked.addListener(() => {
386-
openPanelForAllWindows().catch((error) => {
387-
console.error("Failed to open side panel", error);
388-
});
399+
// Keep icon-click predictable: open current window panel first, then best-effort global open.
400+
chrome.windows
401+
.getLastFocused()
402+
.then(async (windowEntry) => {
403+
const focusedWindowId = typeof windowEntry?.id === "number" ? windowEntry.id : null;
404+
let opened = false;
405+
406+
if (focusedWindowId !== null) {
407+
opened = await openPanelForWindow(focusedWindowId);
408+
}
409+
410+
if (!opened) {
411+
opened = await openPanelForAllWindows();
412+
}
413+
414+
if (!opened) {
415+
await chrome.tabs.create({ url: getDashboardUrl() });
416+
return;
417+
}
418+
419+
openPanelForAllWindows().catch(() => {
420+
// Ignore follow-up global open failures.
421+
});
422+
})
423+
.catch(async () => {
424+
await chrome.tabs.create({ url: getDashboardUrl() });
425+
});
389426
});
390427

391428
chrome.tabs.onCreated.addListener((tab) => {

0 commit comments

Comments
 (0)