Writing Chrome Extensions in React

Rate this content
Bookmark
Slides

Did you know that Chrome extensions, or web extensions, are written in plain HTML, CSS, and JavaScript? That means you can use React to write fully featured and sophisticated web extensions using the web development skills you already know. During this talk, we'll go over the basics of web extension development, how to set up a build process for an extension using React, and review a fully functioning extension that's in the Chrome web store right now!

This talk has been presented at React Day Berlin 2022, check out the latest edition of this React Conference.

FAQ

A Chrome extension is a small software program that customizes the browsing experience. They are represented by little icons that appear in the top right of the browser window and can be installed to add functionality to the browser.

You can create a Chrome extension using React by writing the extension with HTML, CSS, and JavaScript. You can use tools like React, Webpack, and ES6 to build your extension as long as the final output complies with the web extension specification.

You can use various tools like React, Webpack, and ES6 to build a Chrome extension. Although there are no comprehensive tools like Create React App or Next.js for extensions, there are boilerplates available that can help you get started.

To load a Chrome extension, go to Chrome's extensions page (chrome://extensions), enable Developer mode, click on 'Load unpacked,' and select the build directory of your extension. The extension will then appear in the browser.

The manifest.json file is a crucial part of a Chrome extension. It contains metadata about the extension, such as its name, version, permissions, and the files it uses. This file is essential for the browser to recognize and load the extension.

Yes, you can use existing web development tools like React, Webpack, and ES6 to build Chrome extensions. The final output, however, must be in the form of HTML, CSS, and JavaScript to be compatible with the web extension specification.

Yes, Chrome extensions can be used in other browsers as well. There is a common web extension specification that many browsers follow, allowing extensions to be compatible across different platforms.

If the dev tools for your Chrome extension don't work immediately, it may be due to permissions issues. Ensure that the necessary permissions are set correctly in the manifest.json file.

You can find more resources and talks by Riaz Varani on his website riazv.me. The site includes various presentations and additional information on Chrome and web extensions.

Yes, Riaz Varani recommends a specific boilerplate for building Chrome extensions in React. This boilerplate includes the most common elements needed for extension development. You can find the link to this boilerplate in the slides of his presentation.

Riaz Virani
Riaz Virani
7 min
05 Dec, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Today's Talk is about making Chrome extensions in React. The speaker demonstrates a boilerplate project and shows how easy it is to get started. The Talk also explores the components of a Chrome extension and how changes trigger a hot reload.

1. Introduction to Chrome Extensions in React

Short description:

Hi everyone, my name is Riaz Varani and today we're going to talk about how to make Chrome extensions in React. What is a Chrome extension? How do you make one? Can you use HTML, CSS, and JS? We're going to quickly do a show and tell with a boilerplate project and see how easy it is to get live.

Hi everyone, my name is Riaz Varani and today we're going to talk about how to make Chrome extensions in React. I work for a company that I own called EQseminalytics, if you want to learn more about it, if you like any part of this talk, there's some information there in the URL below, and there's also some other talks that I've given at riazv.me.

So let's get right into it. What is a Chrome extension? Right? How does it even come about? Well, you probably already have some of them. They're the little icons that you see in the top right of your browser window if you're happening to use Chrome, but they're in a similar place if you use some of the other browsers. And in fact, they're not only just in Chrome. Some people who've only used Chrome for a long time kind of think of them inherently as Chrome, but you can actually take some of the things we're learning in this presentation to all the other browsers. And they're actually nowadays standardizing even on a common web extension specification so that all the browsers are starting to agree on obviously one way to do things. Obviously IE is not there. In fact, IE was the first to do them, so some people think that Chrome is the first to have extensions, but actually every browser vendor has its own variation extension for a long time.

Well, how do you make one? That's really the most important thing that you probably care about. It turns out you already know how. You would think, considering it's native to the browser, that it'd be something based in C or some other complicated language, but actually all of the vendors nowadays just use plain HTML, CSS and JS to let you write your extensions, so you probably already know how to write one, but there is a specific format and a specific way to package them in terms of the functionality of how web extensions work to get it to work the way that you really intended to.

The next thing I immediately think about when I hear that, can you use HTML, CSS and JS, is what can I use all the tooling that I'm used to on the web? Yes, you can. While the end result has to be HTML, CSS, and JS, you can use React, Webpack, ES6 and all of that to help you build your extension, as long as the end result is something that looks like what the web extension specification expects. There aren't great tools like Create React App or Next that sort of do all this in a package way. You kind of have to find a boilerplate or rig it yourself, but there are quite a few decent boilerplates that I would recommend that you can use. This is actually a boilerplate I recommend that I've used for a couple of extensions that just does a great job of bringing in the most common things that you'll need, so I put a QR code up there as well as the URL, and if you miss it right now and you don't want to grab it, I will have another QR code at the very end that has a link to the slides where you can grab this other QR code or grab this URL.

So we're going to quickly do a show and tell. I know it's a bit dangerous to do things live in a talk, but that's, I like to look dangerous that I can say. We're just going to look at a fork of that boilerplate, pop it up, put it into the browser and you can see how easy this is to get live. So I've already cloned that particular project, forked it and cloned it down. This is essentially pretty much what you get from that boilerplate on its own. And we're just going to do NPM run. Actually they're using yarn here. So we'll do yarn dev. That's just running this script that essentially will take all of this stuff that you see, and it's going to pump it into this disk directory in a format that is expected by a browser. So if I jump over to Chrome, I've just created a new profile in Chrome and gone to Chrome colon slash slash extensions, which is how you manage your extensions. And then you have to turn on developer mode if you've never been in here before, which enables a couple of new options. Then you click on load unpacked.

2. Exploring Chrome Extension Components

Short description:

I'm already pre-navigated to the repo that I cloned and the disk directory, which is the output of the build process. It grabs the contents of the Chrome extension, including the pop-up file. By pinning the extension, you can see the React component inside. Changing the text triggers a hot reload.

And I'm already pre-navigated to the repo that I cloned and then the disk directory, which is the output of that build process. If I select that, it's grabbing the information from what she called a manifest.json, which you can go dig into later, but it's grabbing actually what is the contents of this Chrome extension? Now there's a file in there that says pop up, which is what shows up when you click on the extension. So if I come here just to be able to see it, I'll pin it. This is our extension that we're playing with. When I click on it, you can see that it has a pop up and it has some content in there. And that is just a React component right here. So if I change this part that says learn React to learn React now, you'll see when I come over it does essentially a hot reload and that says learn React now. So essentially you've gone from what looks like I've got standard React into a pop up in my browser window.