转载:How to Create Your Own Google Chrome Extension

If you are a Google Chrome user, you’ve probably used some extensions in the browser.

Have you ever wondered how to build one yourself? In this article, I will show you how you can create a Chrome extension from scratch.

# What is a Chrome Extension?

A chrome extension is a program that is installed in the Chrome browser that enhances the functionality of the browser. You can build one easily using web technologies like HTML, CSS, and JavaScript.

Creating a chrome extension is similar to creating a web application, but it requires a manifest.json file which we will discuss in the last section of this post.

# What Will our Chrome Extension Look Like?

Latest Covid Report of UK-Chrome Extension

As you can see, the above chrome extension displays the latest data on Coronavirus (COVID-19) in the UK. We will be looking into how to create this extension in this blog post.

Here, we will be using the https://api.coronavirus.data.gov.uk/v1/data API in order to fetch data. We will only display the latest record for the simplicity of this post.

The complete source code of this project can be found on GitHub.

# How to Create a Chrome Extension

First of all, we need to create an empty folder where we will add our HTML, CSS, and JavaScript files.

Inside the folder, let’s create an index.html file with this HTML boilerplate code:

1
2
3
4
5
6
7
8
<!DOCTYPE html>
<html>
<head>
<title>Covid-19 Stats- UK</title>
<meta charset="utf-8" />
</head>
<body></body>
</html>

Now, let’s add a link to the Bootstrap CDN in the head tag. We will be using the Bootstrap framework here so that we don’t have to write some extra CSS in this example.

1
2
3
4
5
6
7
8
<head>
<title>Covid-19 Stats- UK</title>
<meta charset="utf-8" />
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
rel="stylesheet"
/>
</head>

In the demo, we saw that the records are displayed as a table. So now we need to work on creating a table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!DOCTYPE html>
<html>
<head>
<title>Covid-19 Stats- UK</title>
<meta charset="utf-8" />
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
rel="stylesheet"
/>
</head>
<body>
<div class="container mt-3" style="width: 450px;">
<h2 class="text-center">Covid Latest Report-UK</h2>
<table class="table table-bordered">
<thead>
<tr>
<th>Date</th>
<th>Country</th>
<th>Confirmed</th>
<th>Deaths</th>
</tr>
</thead>
<tbody>
<tr>
<td id="date"></td>
<td id="areaName"></td>
<td id="latestBy"></td>
<td id="deathNew"></td>
</tr>
</tbody>
</table>
</div>
</body>
<script src="script.js"></script>
</html>

The code above creates a table with a width of 450px . There are four different headings in a table: Date , Country , Confirmed , and Deaths .

Here, you can see that each table data td has been assigned different IDs. We will be using the value of these IDs in JavaScript to update the table data. Also, here we have loaded the JavaScript in the end after loading all the HTML content.

Now, since the table has been displayed, we need to work on writing JavaScript in order to fetch data from the API.

Let’s create a script.js file and add the following code:

1
2
3
4
5
6
7
8
9
async function fetchData() {
const res = await fetch("https://api.coronavirus.data.gov.uk/v1/data");
const record = await res.json();
document.getElementById("date").innerHTML = record.data[0].date;
document.getElementById("areaName").innerHTML = record.data[0].areaName;
document.getElementById("latestBy").innerHTML = record.data[0].latestBy;
document.getElementById("deathNew").innerHTML = record.data[0].deathNew;
}
fetchData();

Now, let’s break down the above code:

  • Here we are using the async function called fetchData .
  • The data is being fetched from the https://api.coronavirus.data.gov.uk/v1/data API.
  • The JSON data is stored in a variable called record .
  • The HTML content of td with ids date , areaName , latestBy and deathNew are updated by the corresponding values of API.

If we check the browser, we will be able to see the following result.

Latest Covid Report of UK - Browser Preview

The data is being fetched from the API and it keeps on updating as soon as the data in API changes.

# Manifest.json File

As we discussed earlier, building a Chrome extension is similar to building any web application. The only difference is that the Chrome extension requires a manifest.json file where we keep all the configurations.

