Messages
To communicate between the content script and the background script, you need to send Messages(or use local storage)
There exists a React Store for Chrome Plugin made by Go Guardian to foster this communication
Between Background and Content
Send Message
Content
browser.runtime.sendMessage(payload); //send message
//send message and interact with response
chrome.runtime.sendMessage({type: "gid", "data": {....}}, (resp) => {
debug(resp);
});Background
Same except specify tab id and tabs sendMessage
browser.tabs
.query({ active: true, currentWindow: true })
.then(tabs => browser.tabs.sendMessage(tabs[0].id, { type: C.toast }))
.then(resp => debug("Got response", resp))
.catch(_ => debug(browser.runtime.lastError.message));Listener
Return a promise instead of sendResponse for new W3C specSender Info
Gotchas
Do not add an async listener
browser.runtime.addListener(async (req) => ....)as it will consume all the messages blocking other listeners, instead use promises inside ft
Message Passing Between Extensions
Lets you make a "public api" that other chrome extensions can use
Similar interface to regular Messages except need chrome id which you can find here
Send Message
Listener
Only in background script
From Webpages
Can't do ports, can do messages.
Must add to manifest.json
webpage
Any script in extension
Doesn't work in Firefox, instead you could do postmessage/custom events and listen in the content script
Long lived Connections
Background
content
Last updated