From 5bea9c035ac755ed99fe6d74c411fe3a0c50f5db Mon Sep 17 00:00:00 2001 From: Sherif Maher Date: Wed, 25 Nov 2026 03:26:25 +0000 Subject: [PATCH] fix activate tab issues --- src/js/popup/activate-tab.js | 21 ++++++++++++++++ src/js/popup/app.jsx | 11 ++++---- test/unit/activate-tab.test.js | 46 ++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 src/js/popup/activate-tab.js create mode 100644 test/unit/activate-tab.test.js diff --git a/src/js/popup/activate-tab.js b/src/js/popup/activate-tab.js new file mode 100644 index 00000000..4ffd6d9e --- /dev/null +++ b/src/js/popup/activate-tab.js @@ -0,0 +1,21 @@ + // bring the tab's window forward and make the tab active. both calls are + // made synchronously, before anything is awaited, because the toolbar menu + // is an action popup: focusing a browser window takes focus away from it, + // and Chrome then closes it and destroys its page. a tabs.update() chained + // after windows.update() resolved would never be sent, so the menu would + // just close without switching tabs (issues #170 and #173). once a call + // has been dispatched, the browser carries it out even if the page is gone. + // + // the window is still focused first, since activating the window can + // sometimes put keyboard focus on the very first tab button on macOS 10.14 + // (could never repro on 10.12), and focusing the tab afterwards fixes that. +export function activateTab( + tab, + options) +{ + const windowFocused = chrome.windows.update(tab.windowId, { focused: true }); + const tabActivated = chrome.tabs.update(tab.id, { active: true, ...options }); + + return Promise.all([windowFocused, tabActivated]) + .then(([, activatedTab]) => activatedTab); +} diff --git a/src/js/popup/app.jsx b/src/js/popup/app.jsx index 02c09371..699551d9 100644 --- a/src/js/popup/app.jsx +++ b/src/js/popup/app.jsx @@ -6,6 +6,7 @@ import MessageItem from "./message-item"; import Banner from "./banner"; import OptionsButton from "./options-button"; import { connectPopupWindow } from "./popup-window"; +import { activateTab } from "./activate-tab"; import scoreItems from "./score/score-items"; import initTabs from "./data/init-tabs"; import getBookmarks from "./data/get-bookmarks"; @@ -797,12 +798,10 @@ export default class App extends React.Component { // focusing the tab's window blurs the popup, which closes it this.pendingCloseReason = "select"; - // bring the tab's window forward *before* focusing the tab, since - // activating the window can sometimes put keyboard focus on the - // very first tab button on macOS 10.14 (could never repro on - // 10.12). then focus the tab, which should fix any focus issues. - return chrome.windows.update(tab.windowId, { focused: true }) - .then(() => chrome.tabs.update(tab.id, { active: true, ...options })) + // focus the window and the tab in one go, since in the toolbar + // menu, focusing the window closes the menu before anything + // chained after it can run. see activateTab(). + return activateTab(tab, options) .then((focusedTab) => { this.executeAddTab(); diff --git a/test/unit/activate-tab.test.js b/test/unit/activate-tab.test.js new file mode 100644 index 00000000..40d40e4d --- /dev/null +++ b/test/unit/activate-tab.test.js @@ -0,0 +1,46 @@ +import { describe, it, expect, beforeEach, vi } from "vitest"; +import { activateTab } from "@/popup/activate-tab"; + + // the toolbar menu is an action popup, which Chrome closes, destroying its + // page, as soon as focusing a browser window takes focus away from it. any + // chrome call queued behind an await of windows.update() never gets sent, + // so selecting a tab just closed the menu (issues #170 and #173). both + // calls have to be dispatched synchronously, before the first await. +describe("activateTab", () => { + let windowsUpdate; + let tabsUpdate; + + beforeEach(() => { + // windows.update() never settles, as if the page died mid-await + windowsUpdate = vi.spyOn(chrome.windows, "update") + .mockImplementation(() => new Promise(() => {})); + tabsUpdate = vi.spyOn(chrome.tabs, "update") + .mockImplementation((id, props) => Promise.resolve({ id, ...props })); + }); + + it("dispatches both the window focus and tab activation synchronously", () => { + activateTab({ id: 7, windowId: 3 }); + + expect(windowsUpdate).toHaveBeenCalledWith(3, { focused: true }); + expect(tabsUpdate).toHaveBeenCalledWith(7, { active: true }); + }); + + it("focuses the window before activating the tab", () => { + activateTab({ id: 7, windowId: 3 }); + + expect(windowsUpdate.mock.invocationCallOrder[0]) + .toBeLessThan(tabsUpdate.mock.invocationCallOrder[0]); + }); + + it("passes extra options, like an unsuspend URL, to tabs.update()", () => { + activateTab({ id: 7, windowId: 3 }, { url: "https://example.com/" }); + + expect(tabsUpdate).toHaveBeenCalledWith(7, { active: true, url: "https://example.com/" }); + }); + + it("resolves to the updated tab", async () => { + windowsUpdate.mockResolvedValue({ id: 3 }); + + await expect(activateTab({ id: 7, windowId: 3 })).resolves.toEqual({ id: 7, active: true }); + }); +}); \ No newline at end of file