Create A Chrome Extension in HTML CSS & JavaScript
Last Updated :
29 Jul, 2024
We are going to use HTML, CSS, and JavaScript to create a Chrome extension. Whether you're a beginner or an experienced developer, this article will walk you through the process, from conceptualization to coding and testing. By the end, you'll have the skills to craft personalized Chrome extensions, enhancing your browsing experience in the way you prefer.
What is a Chrome Extension?
A Chrome Extension is a combination of web technologies like HTML, CSS, and JavaScript resulting in a type of short and simple web application that enhances the browsing experience by customizing the user interface, observing browser events, and modifying the web. You can visit the Chrome Web Store for more examples of what extensions can do and download the ones you require for yourself.
How are they built?
You can build a Chrome extension using the same web technologies that are used to create web applications: HTML, CSS, and JavaScript. It is as easy as creating a simple web application.
What can they do?
In addition to Web APIs, Chrome extensions also have access to Chrome Extension APIs to accomplish different tasks. These extensions add a new feature to your browser while you are surfing on the internet like auto captcha solving, a to-do list, and many more. For a more detailed overview, take a look at the Develop guide.
Prerequisites:
Project Structure:
Project StructureStep 1: Create an empty folder on your device
First, we need to create an empty folder where we would be storing all the files i.e. the HTML, CSS, JavaScript, and Manifest.json files and the images for the extension icon.
Step 2 : Create an manifest.json file
Note: As mentioned in the beginning of the article that creating a chrome extension is exactly same as creating a simple web application but there is an only difference that chrome extension requires a manifest.json file where we make some configurations indicating all the necessary information . It is like an index page of a book that gives all the necessary information about the contents of the book . This file is the first file we need to create while creating a chrome extension .
{
"manifest_version": 2,
"name": "Todo List Extension",
"version": "1.0",
"description": "A simple to-do list extension",
"permissions": ["storage"],
"browser_action": {
"default_popup": "popup.html",
"default_icon": {
"16": "images/task-list.png",
"48": "images/check.png",
"128": "images/check-list.png"
}
},
"icons": {
"16": "images/task-list.png",
"48": "images/check.png",
"128": "images/check-list.png"
},
"permissions": ["storage"]
}
Explanation : This code snippet represents a manifest file for a Chrome extension, defining the basic configuration and features. The manifest file specifies essential details such as the extension's name, version, and description. It declares the required permissions, indicating that the extension needs access to storage. The extension includes a browser action, with a default popup defined in "popup.html" and icons for different sizes. The icons are associated with the extension's appearance in various contexts, like the toolbar. Overall, this manifest sets up the foundation for a simple "Todo List Extension" that manages tasks and utilizes browser storage.
Step 3: Create a popup.html file
The provided popup.html file is a user interface for the Chrome extension's popup. It consists of a simple structure with a header displaying the main title, a content section containing an input field for adding tasks, and an unordered list (<ul>) with the id "todoList" where tasks are expected to be displayed. The file references an external stylesheet (style.css) for styling purposes and includes a script tag pointing to an external JavaScript file (popup.js). The JavaScript file contains functionality to handle dynamic behavior, such as adding and displaying tasks in response to user interactions. In simple words , this HTML file serves as the visual representation of the extension's popup, allowing users to manage their tasks conveniently.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Take Note of Your Tasks</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div class="header">
<h1>Take Note of Your Tasks</h1>
</div>
<div class="content">
<div class="input-container">
<input type="text" id="taskInput" placeholder="Add a task..." />
</div>
<ul id="todoList"></ul>
</div>
<script src="popup.js"></script>
</body>
</html>
Step 4: Create a style.css file
CSS
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 0;
}
.header {
background-color: #4CAF50;
color: white;
text-align: center;
padding: 15px;
font-size: 1.5em;
width: 30rem;
}
.content {
padding: 20px;
}
.input-container {
margin-bottom: 15px;
}
input[type="text"] {
width: 100%;
padding: 10px;
box-sizing: border-box;
font-size: 16px;
}
ul {
list-style-type: none;
padding: 0;
margin: 0;
}
li {
margin: 10px 0;
display: flex;
align-items: center;
font-size: 1.2em;
}
.completed {
text-decoration: line-through;
color: #999;
}
.checkbox {
margin-right: 10px;
cursor: pointer;
}
Step 5: Create a popup.js file
JavaScript
document.addEventListener('DOMContentLoaded', function () {
// Load tasks from storage
chrome.storage.sync.get(['tasks'], function (result) {
const tasks = result.tasks || [];
updateTodoList(tasks);
});
// Handle checkbox change
document.getElementById('todoList').addEventListener('change',
function (event) {
const checkbox = event.target;
const taskId = checkbox.dataset.id;
// Update task as completed
chrome.storage.sync.get(['tasks'], function (result) {
const tasks = result.tasks || [];
const updatedTasks = tasks.map(task => {
if (task.id === taskId) {
task.completed = checkbox.checked;
}
return task;
});
// Save updated tasks
chrome.storage.sync.set({ 'tasks': updatedTasks });
// Remove completed task after 2 seconds
if (checkbox.checked) {
setTimeout(function () {
const updatedTasks = tasks
.filter(task => task.id !== taskId);
chrome.storage.sync.set({ 'tasks': updatedTasks });
updateTodoList(updatedTasks);
}, 2000);
}
});
});
// Add task on Enter key press
document.addEventListener('keypress', function (event) {
if (event.key === 'Enter') {
const input = document.getElementById('taskInput');
const taskText = input.value.trim();
if (taskText !== '') {
chrome.storage.sync.get(['tasks'], function (result) {
const tasks = result.tasks || [];
const newTask = {
id: Date.now().toString(),
text: taskText, completed: false
};
const updatedTasks = [...tasks, newTask];
chrome.storage.sync.set({ 'tasks': updatedTasks });
updateTodoList(updatedTasks);
input.value = ''; // Clear input field
});
}
}
});
// Update the displayed todo list
function updateTodoList(tasks) {
const todoList = document.getElementById('todoList');
todoList.innerHTML = '';
tasks.forEach(task => {
const listItem = document.createElement('li');
listItem.innerHTML = `
<input type="checkbox" class="checkbox" data-id="${task.id}"
${task.completed ? 'checked' : ''}>
<span class="${task.completed ? 'completed' : ''}">
${task.text}</span>`;
todoList.appendChild(listItem);
});
}
});
Step 6: Create a sub folder as images
Upload the images you would like to use as icons .To find the ones i have used , click here .
Step 7: Enable Developer Mode in Chrome browser
Open Chrome browser and go to Three dots>Extensions>Manage Extensions from the browser menu .On being redirected to the extensions page , enable Developer Mode.
Enable Developer Mode
Step 8: Upload the project folder
This is the last step . Click on Load unpacked button and and select the project folder containing all the files from your device .On fulfilling this step , you will find your extension visible in your browser. You can also pin the extension in the browser for easy access.
Output: Click on the extension
Similar Reads
Create GeeksforGeeks Clone Using HTML CSS & JavaScript
In this article, we'll make a clone of the GeeksforGeeks website. The clone includes home, about, courses, and contact pages. On the home page, we've added sections like categories, features, testimonials, and FAQs, similar to the GFG website. We named our clone GeeksforGeeks 2.0 to avoid copyright
15+ min read
How to Create a Chips Component using HTML CSS & JavaScript ?
In this article, we will see how we can create a chip component with the help of HTML, CSS, and JavaScript. In a chip component, there are basically two sections: one is the content section, and the other is the cross-button section. Both should be under one container with a proper border-radius. Pr
3 min read
Create a Resize and Compress Images in HTML CSS & JavaScript
While using the GeeksforGeeks Write Portal to write articles, we need to upload the images. As we need to resize the image as per GeeksforGeeks's requirement, we search for different tools and websites on the internet to resize and compress the image. But, as a web developer, we can create our own i
7 min read
Create an QR Code Generator Project using HTML CSS & JavaScript
Today, more than 75% of websites and apps use QR codes to share links, contact info, or event details quickly. Here, youâll learn how to make your own QR Code Generator using HTML, CSS, and JavaScript. Weâll guide you step by step, so even if youâre a beginner, you can follow along easily. By the en
3 min read
Create A Download Button with Timer in HTML CSS and JavaScript
In this article, we will discuss how to create a Download button with a timer attached to it using HTML, CSS, and Javascript.Our goal is to create a button that has a timer attached to it. The download should only start after the timer has run out, which we will achieve using the setTimeout and setI
2 min read
How to create a Color Generator using HTML CSS and JavaScript ?
In this article, we will develop a Color Generator application using HTML, CSS, and JavaScript.In this application, we have created the Multi Format color generator. Users can select Colour Types like RGB, HEX, and CMYK. Use the sliders to customize the color and Inout field in case of HEX and color
6 min read
Create a Health Tracker using HTML CSS & JavaScript
In this article, we will see how we can create a health tracker using HTML, CSS, and JavaScript.Here, we have provided the user with three inputs i.e. input water intake, exercise duration, and blood sugar levels. The user can input these daily and then the provided data is stored in the localStorag
5 min read
Create a Build A Simple Alarm Clock in HTML CSS & JavaScript
In this article, we will develop an interactive simple Alarm Clock application using HTML, CSS, and JavaScript languages.Develop an alarm application featuring date and time input fields for setting alarms. Implement comprehensive validation to prevent duplicate alarms and enforce a maximum limit of
5 min read
Create a QR Code Scanner or Reader in HTML CSS & JavaScript
In this article, we will see how we can implement a QR Code Scanner with the help of HTML CSS & JavaScript. A QR code scanner will provide the user with two options to scan the QR code either by uploading the image file of the URL to be scanned or by using the camera of your system to scan the Q
3 min read
How to Add JavaScript in HTML Document?
To add JavaScript in HTML document, several methods can be used. These methods include embedding JavaScript directly within the HTML file or linking an external JavaScript file.Inline JavaScriptYou can write JavaScript code directly inside the HTML element using the onclick, onmouseover, or other ev
3 min read