how to reliably detect the tab ID where a sidepanel is open in or target messages to specific sidepanel

1,409 views
Skip to first unread message

Zak El hjouji

unread,
Sep 5, 2023, 9:11:58 PM9/5/23
to Chromium Extensions
Issue:
I have multiple sidepanels open in different websites, but I want to show specific information depending on the tab. This information that I need to show is sent from the background script.

Ideal scenario: 
From the background script, I can specify the specific sidepanel to whom I can send the message. (similar to sending message to tabs with chrome.tabs.sendmessage)

Current Hack:
When I open the sidepanel for the first time, I store the active tab Id. However, this hack is not really reliable because if there is a lag by the time the user opens the sidepanel and them moving to another tab. this variable will be set incorrectly. 

Other potential solutions:
Is it possible to detect the tab Id where the sidepanel is open. I believe this should probably exist since when we enable/set the sidepanel, we specify the tabId, so there's probably a way to figure out the tabId of the sidepanel. 

````
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
/// information to show in the sidepanel of this specific tab })
```
Thank you! 

Patrick Kettner

unread,
Sep 6, 2023, 1:18:48 AM9/6/23
to Zak El hjouji, Chromium Extensions
the sidepanel is attached to a window, not a tab. So it isn't really attached to a given tab. Are you wanting to know the active tab for a given sidepanel? If so, I would use chrome.tabs.onActivated.addListener to get listen for changes to the current active tab.

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/302a63a0-d01d-466e-98fc-f7ae2cda854an%40chromium.org.

wOxxOm

unread,
Sep 6, 2023, 1:25:19 AM9/6/23
to Chromium Extensions, Patrick Kettner, Chromium Extensions, Zak El hjouji
Just supply windowId and/or tabId as path: 'foo.html?tabId=' + tabId in chrome.sidePanel.setOptions, which will be available inside via `location.search`.

Zak El hjouji

unread,
Sep 6, 2023, 12:53:39 PM9/6/23
to Chromium Extensions, Patrick Kettner, Chromium Extensions, Zak El hjouji
I don't think chrome.tabs.onActivated.addListener resolves it because a sidepanel in a different tab will also know what the active tab is. Instead i want to know the sidepanel attached to that tab. Chrome allows you to open a sidepanel per tab so Im not sure why you are saying that it is attached to a window. Do you mind elaborating? 

Zak El hjouji

unread,
Sep 6, 2023, 12:59:00 PM9/6/23
to Chromium Extensions, wOxxOm, Patrick Kettner, Chromium Extensions, Zak El hjouji
Oh very cool idea, I love that! Thank you!!!

Uladzimir Yankovich

unread,
Sep 7, 2023, 5:20:05 PM9/7/23
to Chromium Extensions, Zak El hjouji, wOxxOm, Patrick Kettner, Chromium Extensions
I will add once again that in addition to a simple way of defining a window or tab associated with a panel. There is a request to determine the position of the panel (left or right), as well as the visibility (active state) of the extension in the panel (switching panel applications do not kill inactive panels).

Ideally, all these questions would be answered by runtime.getContexts and a couple of events.

wOxxOm

unread,
Sep 8, 2023, 2:34:54 AM9/8/23
to Chromium Extensions, Uladzimir Yankovich, Zak El hjouji, wOxxOm, Patrick Kettner, Chromium Extensions
There's a way to determine the side by listening to any mouse event like click or mousemove and then comparing (await chrome.windows.getCurrent()).left to event.screenX. A simpler solution would be to use window.screenX without the need to listen to an event but it's bugged - it always equals the main browser window's position whereas it must be the panel's left coordinate which is different when the panel is at the right.

As for visibility, just use the standard DOM method: listen to the visibilitychange event.

Uladzimir Yankovich

unread,
Sep 18, 2023, 6:26:10 PM9/18/23
to wOxxOm, Chromium Extensions, Zak El hjouji, Patrick Kettner
Unfortunately, the VisibilityChange event is not a perfect solution. It is triggered when the browser is minimized or the OS desktops are switched. As a result, we had poor UX.
--
Uladzimir Yankovich,
Founder @ Manganum (manganum.app).

wOxxOm

unread,
Sep 18, 2023, 6:33:34 PM9/18/23
to Chromium Extensions, Uladzimir Yankovich, Chromium Extensions, Zak El hjouji, Patrick Kettner, wOxxOm
Did you try pagehide and pageshow events?

Uladzimir Yankovich

unread,
Sep 18, 2023, 6:42:23 PM9/18/23
to wOxxOm, Chromium Extensions, Zak El hjouji, Patrick Kettner
Maybe I'm doing something wrong, but PageHide и PageShow never triggered for me in the side panel :( 

wOxxOm

unread,
Sep 18, 2023, 7:40:29 PM9/18/23
to Chromium Extensions, Uladzimir Yankovich, Chromium Extensions, Zak El hjouji, Patrick Kettner, wOxxOm
Indeed, they don't. So, this API indeed is lacking. I would expect the document to be frozen similarly to what happnes in BFCache when it's inactive because there's no point in keeping it running as we can't just call chrome.sidePanel.open from within (it requires a gesture), we can only do it in another context such as the action popup.

The workaround might be to use visibilitychange + check the state of the window via chrome.windows.getCurrent API.

Uladzimir Yankovich

unread,
Sep 19, 2023, 4:54:12 AM9/19/23
to wOxxOm, Chromium Extensions, Zak El hjouji, Patrick Kettner
wOxxOm, thank you very much! It seems to me that you can find an emergency way out of any situation 😂

But still, I hope we will see an update to runtime.getContext and the addition of at least one event 🙏.


Reply all
Reply to author
Forward
0 new messages