How to Build a Simple Audio Chrome Extension

How to Build a Simple Audio Chrome Extension

How to Build a Simple Audio Chrome Extension

Learn how one can create your personal Chrome Extension

By Julius Ngwu on Oct 18, 2023

quixy.com

What is a Chrome Extension

A Chrome Extension is a small software program program that may be put in into the Google Chrome internet browser. These extensions present further performance and options to the browser, enhancing its capabilities and customizing the consumer’s looking expertise. Chrome Extensions can carry out a variety of duties, comparable to blocking advertisements, managing passwords, offering fast entry to particular web sites or instruments, translating content material, and extra.

What we’re going to construct

I’ve a beloved poem that I get pleasure from revisiting periodically. Occasionally, I take heed to it utilizing my audio participant or discover it on YouTube, whereas different occasions I merely recite it aloud. However, as I observed that I spend a big period of time growing and debugging functions within the Chrome browser, I believed it could be handy to have fast entry to it. Consequently, I made the choice to develop a simple audio Chrome extension for this goal. In the next part, I’ll define the step-by-step course of I adopted to perform this activity

Prerequisites

The solely device you want for this venture is an IDE. I shall be utilizing vscode for the implementation.

Build Chrome Extension

Step 1:

Create a listing and title it unbowed. Then contained in the unbowed add an index.html file and paste the code under:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta title="viewport" content material="width=device-width, initial-scale=1.0" /> <title>Unbowed</title> <script src="./script.js" defer="defer"></script> <fashion> #src-btn { high: 15px; border: none; define: none; shade: #fff; cursor: pointer; font-size: 1rem; padding: 15px 25px; border-radius: 30px; background: #4a98f7; } </fashion> </head> <physique> <button id="src-btn">Go</button> </physique> </html> 

Note: you may title this file something so long as you utilize the identical title whereas configuring the manifest.json file.

This is what popups up once we click on on the extension icon. On the header a part of our html, you’ll discover we imported a script known as script.js. Let see what’s there.

Step 2:

Add one other file known as script.js. Once once more, the title could be something so lengthy you import it in your .html file utilizing the identical title. Inside script.js, paste the the code under:

const textual content = ` Out of the night time that covers me, Black because the pit from pole to pole, I thank no matter gods could also be For my unconquerable soul. In the fell clutch of circumstance, I've not winced nor cried aloud. Under the bludgeonings of probability, My head is bloody, however unbowed. Beyond this place of wrath and tears, Looms however the Horror of the shade, And but the menace of the years, Finds and shall discover me unafraid. It issues not how strait the gate, How charged with punishments the scroll, I'm the grasp of my destiny, I'm the captain of my soul. `; const playVoice = async () => { if ("speechSynthesis" in window) { var utterance = new SpeechSynthesisUtterance(); utterance.textual content = textual content; utterance.pitch = 0.2; utterance.quantity = 3; utterance.charge = 0.5; utterance.lang = "en-US"; window.speechSynthesis.converse(utterance); } else { alert("Sorry, your browser does not assist textual content to speech!"); } }; const el = doc.getElementById("src-btn"); el.addEventListener("click on", playVoice); 

In this code snippet, we’ve outlined a operate named playVoice which makes use of the SpeechSynthesis API. SpeechSynthesis serves because the management interface for text-to-speech performance, enabling functions to vocalize their textual content material. You have the pliability to customise varied parameters to fit your preferences.

To use this operate, you must present the message you wish to convert into speech, just like how we dealt with the poem. Additionally, retrieve the button component from the .html file utilizing the getElementById technique from the Document Object Model (DOM). Finally, bind the playVoice operate to execute upon a click on occasion occurring on the button.

Now we now have created an audio reader however how can we configure the Chrome browser to grasp what we would like? Let’s see

Step 3:

Create a brand new file and name it manifest.json. This is the one file it’s essential to follow the precise title. Paste the code under:

{ "manifest_version": 3, "title": "Unbowed", "description": "Remind your self that you've got come a great distance however but undeafeted and unbowed", "model": "1.0", "motion": { "default_popup": "index.html" }, "icons": { "32": "icon32.png" } } 

This JSON object describes the extension’s capabilities and configuration. As of the time I’m creating this text, Manifest Version 3 (Manifest V3) is the newest iteration of the Chrome extension platform. To be taught extra go to Manifest V3

Loading our Project

Now we’re executed with the IDE work. Let’s go forward and cargo it in builders mode.

  1. Go to the Extensions web page by getting into chrome://extensions in a brand new tab. (By design chrome:// URLs should not linkable.)
  • Alternatively, click on on the Extensions menu puzzle button and choose Manage Extensions on the backside of the menu.
  • Or, click on the Chrome menu, hover over More Tools, then choose Extensions.
  1. Enable Developer Mode by clicking the toggle swap subsequent to Developer mode.
  2. Click the Load unpacked button and choose the extension listing.

Awesome. The extension has been efficiently put in. If no extension icons have been included within the manifest, a generic icon shall be created for the extension.

We spoke about having it a click on away proper?. By default, if you load your extension domestically, it would seem within the extensions menu Puzzle. Pin your extension to the toolbar to rapidly entry your extension throughout improvement.

Testing our Extension

Click on the extension’s motion icon (toolbar icon); you must see a popup. Then click on Go to take heed to your audio

Congratulations for making it this far. You have efficiently constructed your first audio Chrome Extension.

You can entry this venture through the next hyperlink on GitHub

Conclusion

You can train better creativity when growing the audio extension. Envision operating a script that retrieves your favourite weblog, consistently updating its content material as textual content, after which having your extension convert it into spoken phrases when you concurrently have interaction in different browser actions. The prospects for what could be achieved with this expertise are boundless, and I anticipate witnessing the outstanding improvements you’ll carry to life utilizing these instruments.

Resources

Chrome for Developers

HI-FI News

through JavaScript on Medium https://ift.tt/W8Ecj1i

November 2, 2023 at 09:01AM