How to create chrome extension with TypeScript?

How to create chrome extension with TypeScript
How to create chrome extension with TypeScript?

Introduction

If you are a web developer, you have probably been using JavaScript to create interactive web pages for years. But what if you want to take your JavaScript skills to the next level by creating a Chrome extension?

Creating a Chrome extension is a great way to improve your web development skills. Not only will you learn how to use the Chrome extension API, but you will also get practice working with TypeScript, a typed superset of JavaScript that can help you write more robust code.

In this post, we will walk you through the steps of creating a simple Chrome extension using TypeScript. We will start by creating a basic manifest file that defines our extension’s name, description, and permissions. Then we will create a TypeScript file that contains our extension’s code. Finally, we will package our extension into a .crx file that can be installed in Chrome.

So, if you are ready to learn how to create a Chrome extension with TypeScript, let’s get started!

What is TypeScript?

Before going to the coding part, learn a little bit about TypeScript.

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript, and it offers classes, modules, and interfaces to help you build robust components. TypeScript is one of the modern programming languages and it is the recommended language for developing Angular applications.

What are the benefits of using TypeScript?

TypeScript Benefits:

  • TypeScript is a typed superset of JavaScript, which means that it adds type annotations to your code in order to provide type information. This is beneficial because it can help you catch errors early in the development process.
  • TypeScript is open source, which means that you can use it for free and contribute to the development of the language.
  • The TypeScript team is constantly improving the language and adding new features, which means that you can always be up to date with the latest and greatest features.
  • TypeScript uses the same syntax and semantics as JavaScript, so it is easy for JavaScript developers to get started with TypeScript.
  • TypeScript is a typed superset of JavaScript. That means that it includes all the functionality of JavaScript, but with additional features that make it more robust and reliable. For example, TypeScript includes support for types, which can help you to catch errors early on in the development process.
  • TypeScript is also a superset of JavaScript, which means that it compiles to plain JavaScript. That means that you can use TypeScript on any browser, any host, and any operating system. Plus, TypeScript is open source, so you can use it for free.
  • So, what are the benefits of using TypeScript?
  • TypeScript is a typed superset of JavaScript, which means that it includes all the functionality of JavaScript, but with additional features that make it more robust and reliable.

How to create a chrome extension with TypeScript?

Assuming you have a basic understanding of TypeScript and how to create a simple extension, we will now go over how to create a chrome extension with TypeScript. This guide will assume you are using Visual Studio Code as your text editor and Google Chrome as your browser.

The first thing you need to do is create a new folder for your project. In this folder, create a tsconfig.json file and a src folder. Your tsconfig.json file should look like this:

{

    “compilerOptions”: {

        “target”: “es6”,

        “module”: “commonjs”,

        “outDir”: “./dist”,

        “allowJs”: true

    },

    “include”: [

        “./src/**/*”

    ]

}

This will tell the TypeScript compiler to target ES6, use commonjs modules, and output the compiled files to a dist folder. The allowJs option will allow us to include .js files in our project.

Next, we need to create a manifest.json file. This file is required for all chrome extensions and will tell the browser what permissions our extension needs and what files to load. Our manifest.json file will look like this:

{

    “manifest_version”: 2,

    “name”: “My Extension”,

    “version”: “1.0.0”,

    “description”: “My extension description”,

    “browser_action”: {

        “default_popup”: “popup.html”

    },

    “permissions”: [

        “activeTab”

    ]

}

The manifest version field is required and should be set to 2. The name, version, and description fields are all optional but recommended. The browser action field is necessary if you want your extension to have a popup. The default popup field should be set to the name of your popup HTML file. The permissions field is required if your extension needs any permissions. In this case, we are requesting the activeTab permission which allows our extension to access the currently active tab.

Now that we have our tsconfig.json and manifest.json files set up, we can start writing our extension code. Create a new file in the src folder called background.ts. This file will contain our extension’s background code. The background code is executed when the extension is first loaded and can run even when the extension’s popup is not open. We will use the background code to listen for messages from our popup code. Add the following code to background.ts:

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {

    if (message.type === “getTabId”) {

        sendResponse({ tabId: sender.tab.id });

    }

});

This code adds a listener for messages from our popup code. The listener will call the sendResponse callback with the tabId of the current tab when it receives a message with type “getTabId”.

Next, we need to create our popup code. Create a new file in the src folder called popup.ts. This file will contain the TypeScript code for our popup. The popup code is executed when the user clicks on the extension’s icon in the browser toolbar. Add the following code to popup.ts:

chrome.runtime.sendMessage({ type: “getTabId” }, (response) => {

    const tabId = response.tabId;

    document.getElementById(“tabId”).innerText = tabId;

});

This code sends a message to our background code with type “getTabId”. The background code will respond with the tabId of the current tab. We then set the innerText of a DOM element with id “tabId” to the tabId.

Now that we have our TypeScript code written, we need to compile it to JavaScript. Open a terminal in your project’s folder and run the following command:

tsc

This will compile your TypeScript code to JavaScript and output the files to the dist folder.

Next, we need to create our popup HTML file. Create a new file in the dist folder called popup.html. This file will contain the HTML for our popup. Add the following code to popup.html:

<!DOCTYPE html>

<html>

<head>

    <meta charset=”UTF-8″>

    <title>My Popup</title>

</head>

<body>

    <h1>My Popup</h1>

    <p>The tab id is: <span id=”tabId”></span></p>

</body>

</html>

This HTML file contains a simple heading and paragraph. The paragraph contains a span element with id “tabId“. This is where our popup code will insert the tabId.

Now that our extension code is written and compiled, we can load it into Google Chrome. Open Google Chrome and enter chrome://extensions in the address bar. This will open the Extensions page. Click the Load unpacked extension button and select the dist folder. This will load our extension into Google Chrome.

Click the extension’s icon in the browser toolbar to open the popup. You should see the tabId displayed in the popup.

And that is it! You have now created a chrome extension with TypeScript.

Conclusion

If you are looking to create a chrome extension with TypeScript, there are a few things you need to keep in mind. First, you will need to make sure you have a good understanding of both TypeScript and JavaScript. Second, you will need to be familiar with the chrome extension development process. And third, you will need to be able to use TypeScript to write your extension’s code. In addition, the groovy chrome extension development service can also help you to improve the security of your website by adding features that can help to prevent malicious code from being injected into your website. We offer an extension development service is a great way to improve the functionality of your website. By adding features that are not available in the standard version of the browser, you can make your website more user-friendly and improve the overall experience for your visitors.