The manifest.json file contains all the necessary information required to build the Chrome extension. It is the first file the extension checks and everything is loaded from this single file.

Now, let’s create a manifest.json file in the root folder and add the following code:

1
2
3
4
5
6
7
8
9
10
11
{
"name": "Covid-19 Stats UK",
"version": "1.0.0",
"description": "latest covid data of UK",
"manifest_version": 3,
"author": "Sampurna Chapagain",
"action": {
"default_popup": "index.html",
"default_title": "Latest Covid Report"
}
}

Manifest.json

Our manifest.json file contains the value of name , version , description , manifest_version (3 in this case, which is the latest manifest version), author , and action fields. In the action field, there’s the value for default_popup which contains the path to the HTML file which is index.html in this example.

You can have a look here to see all configurations of a manifest.json file.

Now, since we’ve also added the manifest.json file we are ready to add this project as an extension in our Chrome browser.

For that, we need to go to Select More Tools and then choose Extensions from the browser menu as shown in the picture below:

Navigating to extensions in Chrome

After choosing Extensions, it redirects to the extensions page in Chrome. Make sure to enable the Developer mode here.

Once that’s done, you need to click the Load unpacked button which will allow us to load our project in the Chrome extension store.

Now, the extension is available in our Chrome extension store. You can also pin the extension in the browser as shown in the gif below:

Pin extension to the browser

This extension works only in your browser. If you want to publish it on the Chrome Web Store, you can follow this link.

# Conclusion

If you have some HTML, CSS, and JavaScript knowledge, you can easily build Chrome extensions. I hope after reading this blog post, you will create some cool extensions.


# Comments

index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html>
<head>
<title>Ancient Chinese Poetry</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="poem">
<h1 class="poem-title"></h1>
<p class="poem-body"></p>
</div>
</body>
<script src="script.js"></script>
</html>

style.css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
html {
color: red;
animation: color 30s linear infinite;
}

@keyframes color {
33.3% {
color: #0f0;
}
66.7% {
color: #00f;
}
}

body {
font-family: Kaiti;
margin: 2em;
border-top: 2px solid;
position: relative;
padding: 1em;
}

body:before {
content: "";
position: absolute;
display: block;
inset: 0;
opacity: 0.2;
background-color: currentColor;
background-image: linear-gradient(to bottom, currentColor, #ccc);
}

.poem {
width: 100vw;
}

.poem-title,
.poem-body {
color: black;
margin-top: 0;
}

.poem-title {
font-size: 36px;
text-align: center;
}

.poem-body {
font-size: 24px;
text-align: center;
}

poem.md

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
​```poem
桃夭

桃之夭夭,灼灼其华。之子于归,宜其室家。
桃之夭夭,有蕡其实。之子于归,宜其家室。
桃之夭夭,其叶蓁蓁。之子于归,宜其家人。
​```

​```poem
越人歌

今夕何夕兮,搴舟中流。
今日何日兮,得与王子同舟。
蒙羞被好兮,不訾诟耻。
心几烦而不绝兮,得知王子。
山有木兮木有枝,心悦君兮君不知。
​```

...

script.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
async function fetchData () {
const res = await fetch("./poem.md");
const text = await res.text();
const regexp = /```poem([\s\S]*?)```/g; // [\s\S] include \n, . not include \n
const matches = text.matchAll(regexp);
const poems = [...matches].map(m => m[1]);
let poem = poems[Math.floor(Math.random() * poems.length)]; // get random poem
poem = poem.trim().split("\n");
const poemTitle = poem.shift();
const poemBody = poem.join("\n");
document.querySelector(".poem-title").innerText = poemTitle;
document.querySelector(".poem-body").innerText = poemBody;
}
fetchData();

manifest.jsom

1
2
3
4
5
6
7
8
9
10
11
{
"name": "Ancient Chinese Poetry",
"version": "1.0.0",
"description": "Random ancient chinese poetry",
"manifest_version": 3,
"author": "Babb Chen",
"action": {
"default_popup": "index.html",
"default_title": "Ancient Chinese Poetry"
}
}
Edited on