Description
A developer's journey through code. I build, I break, and I write about it. Explore articles on modern software development, programming tips, and more.
Embarking on the journey of building a Chrome extension is an exciting endeavor. Today, I will walk you through the comprehensive process of developing and publishing Chrome extensions, unraveling the simplicity behind this often-misunderstood domain. We will be building a practical currency converter using HTML, JavaScript, and an API (I am using currency exchange API within API Ninjas, you can Google API Ninjas). So, let us roll up our sleeves and traverse this coding adventure together. Welcome to my online space, my name is Jonathan Izuchukwu also known as SunshineIHCTS, without further introduction let us get started.
Our journey kicks off with the creation of a manifest Json file which is the backbone of our Chrome extension. This file acts as a communication tool, providing metadata to Chrome about our extension, including version details, a description, and browser actions. Let us look at the code:
// manifest.json
{
"manifest_version": 2,
"name": "Currency Converter",
"version": "1.0",
"description": "A Chrome extension for real-time currency conversion",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "index.html"
}
}
Ensure you adhere to Chrome's extension conventions for a seamless integration.
Our next step involves the creation of the HTML file, which forms the structure of the extension's pop-up page. The simplicity of the HTML code allows for easy customization. Here is a snippet:
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Currency Converter</title>
<script src="exchange.js" defer></script>
</head>
<body>
<h1>Currency Exchange Rate Converter</h1>
<input type="number" placeholder="Enter amount" id="amount">
<select id="currency">
<option value="EUR">European</option>
<option value="CAD">Canadian</option>
<option value="GBP">British</option>
<option value="JPY">Japanese</option>
</select>
<button id="convert">Convert Currency</button>
<div id="result"></div>
</body>
</html>
This HTML file sets up a simple user interface (UI) with input fields and a button for the currency conversion.
Now, let us explore the code for the JavaScript logic responsible for making our extension functional. The code below retrieves HTML elements by their IDs, fetches real-time exchange rates using an API, and updates the result dynamically.
// exchange.js
document.addEventListener('DOMContentLoaded', function () {
const amountInput = document.getElementById('amount');
const currencySelect = document.getElementById('currency');
const convertButton = document.getElementById('convert');
const resultDiv = document.getElementById('result');
const apiKey = 'YOUR_API_KEY'; // Replace with your actual API key
const apiUrl = 'https://api.apininja.com/exchange-rate?from=USD&to=';
convertButton.addEventListener('click', function () {
const amount = amountInput.value;
const selectedCurrency = currencySelect.value;
const fetchUrl = `${apiUrl}${selectedCurrency}&apikey=${apiKey}`;
fetch(fetchUrl)
.then(response => response.json())
.then(data => {
const exchangeRate = data.exchange_rate;
const result = amount * exchangeRate;
resultDiv.textContent = `Converted Amount: ${result.toFixed(2)} ${selectedCurrency}`;
})
.catch(error => console.error('Error fetching data:', error));
});
});
In this JavaScript file, we wait for the DOM to load before setting up event listeners. Upon clicking the "Convert Currency" button, the extension fetches real-time exchange rates from the API, processes the conversion, and displays the result.
As is the norm in coding, debugging is a crucial step. In our debugging process, we encountered issues related to naming conventions and the loading sequence of HTML and JavaScript files. Console logs played a pivotal role in identifying and resolving these issues.
Before proceeding to the publication stage, extensive testing is imperative. We load the unpacked extension into Chrome, test its functionality, and ensure a seamless user experience. Debugging at this stage helps catch any last-minute errors.
The pinnacle of our journey involves publishing our creation on the Chrome Web Store and this article will not be complete without this important part. Here are the detailed steps:
After following these steps you have successfully coded and published your Chrome extension. Whether it is a currency converter or another innovative project, share your creations with the community. Let me know in the comments about your experiences and the extensions you have built. Stay tuned for more coding adventures, and happy coding!
Cookies improve user experience on SunshineIHCTS. By continuing to use this website, you consent to the use of cookies in accordance with the Privacy policy.
A developer's journey through code. I build, I break, and I write about it. Explore articles on modern software development, programming tips, and more.
Comments section
You need to be logged in to comment, Login or Register.Approved comments:
No comments yet! be the first to comment