I’m trying to make the browser action in my Firefox MV3 extension configurable so that the user can choose to either open an extension page in a popup window or show a sidebar UI. The configuration would be set in the extension preferences page, and the setting stored in extension storage (get/set via the browser.storage.local
API).
But I’m running into a blocker due to browser.sidebarAction.open()
only callable from a user action. In the browser action handler, I would need to read the user’s preference from storage to determine whether to open the sidebar or the popup window. But the following doesn’t work:
browser.action.onClicked.addListener(async (aTab) => {
// Get the user's preferred action when the browser action is clicked
let pref = await browser.storage.local.get("browserAction");
if (pref.browserAction == 1) {
// This doesn't work!
browser.sidebarAction.open();
}
else {
openThePopupWindow();
}
});
And from what I understand, it doesn’t work because putting browser.sidebarAction.open()
inside an async function causes it to lose the user action/input context. But it’s necessary because the browser.storage
APIs are asynchronous.
What workarounds should I consider?