Plasmo Equivalent to chrome.runtime.onMessage.addListener

Hi, I am a total newbie trying to figure out how to send triggers from my popup window to my content script. Online I have been seeing examples of adding eventListeners. Does this work in Plasmo or is there a 'Plasmo specific' way to do this. Thanks!
2 Replies
Nil
Nil•14mo ago
same question here!
Sam
Sam•13mo ago
I'm still new to Plasmo and browser extensions in general. I've sent messages from the sidebar to a content script with two different methods. You can use the chrome.* methods inside a plasmo project. I've been experimenting with the native methods to learn a little more. In sidepanel.tsx I used chrome.tabs.sendMessage in an onClick handler to send the message.
onClick={async () => {
const queryOptions = { active: true, currentWindow: true };
const tabs = await chrome.tabs.query(queryOptions);

const response = await chrome.tabs.sendMessage(
tabs[0].id,
{ message: 'hello' },
);
}}
onClick={async () => {
const queryOptions = { active: true, currentWindow: true };
const tabs = await chrome.tabs.query(queryOptions);

const response = await chrome.tabs.sendMessage(
tabs[0].id,
{ message: 'hello' },
);
}}
Inside of my content script I used chrome.runtime.onMessage.addListener to handle the message.
chrome.runtime.onMessage.addListener(
async function(request, sender, sendResponse) { ... })
chrome.runtime.onMessage.addListener(
async function(request, sender, sendResponse) { ... })
What I'm getting tripped up on now is getting a response back from the listener. For whatever reason the chrome.tabs.sendMessage is returning before my content script even executes.
Want results from more Discord servers?
Add your server