Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sidePanel API: lifecycle events #517

Open
fregante opened this issue Jan 7, 2024 · 9 comments
Open

sidePanel API: lifecycle events #517

fregante opened this issue Jan 7, 2024 · 9 comments
Labels
enhancement Enhancement or change to an existing feature

Comments

@fregante
Copy link

fregante commented Jan 7, 2024

Our extension uses sidebar to display extra content about the current website, the content script communicates to the sidebar, acting as a controller.

It would be useful to have more control over it, specifically, to only do operations when the sidebar is open or know when it's opened/closed.

Proposal: lifecycle events

Some example useful events could be:

  • sidePanel.onLoad (the document/context is loaded)
    • somewhat possible via manual .sendMessage to all the relevant contexts
  • sidePanel.onUnload (the document/context is unloaded/closed)
    • impossible via chrome.runtime.connect's onDisconnect event if there are multiple sidebars open, because the connection is preserved across instances: https://stackoverflow.com/a/36465331
    • impossible via onbeforeunload because .sendMessage doesn't complete

Some other more specific events could be:

  • sidePanel.onShow (like visibilitychange, the current extension's sidePanel is shown)
  • sidePanel.onHide (the user selects another extension's sidepanel)

These two are less important, I suppose a local visibilitychange + runtime.sendMessage could work as well.

@yankovichv
Copy link

+1 🙏.

Here are my similar requests - https://groups.google.com/a/chromium.org/g/chromium-extensions/c/cJmdMLmpbjg

@tophf
Copy link

tophf commented Jan 8, 2024

Why can't you use the existing DOM visibilitychange event in the side panel to inform the connected content script?

Answered below in #517 (comment)

impossible via chrome.runtime.connect's onDisconnect event if there are multiple sidebars open, because the connection is preserved across instances: https://stackoverflow.com/a/36465331

No, each instance [of the content script] has one connection (port), which is what the linked answer says in different words.

@tophf
Copy link

tophf commented Jan 8, 2024

sidePanel.onLoad (the document/context is loaded)
somewhat possible via manual .sendMessage to all the relevant contexts

It's unclear what benefits an event would provide that aren't achievable just as easily via sendMessage. An example of code might be helpful.

@yankovichv
Copy link

Why can't you use the existing DOM visibilitychange event in the side panel to inform the connected content script?

Any tricks of trying to track the visibility of our extension in the side panel via document.hidden or via clients.matchAll() didn't work. When the user switches between OS applications or minimizes the browser, we get a message that the document is invisible, which makes sense. So we need a native sidepanel API method to report show/hide and window Id accurately.

@tophf
Copy link

tophf commented Jan 8, 2024

I see, so onShow/onHide or onVisibilityChanged event is necessary, but I don't see the need for the other two (onLoad/onUnload).

@yankovichv
Copy link

Yep, this, this is not so critical.

Although I would like to have such a common event for all contexts that may occur in chrome.runtime.getContext - https://developer.chrome.com/docs/extensions/reference/api/runtime#method-getContexts

For now, it’s easier to use a poll every 5 seconds.

@tophf
Copy link

tophf commented Jan 8, 2024

I wouldn't call frequent polling an acceptable workaround. The point in my comments above was that this should be already possible via Port events.

@fregante
Copy link
Author

fregante commented Jan 8, 2024

Polling is not an alternative. By that suggestion, you don't need almost any events, you can just poll it.

Events are useful specifically because they let you avoid polling. That's the whole point of events: you want to respond to changes immediately without continuously waking up the background worker.

@tophf
Copy link

tophf commented Jan 8, 2024

to respond to changes immediately without continuously waking up the background worker.

So this seems to answer my question above and hence should be added to the description of the proposal, because currently it only mentions the content script for which a runtime Port should already work. A port doesn't work well for the background script because in order to use a port's onDisconnected event we would have to keep the background script alive either by performing an action or by calling a special API every 30 seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement or change to an existing feature
Projects
None yet
Development

No branches or pull requests

4 participants