Browser extensions - spicing it up with React

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.
Thanks for sharing this, Chris!
I'm building a browser extension that manipulates the DOM on Twitter.com.
How would you approach something like this with a React-based browser extension?
Hey Akos,
Depending on the size of the extension yeah would probably go with React. You can set the permissions to be only on Twitter and inject on then.
The pro about React is that it's a bit more flexible to make changes.
Do let me know if you want to discuss it in person, happy to help where possible.
Thanks Chris Bongers!
I started with plain JavaScript and found manipulating the existing DOM exceptionally hard.
Twitter classes are obfuscated, and the layout is complex. 🤷🏻♂️
Not that there was anything wrong with Parcel, but I wanted to try out Vite. So instead of doing it all from scratch, let's look at how we can migrate from Parcel to Vite. Note: You can also use this article as a guide on building an extension power...
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...

Now that we have our Browser extension up and running with Tailwind CSS and Parcel let's look at how we can make it more interactive.
You can choose any framework you are familiar with. I decided to go with React for this one.
The idea is to add React to have an interactive new tab browser extension.

Note: if you want to follow along, use the following GitHub branch.
First, we must let our project know we plan to use React, so let's install the needed dependencies.
npm i react react-dom
Then you can go ahead and create an src folder. It will become the central place of our React application.
Now that we have everything installed, those two are the only ones we need 🤯.
We can go ahead and render the React app.
Open up your new-tab.html page. Until now, this was our application's source, but let's remove the HTML and place this inside.
<body>
<div id="app"></div>
<script type="module" src="index.js"></script>
</body>
This will become our injection point as to where we can inject React.
We also added a script that will handle the React injection.
Go ahead and create this index.js file.
import ReactDOM from 'react-dom';
import { App } from './src/App';
const app = document.getElementById('app');
ReactDOM.render(<App />, app);
Now we can move on to creating this App component.
Add the App.js file in your src directory and place the following contents inside.
export function App() {
return (
<div className='flex flex-col items-center justify-center w-screen h-screen bg-indigo-400 text-6xl font-bold text-white'>
<p>Welcome 👋</p>
</div>
);
}
This will render what we already had in the first place.
Let's make it more interactive by creating a Counter.js component.
import { useState } from 'react';
export default function Counter() {
const [counter, setCounter] = useState(0);
const increase = () => setCounter((count) => count + 1);
const decrease = () => setCounter((count) => count - 1);
return (
<div>
<button onClick={decrease}>-</button>
<span className='px-4'>{counter}</span>
<button onClick={increase}>+</button>
</div>
);
}
Now go back to the App.js component and import the Counter.
import Counter from './Counter';
export function App() {
return (
<div className='flex flex-col items-center justify-center w-screen h-screen bg-indigo-400 text-6xl font-bold text-white'>
<p>Welcome 👋</p>
<br />
<Counter />
</div>
);
}
Now, if you run your watch or build command, you should be able to use your new React-powered browser extension.
npm run build
Note: ensure to use the
distfolder when loading the extension

You can find this article's code on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter