I have a variable named tabMode that can take the value ‘openSidebar’ amongst others. It is stored in the options object in storage.sync. I’d like the sidebar to only open if the value ‘openSidebar’ is assigned to tabMode. Unfortunately, I keep getting an error:
Uncaught (in promise) Error: sidebarAction.open may only be called from a user input handler
Here is the latest code that I tried: (which fails)
async function f(info, tab) {
const options = await getOptions();
handleMenuClick(info, tab, options);
}
function addClickListener() {
if (!menuClickListenerAdded) {
browser.menus.onClicked.addListener(f);
menuClickListenerAdded = true;
}
}
function removeClickListener() {
if (menuClickListenerAdded) {
browser.menus.onClicked.removeListener(f);
menuClickListenerAdded = false;
}
}
async function handleMenuClick(info, tab, options) {
const id = (info.menuItemId.startsWith('cs-')) ? info.menuItemId.replace('cs-', '') : info.menuItemId;
const ignoreIds = ['download-video', 'reverse-image-search', 'google-lens', 'options', 'multitab', 'match', 'ai-search'];
if (logToConsole) console.log('Clicked on ' + id);
if (options.tabMode === 'openSidebar' && !ignoreIds.includes(id)) {
if (logToConsole) console.log('Opening the sidebar.');
await browser.sidebarAction.open();
await browser.sidebarAction.setPanel({ panel: '' });
} else {
if (logToConsole) console.log('Not opening the sidebar.');
}
await processSearch(info, tab);
}