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

Difficulty broadcasting messages due to inconsistent messaging behaviour #35

Open
oliverdunk opened this issue Jul 6, 2021 · 2 comments
Labels
inconsistency Inconsistent behavior across browsers

Comments

@oliverdunk
Copy link
Member

oliverdunk commented Jul 6, 2021

One place where standardisation of behaviour would be especially helpful is when trying to broadcast messages to all scripts in an extension.

Existing Behaviour

To understand the problem, first it helps to understand the behaviour across browsers:

Chrome

Messages sent using chrome.runtime.sendMessage do reach chrome-extension:// pages in an iframe.

From @dotproto in the Chromium Slack:

Yep, verified that this is the expected & intended behavior in Chrome.

Firefox

Messages sent using chrome.runtime.sendMessage do not reach moz-extension:// pages in an iframe.

From @zombie in the Mozilla matrix:

in firefox, a moz-extension iframe in a web page only has content script level APIs, and as such receives only messages intended for content scripts, not for extension pages

This means it is hard (impossible without ports?) to broadcast messages to everywhere in an extension without duplicates.

Workarounds that feel good at first, but have gotchas

Different options you might consider are:

Idea 1: Use chrome.runtime.sendMessage only
Problem: Iframes containing moz-extension:// pages will not receive the message

Idea 2: Use chrome.tabs.sendMessage only
Problem: Listeners in the background page will not receive the message

Idea 3: Use chrome.runtime.sendMessage and chrome.tabs.sendMessage
Problem: In Chrome, extension pages open in a tab of their own will receive duplicates

Idea 4: Use only chrome.runtime.sendMessage in Chrome and both in Firefox.
Problem: Normal content scripts will not receive the message in Chrome

Idea 5: Opening a port to the background from everywhere that wants to get broadcasts.
Problem: Introduces the requirement to track open ports in the background

Conclusion

With all of this considered, it feels like the ideal solution would be tweaking the behaviour of these so they are consistent. This would ideally be done in a way that supports the broadcast use case - not all behaviours meet this criteria. The other option would be introducing a new API that reliably broadcasts to absolutely everywhere that's listening.

@dotproto dotproto added the inconsistency Inconsistent behavior across browsers label Jul 7, 2021
@tophf
Copy link

tophf commented Jul 7, 2021

Idea 6: make Firefox implement extension:// iframes properly. AFAICT, their current behavior was caused by single-process architecture at the time they implemented WebExtensions. Now that Firefox is fully multi-process they can run the iframe in a different process that belongs to the extension as it should and as it does in Chrome. This is tracked in https://bugzil.la/1443253

@oliverdunk
Copy link
Member Author

Messaging API Background page Content scripts Iframes containing extension pages (Chrome) Iframes containing extension pages (Firefox) Iframes containing extension pages (Safari)
browser.runtime.sendMessage
browser.tabs.sendMessage

I summarised everything in a table to help (at the very least) future me. I've also added a column for Safari where opening a port seems to be the only way to message an extension page within an iframe - would love to see a behaviour change in that scenario @xeenon ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
inconsistency Inconsistent behavior across browsers
Projects
None yet
Development

No branches or pull requests

3 participants