Originally created by: eboraks
Chrome Version
Version 117.0.5901.0 (Official Build) canary (64-bit)
Use Case
This is about an extension that summarizes text from a webpage. When the user clicks the action button, the background script opens a side panel to display the summarized text. To accomplish this, I want the side panel to appear only after the action button is clicked and only for the tabs where the action button was clicked.
Describe the bug
When opening a side-panel and setting the option to use tabId, I expect that the sidePanel will be limited to the tab. But, that isn't the case.
According to the docs
When using sidePanel.open() (developer.chrome.com), you must choose the context in which it should open. Use windowId to open a global side panel. Alternatively, set the tabId to open the side panel only on a specific tab.
background.js
console.log(
'background.js got message. Action Clicked {tab.id: ' + tab.id + '}'
)
// This will open a tab-specific side panel only on the current tab.
chrome.sidePanel.open({ tabId: tab.id })
chrome.sidePanel.setOptions({
tabId: tab.id,
path: 'sidepanel/sidepanel.html',
enabled: true,
})
})
manifest
"background": {
"service_worker": "src/background.js"
},
"side_panel": {
"default_path": "sidepanel/sidepanel.html"
},
"permissions": ["tabs", "sidePanel"],
To Reproduce
Expected behavior
Since the tab was open with tabId the expected behavior is for the side panel to close when moving between tabs.
Originally posted by: eboraks
I added the following code to see the relationship between tabId and sidePanel
chrome.tabs.onActivated.addListener(async ({ tabId }) => { const { path } = await chrome.sidePanel.getOptions({ tabId }) console.log('tabId: ' + tabId + ' path: ' + path) })As I moved between tabs that console printed the following.
Originally posted by: zakariaelh
hey there, were you able to resolve this? I'm having the same issue and it looks like the documentation says that this behavior is somehow expected.
link
Originally posted by: AmySteam
@eboraks Have you tried removing the default side panel declaration in the manifest?
Originally posted by: zakariaelh
This works, thank you @AmySteam!
Ticket changed by: AmySteam
Originally posted by: ehynds
For other users who might land here, if you have code that looks similar to this:
And are receiving this error:
Make sure you're calling
setOptionsbeforeopen. After making that change, you might start experiencing this error:To fix, remove
awaitfrom thesetOptionscall. Final working code (for me anyway):Originally posted by: bmz1
@ehynds Thanks, it's indeed working. However, the type definitions seem a bit off for
open(), as it indicates that it's not a promise.Originally posted by: belthaZornv
If I do that, this won't work:
Still surprised how complicated it is to just set a sidepanel to stay active on the initial tab 🤦♂️
Originally posted by: belthaZornv
Tried this too:
To no avail, @ehynds do you see anything wrong from what I'm trying to achieve? :D
Originally posted by: oliverdunk
@belthaZornv, you should be able to use
tab.id(from the event listener arguments) rather than querying for the active tab explicitly :)That will also allow you to remove the
awaitand callopensynchronously.Originally posted by: belthaZornv
oops, what a blonde moment that is. hahaha! let me try with that!
Originally posted by: belthaZornv
Cleaned up the code - still opening on all tabs though lol!
Originally posted by: oliverdunk
Yeah, this seems to be the behavior today. I don't remember off the top of my head if that was intentional - I'll try to follow-up on https://github.com/GoogleChrome/chrome-extensions-samples/issues/1179.
Originally posted by: belthaZornv
Thanks @oliverdunk, appreciate it 🙏
Originally posted by: onslauth
@belthaZornv
I have it working using this
Originally posted by: Mohamed3nan
Wow, I can't believe how long it took to figure this out! 😄
Thank you!
--
Here's what finally worked for me after spending the whole day on it :D
and in the Manifest file remove this:
Originally posted by: munr0
@Mohamed3nan, this workaround worked for me! It seems like the issue comes down to the thing in the manifest.
unfortunately however using
.setOptionsbefore.opento emulate this manifest declaration takes too long when combined with my other code and now I'm running into this...CC: [#1179]
Related
Tickets: #1179
Originally posted by: Nabeel-Asghar
Default path should be removable as a cli command.
This is the way I got it to work on only certain sites I wanted it to and I have to manually remove
"side_panel": { "default_path": "sidepanel.html" }from the manifest.json that's generated.