Browser extensions - Messaging

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
This article will examine how we can modify images on the active page. If you'd like to follow along with the article, use the following GitHub branch as your starting point. Below is a video of today's article code in action. You can see we replace ...
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

In this article, we'll look at how message sending works. You'll often want to send some data from your popup script to the background worker, where the worker will say something with the data and return it.
Let's take a look at how that works. If you wish to follow along, take the following GitHub branch as your starting point.
For this article, we want to send a message from our popup script to the background worker.
The background worker will then process and return something based on the query.

We first want to modify our src/App.jsx file to include this new button for sending the message.
<button onClick={backgroundRequest}>Background request</button>
While here, we can also add a state that will keep track of the message we get in response from the background worker.
const [message, setMessage] = useState(null);
And then add a rendering part for this text:
{
message && (
<>
<p className='text-white'>{message}</p>
<br />
</>
);
}
Now we can move on to add this function.
const backgroundRequest = () => {
chrome.runtime.sendMessage({ color }, (response) => {
setMessage(response.msg);
});
};
Easy enough, right? We call the chrome runtime and invoke the sendMessage function.
We pass the currently picked color as our parameter and, in response, set the message.
For the next step, we need to move over to our background.js script and create the receiving end for these messages.
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (!request.color) {
sendResponse({ msg: `You didn't set any color` });
}
sendResponse({ msg: `You must really like the color ${request.color}` });
});
Here we add a listener to the onMessage callback. We first want to ensure that the request includes a color. If not, we respond that the user didn't pick a color.
If they did send a color, we send our actual response.
And that's it. We can now communicate from our popup script to our background worker.
If you'd like to see the completed code, check out the following GitHub repo.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter