I have a chrome extension and want to use the recently added chrome.sidePanel API to open the side panel and show my chrome extension there when a user click a button inside the extension's popup.
I have tried the following code:
App.tsx
const handleOpenSidePanel = async () => {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
const currentTabId = tabs[0].id;
chrome.runtime.sendMessage({ type: 'openSidePanel', tabId: currentTabId });
});
}
background.js
chrome.runtime.onMessage.addListener((message, tab) => {
if (message.type === "openSidePanel") {
chrome.sidePanel.open({ windowId: tab.windowId});
}
});
manifest.json
"permissions": [
"sidePanel",
"tabs"
],
"side_panel": {
"default_path": "index.html"
}
...
When I click on the button, I get the following error and the side panel doesn't open:
Uncaught (in promise) Error: At least one of "tabId" and "windowId" must be provided
message, tab
should bemessage, {tab}
, but it won't work anyway because the popup doesn't have a tab, it's a special window not related to the tab. 2) There's no need for messages and the background script here as you can directly call chrome.sidePanel.open in the popup app. – Odeenchrome.sidePanel?.open()
directly in the popup file (App.tsx
) returnsProperty 'sidepanel' does not exist on type 'typeof chrome'.ts(2339)
– Pantiachrome.sidePanel
is not supposed to be called in the application file level, but on the background/service worker. Read developer.chrome.com/docs/extensions/reference/api/sidePanel. There are no examples of the API being called at the application level. – Pantiachrome
API can be called by any extension page that has chrome-extension:// URL, which means the action popup can do it too. You can suggest to add this info to all documentation pages because looking back indeed quite a few people share the same misconception. BTW there's no such thing as "application file level" in chrome extensions. – OdeenProperty 'sidepanel' does not exist on type 'typeof chrome'
but I'm getting it in the service-worker file. – Mun