Setting Up Vite and React for Chrome Extension Development

Sviatoslav Oleksiv
5 Apr 202417:42

Summary

TLDRIn this tutorial, the creator guides viewers through the process of developing a Chrome extension using React and TypeScript. Starting from setting up a new project with npm, they explore selecting React and TypeScript as the framework and language. The video explains creating a 'manifest.json' file, which is crucial for defining the extension's properties and behavior. It also delves into the intricacies of setting up the extension to open in a sidebar panel upon clicking the extension icon, including handling permissions and integrating background scripts. The creator concludes with a successful test of the extension, encouraging viewers to leave feedback.

Takeaways

  • 🔧 The video is a tutorial on creating a Chrome extension using React and TypeScript.
  • 💻 The process starts by initiating a new project using the `npm create vite@latest` command.
  • 📝 The tutorial guides viewers to choose React and TypeScript as the framework and language for the extension.
  • 🗂️ A new file named `manifest.json` is required to configure the Chrome extension settings.
  • 🛠️ The tutorial suggests using the latest manifest version, which is version 3 at the time of the video.
  • 📁 The `public` folder's contents should be included in the build folder for the extension to work correctly.
  • 🔗 The manifest file needs to specify the correct file paths for the extension's features, like the sidebar script.
  • 🚫 The video mentions that a background script is not necessary for the extension's basic functionality.
  • 🔄 The tutorial includes building the application and loading the unpacked extension in Chrome to test it.
  • 🔗 The extension can be opened as a side panel by setting the appropriate permissions and manifest configurations.
  • 💡 The video concludes with a working Chrome extension that opens and closes a side panel upon clicking the extension icon.

Q & A

  • What is the purpose of the video?

    -The purpose of the video is to demonstrate how to create a Chrome extension using React and TypeScript.

  • What is the first step to create a new Chrome extension project?

    -The first step is to visit the webextension.dev website, click the 'Get Started' button, and follow the instructions provided.

  • Which command is used to create a new project with a specific framework?

    -The command used to create a new project is 'npm create webext latest', and you can choose the framework or library you want to use.

  • What does the video suggest for the project name when creating a new Chrome extension?

    -The video suggests using 'Chrome extension test' as the project name.

  • What additional features can be chosen during the project creation process?

    -During the project creation process, you can choose to use React, TypeScript, and optionally SWC as a transpiler.

  • What is the significance of the 'manifest.json' file in a Chrome extension?

    -The 'manifest.json' file is crucial as it defines the extension's settings, including its version, permissions, and the files that make up the extension.

  • Why might the video creator choose not to include a popup in their Chrome extension?

    -The video creator might choose not to include a popup because their extension is designed to be opened as a sidebar, not as a popup.

  • What is the role of the 'background' script in a Chrome extension?

    -The 'background' script runs in the background and can perform actions such as listening for browser events or interacting with the extension's UI.

  • How can the video creator ensure their extension opens in a side panel?

    -The video creator can ensure their extension opens in a side panel by setting the 'panel' property in the 'manifest.json' file and using the 'chrome.action.onClicked' event in the background script.

  • What is the final step to test the Chrome extension before it's published?

    -The final step is to build the extension, load it as an unpacked extension in Chrome, and test its functionality.

  • What does the video creator do if the extension doesn't work as expected?

    -If the extension doesn't work as expected, the video creator suggests checking the documentation, reviewing the code, and ensuring all necessary files are correctly linked and included.

Outlines

00:00

🛠️ Setting Up a Chrome Extension with React

The speaker begins by introducing a tutorial on creating a Chrome extension using React and Webpack. They navigate to the Webpack documentation website to get started, and demonstrate how to initiate a new project using the 'npm create web' command. The audience is guided through the process of naming the project 'Chrome extension test' and choosing React as the framework. The speaker opts for TypeScript as the transpiler and proceeds to install the necessary packages. They then open the project in a code editor and discuss the next steps, which include running 'npm install' and 'npm start' to set up the development server. The focus is on adapting a React application to function as a Chrome extension, with a plan to implement it as a sidebar within the browser.

05:02

📄 Creating the Manifest File for the Extension

In this segment, the speaker explains the need to create a 'manifest.json' file, which is essential for any Chrome extension. They consider asking Chat GPT for a basic template but decide to create one manually. The manifest file includes the version number, specifies that the extension uses React, and outlines the necessary permissions. The speaker decides not to include a popup and instead focuses on setting up a sidebar. They discuss the inclusion of a background script, which is not necessary for their project, and proceed to build the application. The process involves ensuring that the 'manifest.json' file is correctly linked to the sidebar script and that the public folder's contents are correctly reflected in the build folder.

10:03

🔧 Debugging and Testing the Chrome Extension

The speaker explores the possibility of creating a Chrome extension without a background script, confirming that it is feasible. They build the extension again and attempt to load it as an unpacked extension in Chrome. After successfully loading the extension, they verify that it appears in the list of installed extensions. The speaker then delves into adding an action to the extension, such as opening a side panel when the extension icon is clicked. They encounter issues with outdated documentation and incorrect file paths but eventually find a solution involving the 'action' property in the 'manifest.json' and the use of a background script to handle the click event. Despite some initial failures, they manage to get the side panel to open and close as intended.

15:03

🎉 Finalizing and Interacting with the Side Panel

In the final part, the speaker revisits the documentation to recall how to open a side panel through user interactions. They discover that the 'chrome.action.onClicked' event is used to open the side panel and set the panel's behavior to open on action click. The speaker incorporates this into their project, adding the necessary background JavaScript file to handle the event. After rebuilding the extension and refreshing it in Chrome, they successfully demonstrate the opening and closing of the side panel upon clicking the extension icon. The tutorial concludes with a call for feedback from viewers and a farewell note.

Mindmap

Keywords

💡Chrome Extension

A Chrome Extension is a software program that adds functionality to the Google Chrome web browser. In the video, the creator is demonstrating how to build a Chrome Extension using React and Webpack, showcasing the process from initialization to implementation. The theme revolves around web development and browser customization.

💡React

React is a popular JavaScript library for building user interfaces, particularly for single-page applications. It is used in the video to create the user interface of the Chrome Extension. The script mentions choosing React as the framework when setting up the project, indicating its importance in modern web development.

💡Webpack

Webpack is a module bundler that is mainly used for JavaScript applications. It is mentioned in the context of setting up the development environment for the Chrome Extension. Webpack is instrumental in optimizing and bundling the project's resources, which is a crucial step in the development process.

💡npm

npm stands for Node Package Manager, a package manager for JavaScript and the default package manager for the JavaScript runtime environment Node.js. The script refers to npm commands such as 'npm create' and 'npm install', which are used to set up the project and install dependencies, highlighting npm's role in managing project packages.

💡TypeScript

TypeScript is a superset of JavaScript that adds static types to the language. It is mentioned in the script as an option when setting up the project. The use of TypeScript in the video indicates a preference for a more structured approach to JavaScript development, offering better tooling and error checking.

💡manifest.json

The manifest.json file is a configuration file that tells the browser about the extension and how it should behave. In the video, the creation of this file is a key step in turning the React app into a Chrome Extension. The script details the necessary properties like 'manifest_version' and 'action', which are crucial for the extension's functionality.

💡Service Worker

A Service Worker is a script that the browser runs in the background, separate from a web page, and is used to enable features like offline functionality or push notifications. The video discusses the need for a service worker in the context of the Chrome Extension, indicating its role in handling background tasks.

💡Background Script

A background script is a JavaScript file that runs in the background of a Chrome Extension, performing tasks independently of web pages. The script mentions the possibility of not needing a background script for the extension, but later it is included to handle opening the side panel, demonstrating its utility in extension development.

💡Side Panel

A side panel in the context of Chrome Extensions refers to a UI element that appears on the side of the browser window. The video's main goal is to create a Chrome Extension with a side panel that can be opened and closed by clicking the extension icon. This feature is central to the extension's functionality and user interaction.

💡Action

In Chrome Extensions, an 'action' is a UI element that the user can interact with, such as a button in the toolbar. The script discusses setting up an action in the manifest.json file, which is tied to the behavior of clicking the extension icon, such as opening a side panel. This concept is integral to the user's interaction with the extension.

Highlights

Introduction to creating a Chrome extension using React and TypeScript.

Accessing the official website for creating Chrome extensions.

Instructions on initiating a new Chrome extension project with npm.

Choosing React as the framework for the Chrome extension.

Deciding between TypeScript and SWC as the transpiler.

Installation of project dependencies using npm install.

Running the development server with npm start.

Explanation of the need for a manifest.json file for Chrome extensions.

Creating a manifest.json file with version 3 specifications.

Decision on not using a popup for the Chrome extension.

Setting up the action that occurs when the extension icon is clicked.

Discussion on whether a background script is necessary for the extension.

Building the application and preparing it for the Chrome extension.

Adding permissions to the manifest.json for the extension's functionality.

Setting the correct file to be opened as a side panel in the extension.

Loading the unpacked extension in Chrome for testing.

Troubleshooting and resolving issues with the extension's side panel.

Adding a background script to handle the side panel's opening on icon click.

Final testing and confirmation of the Chrome extension's functionality.

Conclusion and call to action for feedback on the tutorial.

Transcripts

play00:01

hi guys hi guys today I'm going to show

play00:05

you how to create Chrome extension using

play00:11

re and whe so uh I'll just go ahead and

play00:17

get

play00:17

started so here is the withjs dod

play00:23

website where we can find how to get

play00:26

started so we click button get started

play00:30

and let's

play00:31

see so here is here are the instructions

play00:35

how to do that npm create we latest we

play00:39

use we use just

play00:41

npm um so I think that's many people use

play00:47

that so we just copy that and we go

play00:53

to and go and create a new

play00:57

project how do we do that we just go to

play01:00

the folder we want to have the project

play01:05

to be

play01:07

created and let's let's paste this

play01:11

command in

play01:15

here and let's see what

play01:20

happens so it asks for the project name

play01:25

let's um

play01:27

Chrome extension

play01:34

test now we want to choose from

play01:37

here which framework or Library we want

play01:41

to use and we choose

play01:44

react

play01:46

now we can use typescript typescript

play01:50

plus s swc I'm not sure what is

play01:58

that look like a already I

play02:01

already Googled it but

play02:08

actually so um it's kind of a new

play02:13

transpiler or transpiler

play02:24

for okay never mind I'll just use

play02:28

typescript regular trans

play02:30

spiler and the project is installed

play02:34

let's go and open that

play02:40

Library

play02:47

umm

play02:49

okay let's open it let's terminate

play02:52

terminate all the the current processes

play02:57

which I

play02:58

have and now we have the react

play03:03

we installed

play03:08

let's let's actually install the

play03:12

packages because it should actually say

play03:15

what should we do next

play03:18

after we create latest okay here it is

play03:23

go to your

play03:24

project and now we should run npm

play03:28

install

play03:30

and then npm npm run there it just to

play03:34

run the

play03:36

regular the regular

play03:38

um

play03:45

server but for Chrome extension we are

play03:48

going to use something else so here it

play03:52

is npm

play03:54

packages have been successfully

play03:56

installed and now we can run this

play04:00

command this command is actually uh

play04:03

located in

play04:04

here so you can run it

play04:08

from package Json itself if you have a

play04:12

uh jet brains webstorm I think it should

play04:16

be similar with W

play04:19

code so now if you open that it should

play04:24

it should show you here the

play04:27

default page which is functional as you

play04:30

can

play04:32

see but how to make it work as a Chrome

play04:36

extension that's the

play04:38

question um so I plan to make this as a

play04:43

Chrome extension to be opened on the

play04:46

left sa side panel like

play04:52

here inside of here it's possible to

play04:55

open extensions if they support

play04:58

that uh so let's do

play05:01

that at least we are running this and

play05:08

um actually to make the Chrome extension

play05:12

we just need to add one file I believe

play05:16

at least one file called manifest

play05:21

Json so um let's let's do that but I'll

play05:26

go ahead and ask chat GPT to provide

play05:29

Prov me a

play05:32

u basic

play05:33

manifest manifest Json

play05:40

example so I don't search

play05:46

for for that

play05:49

provide a

play05:51

manifest Json to create a Chrome

play05:58

extension

play06:13

y okay so as you can

play06:19

see that is Manifest version number

play06:22

three that's the latest one I

play06:24

believe and uh let's use it we can

play06:29

actually C copy the code here and we can

play06:33

go and create it inside of

play06:37

our I

play06:42

believe

play06:45

inside not

play06:47

SRC but in the end result we would need

play06:50

that manifest to be

play06:55

included into the dist

play06:58

folder so let's

play07:00

see um popup we don't need popup at

play07:04

least I don't need a

play07:06

popup uh but that's an action the action

play07:10

which is a what is going to happen when

play07:13

you click uh the the extension button

play07:19

default icon we don't have that and we

play07:22

will change that to the sidebar uh HTML

play07:27

file now background

play07:30

do we really have a

play07:33

background uh we don't have a background

play07:36

JavaScript because we don't have

play07:38

anything to run at least for my

play07:41

needs permission storage

play07:44

okay uh what should we do

play07:51

next we actually should make a

play07:55

build of

play07:58

the

play08:00

application the whole

play08:05

application let's

play08:07

see okay uh and I believe that

play08:11

everything we put in here into public

play08:14

folder it should also be inside of build

play08:18

folder let's try to build it

play08:21

again and let's

play08:24

see okay manifest exist now and the only

play08:28

thing we we need to do right now is to

play08:31

connect our manifest with

play08:35

the sidebar script in here we don't run

play08:40

content scripts too I just need a

play08:45

sidebar icons we don't have any icons

play08:48

for

play08:50

now

play08:52

okay so default popup index no it's not

play08:55

a

play08:57

popup um let's Google Google for um

play09:01

Google Chrome side panel

play09:06

API let's

play09:14

see okay so permissions we need to add

play09:17

permissions uh

play09:23

permissions not storage but side panel

play09:26

we delete it in

play09:28

here

play09:30

permissions we are okay and to okay so

play09:36

we need to set the correct file to be

play09:40

opened as a side panel so that's how we

play09:44

do that we write side panel default path

play09:47

and in our case it's not side panel HTML

play09:50

it should be index HTML

play09:55

right okay now it's

play09:57

done and we need to to

play10:00

[Music]

play10:02

trigger okay we'll do that

play10:07

later background can we do without

play10:11

background can I have manifest Json

play10:16

without background

play10:19

script question mark certainly you can

play10:23

create manifest

play10:24

without okay that's great I like it

play10:33

and now let's build it again and let's

play10:35

try to run this extension and

play10:41

see is Miracle happens is it going to

play10:44

run from the first

play10:47

time so let's go in here

play10:50

and uh extensions manage

play10:53

extensions load unpacked so let's see

play10:57

where did

play10:58

we

play11:01

where do we locate it uh I will copy

play11:04

path

play11:06

reference like

play11:08

here and let's load unpacked let's paste

play11:12

it to go directly there let's use this

play11:16

folder and we are ready to choose the

play11:20

folder it doesn't give any errors that's

play11:24

good and once we uploaded the extension

play11:29

we you should actually see

play11:30

it in in the list and if we hit that

play11:35

okay you can see

play11:37

that it's up and running it's working

play11:42

functional so uh we are ready to

play11:46

continue how do we continue I would add

play11:53

action I would add

play11:56

action I mean I want that extension to

play12:00

be

play12:00

opened when I hit this icon so how do we

play12:06

do that there

play12:07

is um there is something called action

play12:11

default popup I'm not

play12:14

sure is default

play12:16

popup let's see

play12:19

action default

play12:22

title

play12:24

uh on action click okay let's

play12:28

see

play12:35

so let's try this default popup

play12:39

HTML

play12:47

H um I actually forgot how to do that

play12:50

how

play12:51

to

play12:53

open side panel on icon

play12:58

click

play13:05

okay so we have size panel in

play13:13

here okay the the

play13:16

the information is a bit outdated

play13:24

because let's see so the default pop up

play13:27

I actually have that in another

play13:29

extension let me try to remember so sta

play13:35

spin I have

play13:39

that that's another project I worked on

play13:42

it's built on top of

play13:45

angular but let's find manifest

play13:50

Json

play13:51

[Music]

play13:55

and okay default title click to open

play13:58

Panel

play14:00

but this is not going to open it by

play14:05

itself anyways let's let's see I think

play14:10

what can open the default panel panel

play14:15

is background

play14:21

page so background let's see where where

play14:25

do we have background okay background Js

play14:31

[Music]

play14:44

and H that's weird okay let me try to

play14:48

build it again and let's see whether

play14:53

this uh

play14:55

works we'll need to refresh let's see no

play15:00

it doesn't

play15:03

work

play15:07

H

play15:14

um okay let's see the docks because I I

play15:18

totally forgot how

play15:20

to open the side

play15:27

panel

play15:31

okay you can open the side panel through

play15:34

user interactions using side panel open

play15:36

and set panel Behavior suest action

play15:40

click okay so I think we need to use

play15:43

this one and see in my old

play15:46

project okay yeah we can see

play15:49

that we set this

play15:52

setting and I believe we should

play15:56

also have our um

play16:01

[Music]

play16:02

background JS oh not Json

play16:07

JS and

play16:10

let's add

play16:13

that worker service

play16:17

worker right something like that we will

play16:21

need to build it

play16:24

again let's try to refresh

play16:27

it

play16:29

and let's try to

play16:33

open now there are

play16:35

errors unrecognized service worker okay

play16:39

let's

play16:41

see um how it looks is

play16:46

it

play16:49

background all right that's

play16:57

funny

play17:01

let's refresh it again and let's try to

play17:04

click okay it works and when we hit

play17:07

again it closes

play17:10

that's what I I

play17:13

need and it's working great great great

play17:18

great that's actually it

play17:21

guys I hope that you find

play17:25

this I don't know instruction or video

play17:29

useful leave me a comments to this video

play17:33

so I know your opinion thank you

play17:40

bye

Rate This

5.0 / 5 (0 votes)

Ähnliche Tags
Chrome ExtensionReact TutorialWeb DevelopmentJavaScriptManifest FileTypeScriptnpm CommandsSide PanelExtension IconsUser Interaction
Benötigen Sie eine Zusammenfassung auf Englisch?