How to share service worker API response to many content scripts
I have several content scripts that require the response (HTML) that a service worker background script provides. Only one of these content scripts is able to scrape the URL that is required by this background scripts fetch that it makes (Passed via the sendToBackground body payload). I cant figure out how I can share this HTML string response from the background script to all the content scripts (like react context etc).
For this to happen I can see that the Card.tsx (containing the sendToBackground call with the required URL to call in) would need to be the first CS to run and possibly store the response so that the other CS's could call a different message that retrieves the stored value via an identifier?
Any help will be much appreciated.
![No description](https://cdn.answeroverflow.com/1237682878914105365/Screenshot_2024-05-08_at_09.23.05.png)
6 Replies
I'm new to extension development / plasmo framework, but I recently made some good progress with using
chrome.storage.local
(Plasmo has their own Storage API abstraction) to hold state required by all extension contexts.
Could you have your background script get/set the HTML string in chrome.storage.local
and have your content scripts Card.tsx
and Title.tsx
listen for changes to the value of targetHTML
(for example)?Thanks @filthytone and @Lucas . I'd used the storage api from plasmo initially but just figured there make be part fot he messaging api I'd misundertstood.
Gave +1 Rep to @filthytone (current:
#4
- 19
)You could also use the messaging api
@filthytone Which method of the messaging api would allow a content script to send to a content script? I tried
sendToContenScript()
https://github.com/PlasmoHQ/plasmo/blob/cff49572a46906ad6a8352bc8825b9660c553b57/api/messaging/src/index.ts#L29 But this only works when sending from BGSW or Pages.GitHub
plasmo/api/messaging/src/index.ts at cff49572a46906ad6a8352bc8825b9...
🧩 The Browser Extension Framework. Contribute to PlasmoHQ/plasmo development by creating an account on GitHub.