Browser extensions - Popup page modifications

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.
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 yo...
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...

We already created a basic extension that made all the websites we visit pink, but what if we want that action to only happen when we click a button inside our popup extensions?
Well, in this article, we'll learn just that, and even better, we base the color of our local storage that we implemented in a previous article.
If you wish to follow this article, use this GitHub branch as your starting point.
What we want to achieve today is that by clicking a button inside our popup extension, the color of the active tab changes.

We'll first need to add some new permissions to our manifest.json file.
{
"permissions": ["alarms", "notifications", "storage", "activeTab", "scripting"],
}
The new ones are activeTab and scripting.
Which do the following:
activeTab allows us to modify and retrieve the active tabscripting allows us to inject scripts via the browserNow let's go ahead and modify our popup page. Open the src/App.jsx file and add a button in the render section.
return <button onClick={colorize}>Colorize 💖</button>;
Now let's add this colorize function.
const colorize = async () => {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: changeColor,
});
};
This looks super complicated, but don't worry. I'll guide you through this.
First, we need to fetch the active tab. We can use the tabs query for this.
Once we have the active tab, we can invoke the chrome scripting API.
We call the executeScript function, which can inject a script or simple function on that tab.
In our case, we inject the changeColor function.
We can then add this function to this file to be used.
const changeColor = () => {
chrome.storage.sync.get('color', ({ color }) => {
document.body.style.backgroundColor = color;
});
};
This function will read out local storage and grab the color from it. Then we'll set the documents body to that color.
And voila! With the click of a button, you're able to change any website!
Note: Remember that we added the colors in local storage? You can play around by changing the color in your options.
You can also find the complete code on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter