,

Step-by-Step Guide: Developing a Chrome Extension in Typescript and Vite

Posted by






Building a Chrome Extension in Typescript and Vite – Full Tutorial

Full Tutorial | Building a Chrome Extension in Typescript and Vite

In this tutorial, we will go through the process of building a Chrome extension using Typescript and Vite. Chrome extensions are powerful tools that allow you to customize and extend the functionality of the Chrome browser. Using Typescript and Vite will help us write scalable, maintainable, and well-structured code for our extension.

Prerequisites

Before getting started, make sure you have the following prerequisites:

  • Basic knowledge of HTML, CSS, and JavaScript
  • Node.js installed on your machine
  • An updated version of Chrome browser
  • An Integrated Development Environment (IDE) of your choice

Setting up the Project

To begin, let’s create a new project folder and navigate into it:


    $ mkdir chrome-extension
    $ cd chrome-extension
    

Next, we need to initialize our project as a Node.js project by running the following command:


    $ npm init -y
    

This will create a package.json file which will keep track of our project dependencies and other details.

Now, let’s install Vite and TypeScript as dev dependencies by executing:


    $ npm install --save-dev vite typescript
    

Vite is a build tool that helps us develop our Chrome extension efficiently, while TypeScript adds static typing to our JavaScript code, making it more robust.

Creating the Manifest File

The manifest file is a crucial component of a Chrome extension as it defines the extension’s behavior, permissions, and other details. Create a new file called manifest.json in the root folder of your project, and populate it with the following content:


    {
      "manifest_version": 2,
      "name": "My Chrome Extension",
      "version": "1.0",
      "content_scripts": [
        {
          "matches": [""],
          "js": ["main.js"]
        }
      ]
    }
    

In this example, we have set the manifest version to 2 and defined the name and version of our extension. Additionally, we have specified a content script that will be injected in all URLs. We will create the main.js file later.

Creating the Main Script

Create a src folder in the root directory, and within it, create a file named main.ts. This will be our main script where we will write the logic of our extension. Replace the content of the file with the following code:


    console.log('Hello world! This is my Chrome extension!');
    

For this tutorial, we will keep it simple and just log a message to the console.

Configuring Vite

To configure Vite, create a new file called vite.config.ts in the root folder, and add the following content:


    import { defineConfig } from 'vite';

    export default defineConfig({
      build: {
        outDir: 'dist',
        emptyOutDir: true,
        rollupOptions: {
          input: './src/main.ts'
        }
      }
    });
    

This configuration specifies that Vite should output the build artifacts in the dist folder, and that the entry point for the build process is src/main.ts.

Building and Testing the Extension

To build the extension, add the following scripts to the package.json file:


    "scripts": {
      "build": "vite build",
      "dev": "vite"
    }
    

You can now run the following command to build the extension:


    $ npm run build
    

After a successful build, navigate to the Chrome browser, open the “Extensions” page by typing chrome://extensions in the address bar, and enable the “Developer mode” option. Click on “Load unpacked” and select the dist folder.

Finally, go to any website, open the developer console (Ctrl + Shift + J), and you should see the “Hello world! This is my Chrome extension!” message logged.

Conclusion

Congratulations! You have successfully built a Chrome extension using Typescript and Vite. This is just the beginning; Chrome extensions have extensive capabilities, and you can continue to enhance and customize your extension as per your requirements. Understanding the fundamentals of Chrome extension development with modern tools like Typescript and Vite will help you build powerful, feature-rich extensions that can greatly enhance the browsing experience for users.


0 0 votes
Article Rating
16 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Jeswer9
1 year ago

Deserves way more views!

Madly Jumping Exo
1 year ago

Could you please show your vite code somewhere? Trying to figure out what you did from behind your webcam and offscreen was not fun

AutomobiliBeast
1 year ago

Dude dont stop the prod. You deserve a lot more subs than this. This channel is underrated

Andy Shevchenko
1 year ago

please share the whole project. due to screen capturing issues it's unclear in some parts

Play Again
1 year ago

I had to play around few videos before I land on this, and it's bang on; explained so nicely with easy steps. Great tutorial! Thanks.

Ritav Das
1 year ago

Great tutorial , but one part I didn't understand is you told me that we need the Content Script to actually change the content of the page we are interacting with.
But here you directly modified that in the same file where we were running that popup.
One more thing why did you clear the service worker file ?

Evans Rutto
1 year ago

awesome ..👏

Godwin Salami
1 year ago

Thank you for this video but can you explain how I can pass a function state to the DOm?

olumide oni
1 year ago

thank you so much, Great video

姬霓太美
1 year ago

Thank you so much, it's an amazing tutorial for Chrome extension!!!

Baljinder Benipal
1 year ago

Phenomenal tutorial James! This video has all the relevant information required for development and the iterative approach is extremely useful. The end product is awesome!

JustIn
1 year ago

no hot reload?

JustIn
1 year ago

this is so well produced. You deserve more subs! Thanks man

Kumar Chandresh
1 year ago

Dude is managing two spellings of color in one file. Respect 🫡

Eric Avila
1 year ago

Thanks, you really help me to understand how work with "two scopes" (extension and web).
keep going!!!…

EpicKeyz Music
1 year ago

Hey, thanks for the tutorials. I'm just starting to learn how to code my own Chrome extensions and I really enjoyed your detailed step-by-step guide!

Also, I wonder how do you type so fast? Or did you just record a coding video and then recorded your explanations?