[BUG] sendToBackground is not allowed in MAIN world content script
Opened this issue · 3 comments
What happened?
As sendToBackground notes:
It seems like sending message from MAIN world content script to background is allowed by adding extensionId. But actually it can not because chrome.runtime is undefined in main world. And you will get this error if you follow the api's notes:
Version
Latest
What OS are you seeing the problem on?
Linux
What browsers are you seeing the problem on?
Chrome
Relevant log output
No response
(OPTIONAL) Contribution
- I would like to fix this BUG via a PR
Code of Conduct
- I agree to follow this project's Code of Conduct
- I checked the current issues for duplicate problems.
also affected
If you inject into MAIN
world and need to accomplish messaging, you're going to have to rely on using window.postMessage
and have your content script set up listeners for those messages, which would then relay the message to your background script.
You could try to leverage https://docs.plasmo.com/framework/messaging#relay-flow , but I haven't personally tested it in this situation.
The main thing tho is if your code is in MAIN
world, you don't have extension access at all, so you need to do this moderated message passing to accomplish it.
If you inject into
MAIN
world and need to accomplish messaging, you're going to have to rely on usingwindow.postMessage
and have your content script set up listeners for those messages, which would then relay the message to your background script.You could try to leverage https://docs.plasmo.com/framework/messaging#relay-flow , but I haven't personally tested it in this situation.
The main thing tho is if your code is in
MAIN
world, you don't have extension access at all, so you need to do this moderated message passing to accomplish it.
Yes I knew that. I successfully send message from main world's content script to service worker with a 'bridge'. But sendMessageToBackground
's hint is really confusing, it makes me feel like just add a extensionId
and plasmo will handle it.