How to build and publish a Chrome extension using Javascript
Tags: javascript, beginners, webdev, tutorial
Published: 2023-06-06 22:57:32

A Chrome extension is a small software program that extends the functionality of the Google Chrome web browser. Extensions can add new features to the browser, modify existing features, or even change the way the browser looks. Chrome extensions are software programs that help to customize a user’s browsing experience.

They are designed to be easy to use and can be installed directly from the Chrome Web Store with just a few clicks. Once installed, an extension will appear as an icon in the Chrome toolbar and can be used to perform various tasks or access specific features. 

## Why are Chrome Extensions Useful?

- Improved productivity: Chrome extensions can help users save time and be more productive by automating tasks, organizing information, or providing quick access to valuable tools.

- Enhanced security: Extensions can help protect users from online threats by blocking malicious websites, tracking cookies, or alerting them to potential scams.

- Customized browsing experience: Extensions can change the way a user interacts with the web by adding new functionality or altering the appearance of websites.

- Access to exclusive features: Some extensions offer features that are not available on the web or in other browsers.

- Improved accessibility: Extensions can make the web more accessible to users with disabilities by providing text-to-speech functionality, magnifiers, or other assistive technologies.

In this article, we will be building a color picker Chrome extension using the Eyedropper Web API.

## Color Picker Chrome Extension

A color picker Chrome extension is a tool that allows users to select and identify colors on websites and applications. It typically includes a color picker interface that can be activated by clicking on the extension icon in the Chrome toolbar. When the color picker is activated, users can hover their cursor over any element on the page and the extension will display the color code for that element.

### EyeDropper Web API

The EyeDropper API is a Web API that provides a technique for creating an eyedropper tool. This tool allows users to select colors from their screen and also access the value of the color selected. 

## Let’s build a Color Picker Extension in 4 Simple Steps

### Setup a Basic HTML File

To start,create a basic HTML file with three main elements: a button that will serve as the eyedropper tool, a paragraph element to display the selected color's hexadecimal code, and a body tag to hold everything. The button will be clicked to activate the eyedropper tool, and the paragraph will show the hex code of the chosen color.

```
<!DOCTYPE html>
<html lang="en">


<head>
 <meta charset="UTF-8" />
 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 <title>Document</title>
 <link rel="stylesheet" href="style.css" />
</head>


<body>
 <button id="selectBtn">Open the colorpicker</button>
 <p>The color code is : <br> <span id="result"></span></p>
 <script src="/app.js"></script>

</body>
</html>

```

### Setup a CSS file for Basic Styling

```
body{
   width: 100%
}


button{
   background-color: #000000;
   color: #fff;
   border-radius: 10px;
   border: 1px solid #ffc0cb;
   margin-right: 1rem;
}


span {
   font-weight: bold ;
}

```
### Javascript

```
const selectBtn = document.getElementById("selectBtn");
const hexcode = document.getElementById("result");
const bgColor = document.querySelector("body");

const eyeDropper = new EyeDropper();

selectBtn.addEventListener("click", () => {
 eyeDropper.open()
   .then((colorSelectResult) => {
     hexcode.textContent = colorSelectResult.sRGBHex;
     bgColor.style.background = colorSelectResult.sRGBHex;
   })
   .catch((err) => {
     hexcode.textContent = err;
   });
});
```
- First, select the button, body, and paragraph element via the dom and assign them a variable.

- Create a new instance of an EyeDropper object and store it in the eyeDropper variable and this allows you to use the API.

- Next, add an event listener to the button element, and this event is triggered when a user clicks on the button.

- Add an event listener to the `selectBtn` element that listens for a "click" event and when the button is clicked, the event listener calls the `open()` method of the eyeDropper object.

The `open()` method returns a promise that resolves with an object containing the selected color's sRGB hexadecimal code, if the eyedropper tool is used successfully the resolved value is then used to update the text content of the `hexcode` element and the background color of the `bgColor` element.

- Lastly, you also want to catch any error that might occur while using the EyeDropper tool.

### Create a Manifest.json file
A `manifest.json` file is a JSON file that is required for every Chrome extension. It contains important information about the extension, such as its name, version number, and the permissions it requires in order to function.

The manifest.json file serves as a sort of "configuration file" for the extension and is used by the Chrome browser to understand how the extension should be installed and activated. Some of the specific things that a manifest.json file might include are:

- The extension's name and version number.
- The extension's icons and other graphical assets
- Description of the extension
- Any options or settings that the extension exposes to the user

You can either create the manifest.json from scratch or you can use a manifest.json file generator.
```
{
   "name": "Color Picker App",
   "version": "1.0.0",
   "description": "sample colors from your screen",
   "manifest_version": 3,
   "author": "Damilola ezekiel",
   "action":{
       "default_popup": "index.html",
       "default_title": "Color picker App"
   }
}
```
### Publish to Chrome

The final step in this tutorial is to publish the extension to chrome. This will make the extension available for use on the Chrome browser.

There are two ways you can publish a Chrome extension. You can either publish an extension locally or on the Chrome web store. In this article, we will be focusing majorly on publishing the extension locally.

To publish your Chrome extension locally, you’ll need to follow the steps below:

- From your Chrome browser, click on the three-dot icon.

![Navigate to extensions on chrome](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xscyhlqcmtsjsvoovdbt.png)

- Navigate to `Extensions` > `Manage Extensions`.

![Navigate to extensions on chrome](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s3aw2juollgq85z5td5a.png)

- Toggle the developer mode on.

![Toggle the developer mode on chrome](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mua84350vqycef2rvtpi.png)

- Click on the load unpacked button to import your file from your computer.

- Click on the extension bar to pin your color picker extension and it will be ready for use.


![Upload extension to chrome](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1c19rgfh5iphukk50pp.gif)
- This extension will only work in your browser. If you'd like to make it available to the public by publishing it on the Chrome Web Store, follow this link.

## Conclusion

Building a Chrome extension with JavaScript is a straightforward process that can add useful features and functionality to your web browsing experience. With just a few lines of code and a  manifest.json file, you can create an extension that can manipulate web pages, communicate with servers, and interact with the user.

With knowledge of HTML, CSS, and Javascript, you can build awesome Chrome extensions.