亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
About Page to Voice Extension
Basics of Chrome Extension
Writing a page to voice Chrome extension
Conclusion
Text to Speech Chrome Extension FAQ
How to install the text to voice Chrome extension?
Can I customize my voice in my text to speech Chrome extension?
Is the text to voice Chrome extension free to use?
Can I use the text to voice Chrome extension offline?
How to use the text to speech Chrome extension?
Can I use the text to voice Chrome extension on any website?
Is my data safe in text to speech Chrome extension?
Can I change the speed of speech in the text to speech in Chrome extension?
Can I use the text to voice Chrome extension in other browsers?
Can I use the text to voice Chrome extension on my mobile device?
Home Web Front-end JS Tutorial Create a Text-to-Speech Chrome Extension

Create a Text-to-Speech Chrome Extension

Feb 18, 2025 am 11:30 AM

Create a Text-to-Speech Chrome Extension

Core points

This article explains how to create a Chrome browser text-to-speech (TTS) extension that uses HTML5 voice synthesis API or third-party API to convert highlighted text or clipboard content into speech.

Chrome extensions usually contain manifest files (metadata files), images (such as extension icons), HTML files, JavaScript files, and other resources (such as style sheets).

TTS extension waits for the user to click on their icon or press a specific hotkey (Shift Y), and then converts the highlighted text or clipboard content to voice.

The code for the extension includes background scripts and content scripts, permissions to access active tags and user clipboards, as well as checking highlighted text or clipboard content, initializing extensions, adding hotkeys, and converting text to voice method.

If the HTML5 Voice Synthesis API is not available, the extension will use a third-party API such as Voice RSS to convert text to speech. The extension also includes a bug fix to fix the problem that Chrome stops pronunciation after 200-300 words.

This article was peer-reviewed by Marc Towler. Thanks to all the peer reviewers of SitePoint to make the content of SitePoint perfect!

Text to speech (also known as speech synthesis or TTS) is a way of artificially producing human speech. This is nothing new, according to Wikipedia, people have tried to create machines that can produce human voice for at least a thousand years.

TTS is becoming more and more common in our lives today and everyone can benefit from it. We will demonstrate this by creating a Chrome extension that converts text to speech. HTML5 brings us a speech synthesis API that allows any web application to convert arbitrary text strings into speech and play to users for free.

Chrome extensions usually contain the following:

  1. Talent file (required file containing metadata)
  2. Image (such as icon for extension)
  3. HTML file (for example, a popup window that appears when the user clicks on the extension's icon)
  4. JavaScript files (such as content and/or background scripts that will be explained later)
  5. Any other resources that the application may use (such as style sheets)

About Page to Voice Extension

Due to the popularity of Chrome and the rise of TTS, we will create a Chrome extension that converts text to voice. The extension will wait for the user to click on their icon or press a special hotkey (Shift Y), and then try to find what the user highlights on the page they are currently viewing, or try to find what is copied to their clipboard. If anything is found, it will first try to convert it to speech using the HTML5 speech synthesis API, and if that API is not available, a third-party API is called.

Basics of Chrome Extension

Each Chrome extension requires a file named manifest.json. The manifest is a JSON file containing data that is critical to the application, from the extension's name, description, icon, and author, to the data that defines the extension's requirements - which websites should the extension be able to be on Run (these will be permissions that the user must grant) or what files to run when the user browses a specific website.

{
  "manifest_version": 2,

  "name": "Page to Speech",
  "description": "This extension will produce English speech to whatever text you highlight on a webpage. Highlight text and click the extension's icon",
  "author": "Ivan Dimov",
  "version": "1.0",
  "icons": { 
    "16": "icon16.png",
    "48": "icon48.png",
    "128": "icon128.png"
  },

Our list begins with the name, description, author, version, and icon of the extension. You can provide many icons with different sizes in the icons object.

 "background": {
    "scripts": ["background.min.js"]
  },
  "content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*"],
      "js": [ "polyfill.min.js", "ext.min.js"],
      "run_at": "document_end"
    }],

Then, we define a background script called background.min.js in the background object (note that we are using a minimization file). Background scripts are long-running scripts that will continue to run until the user's browser is closed or the extension is disabled.

After

we have an array of content_scripts that instruct Chrome to load two JavaScript files on each website request due to wildcards "http://*/*" and "https://*/*"" and "https://*/*"" . Unlike background scripts, content scripts can access the DOM of the actual website the user is visiting. Content scripts can both read and modify the DOM of any web page embedded in it. Therefore, our polyfill.min.js and ext.min.js will be able to read and modify all data on each web page .

  "browser_action": {
    "default_icon": "speech.png"
  },
   "permissions": [
     "activeTab",
     "clipboardRead"
    ]
}

Wait! We also have an array called permissions, which we request to access only the web page (activity tag) currently opened by the user. We also request another permission called clipboardRead, which will allow us to read the user's clipboard (so we can convert its contents into voice).

Writing a page to voice Chrome extension

First, we create our only background script that hooks up an event listener that will fire when the user clicks on the extension's icon. When this happens, we will call the sendMessage function, which uses the chrome.tabs.sendMessage(tabId, message, callback) method to send a message to our content script (the content script can read the DOM and find out what the user highlights. content and/or content placed by the user on the clipboard). We use the chrome.tabs.query method to send a message to the currently opened tab page - because this is what we are interested in and what we are able to access - the parameters of the method include a callback function that will use the following: Query the parameter call for matching tab pages.

chrome.browserAction.onClicked.addListener(function (tab) {
    //fired when the user clicks on the ext's icon
    sendMessage();
});
function sendMessage() {
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
    chrome.tabs.sendMessage(tabs[0].id, {action: "pageToSpeech"}, function(response) {});
  });
}

Now, the more verbose thing is our content script. We create an object to hold some data related to the extension and then define our initialization method.

initialize: function() {
    if (!pageToSpeech.hasText()) { return;}
    if (!pageToSpeech.trySpeechSynthesizer()) {
        pageToSpeech.trySpeechApi();
    }
},

This method checks whether the user is not highlighted with text or nothing in the clipboard, and in this case it is only returned. Otherwise, it will try to generate speech using the HTML5 speech synthesis API. If this fails, it will eventually try to use a third-party API.

How to check text performs several actions. It tries to get an object containing highlighted text using the built-in getSelection() method and convert it into a text string using toString(). Then, if the text is not highlighted, it will try to find the text in the user's clipboard. It does this by adding an input element to the page, focusing it, triggering a paste event with execCommand('paste'), and then saving the text pasted into that input in a property. Then it clears the input. In either case, it returns what it found.

{
  "manifest_version": 2,

  "name": "Page to Speech",
  "description": "This extension will produce English speech to whatever text you highlight on a webpage. Highlight text and click the extension's icon",
  "author": "Ivan Dimov",
  "version": "1.0",
  "icons": { 
    "16": "icon16.png",
    "48": "icon48.png",
    "128": "icon128.png"
  },

To enable the user to run text-to-speech conversion using hotkeys (hard coded as Shift Y), we initialize an array and set up an event listener for the onkeydown and onkeyup events. In the listener, we store an index corresponding to the keyCode of the key pressed, which is derived from the comparison result of the e.type event type and keydown, and is a boolean value. Therefore, whenever a key is pressed, the value of the corresponding key index will be set to true, and whenever a key is released, the value of the index will be changed to false. So if both indexes 16 and 84 hold true values, we know that the user is using our hotkeys, so we will initialize the text to speech conversion.

 "background": {
    "scripts": ["background.min.js"]
  },
  "content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*"],
      "js": [ "polyfill.min.js", "ext.min.js"],
      "run_at": "document_end"
    }],

To convert text to speech, we rely on the trySpeechSynthesizer() method. If the HTML5 speech synthesis exists in the user's browser (window.speechSynthesis), we know that the user can use it, so we check if the speech is currently running (we know if it is running through the pageToSpeech.data.speechInProgress boolean). If the voice is in progress, we will stop the current voice (because trySpeechSynthesizer will start a new voice, we do not want to make two sounds at the same time). We then set speechInProgress to true, and whenever the speech is finished, we set the property to a false value again.

Now, I don't want to elaborate on why we use speechUtteranceChunker, but it's a bug fix related to Chrome stopping speech synthesis after emitting 200-300 words. Basically, it splits our text string into many smaller chunks (120 words in our case) and calls the speech synthesis API using one block after another.

  "browser_action": {
    "default_icon": "speech.png"
  },
   "permissions": [
     "activeTab",
     "clipboardRead"
    ]
}

Finally, if the HTML5 Voice Synthesis API is not available, we will try an API. We have the same properties to know if we need to stop the already running audio. We then create a new Audio object directly and pass it the URL of the desired API endpoint, as the demo API we selected directly streams the audio. We just have to pass the API key and the text to be converted. We also check if the audio triggers an error. In this case, we just need to show the user an alert that we cannot help at this time (we test the code for this specific API, Voice RSS, allowing 300 requests on the free hierarchy).

{
  "manifest_version": 2,

  "name": "Page to Speech",
  "description": "This extension will produce English speech to whatever text you highlight on a webpage. Highlight text and click the extension's icon",
  "author": "Ivan Dimov",
  "version": "1.0",
  "icons": { 
    "16": "icon16.png",
    "48": "icon48.png",
    "128": "icon128.png"
  },

Finally, outside of any local scope, we call the addHotkeys method, which will start waiting for the user to press the correct hotkey, and we set up a listener that will wait for the message to be received from the background script. If you receive the correct message (speakHighlight) or press the hotkey, we will initialize the text to speech conversion object.

 "background": {
    "scripts": ["background.min.js"]
  },
  "content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*"],
      "js": [ "polyfill.min.js", "ext.min.js"],
      "run_at": "document_end"
    }],

Conclusion

Voice, we have a nice Chrome extension that converts text to voice. The concept here can be used to create Chrome extensions for different purposes. Have you built any interesting Chrome extensions, or do you want to build one? Please let me know in the comments!

If you like this idea and want to develop it further, you can find the complete code in our GitHub repository. If you want to test it, you can find a production version of the extension in the Chrome Web Store.

References: http://ipnx.cn/link/b8b0e04211dce1c104dfcdb685c9b9ad http://ipnx.cn/link/e417baa9cdf34202f71b55a27da899e8

Text to Speech Chrome Extension FAQ

How to install the text to voice Chrome extension?

Installing the Text-to-Speech Chrome extension is an easy process. First, open your Google Chrome browser and navigate to the Chrome Web Store. In the search bar, enter the name of the extension you want to install, such as "Read Aloud" or "Text-to-Speech (TTS)". Click the extension from the search results and click the "Add to Chrome" button. A pop-up window will appear to ask for confirmation and click "Add Extension". The extension will be installed and an icon will appear on your browser toolbar.

Can I customize my voice in my text to speech Chrome extension?

Yes, most text to speech Chrome extensions allow you to customize your speech. You can usually choose from a variety of voices, including male and female voices in different accents and languages. To customize your voice, click the extension icon on the browser toolbar and navigate to the Settings or Options menu. Here you should find options to change voice, speed, tone, and volume.

Is the text to voice Chrome extension free to use?

Many text-to-speech Chrome extensions are free to use, but some may charge a small fee to offer advanced features. These advanced features may include other voice, ad-free use, or saving audio files. Be sure to check the details of the extension in the Chrome Web Store before installing.

Can I use the text to voice Chrome extension offline?

Some text to voice Chrome extensions can be used offline, but not all extensions can do it. It depends on how the extension is designed. If offline use is important to you, check the description of the extension in the Chrome Web Store or the settings for the extension after installation.

How to use the text to speech Chrome extension?

To use the Text to Speech Chrome extension, first, navigate to the web page you want to read aloud. Then, click on the extension icon on the browser toolbar. Some extensions will immediately start reading the page aloud, while others may require you to select the text you want to read. You can usually use controls in the extension pop-up window to pause, resume, or stop reading.

Can I use the text to voice Chrome extension on any website?

Most text to voice Chrome extensions should work on any website, with exceptions possible. Some websites may have compatibility issues with certain extensions, or extensions may not be able to read certain types of content, such as images or videos. If you have problems, try using a different extension or contact the developer of the extension for support.

Is my data safe in text to speech Chrome extension?

Most text-to-speech Chrome extensions should respect your privacy and will not collect or share your data without your consent. However, it is best to check the extension's privacy policy before installing. If you are not satisfied with this policy, consider looking for other extensions.

Can I change the speed of speech in the text to speech in Chrome extension?

Yes, most text to speech Chrome extensions allow you to adjust the speed of your speech. This can usually be done in the settings or options menu of the extension. You can usually choose a range of speeds, from very slow to very fast.

Can I use the text to voice Chrome extension in other browsers?

Text to Speech Chrome extension is designed to run in Google Chrome and may not run in other browsers. However, many extension developers will also create versions of their extensions for other browsers, such as Firefox or Edge. Please check the developer's website or the relevant extension store for these browsers to see if there are any version available.

Can I use the text to voice Chrome extension on my mobile device?

Some text to voice Chrome extensions may work for Chrome on Android or iOS, but not all extensions are available. It depends on how the extension is designed. If mobile usage is important to you, check the description of the extension in the Chrome Web Store or the settings for the extension after installation.

The above is the detailed content of Create a Text-to-Speech Chrome Extension. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does garbage collection work in JavaScript? How does garbage collection work in JavaScript? Jul 04, 2025 am 12:42 AM

JavaScript's garbage collection mechanism automatically manages memory through a tag-clearing algorithm to reduce the risk of memory leakage. The engine traverses and marks the active object from the root object, and unmarked is treated as garbage and cleared. For example, when the object is no longer referenced (such as setting the variable to null), it will be released in the next round of recycling. Common causes of memory leaks include: ① Uncleared timers or event listeners; ② References to external variables in closures; ③ Global variables continue to hold a large amount of data. The V8 engine optimizes recycling efficiency through strategies such as generational recycling, incremental marking, parallel/concurrent recycling, and reduces the main thread blocking time. During development, unnecessary global references should be avoided and object associations should be promptly decorated to improve performance and stability.

How to make an HTTP request in Node.js? How to make an HTTP request in Node.js? Jul 13, 2025 am 02:18 AM

There are three common ways to initiate HTTP requests in Node.js: use built-in modules, axios, and node-fetch. 1. Use the built-in http/https module without dependencies, which is suitable for basic scenarios, but requires manual processing of data stitching and error monitoring, such as using https.get() to obtain data or send POST requests through .write(); 2.axios is a third-party library based on Promise. It has concise syntax and powerful functions, supports async/await, automatic JSON conversion, interceptor, etc. It is recommended to simplify asynchronous request operations; 3.node-fetch provides a style similar to browser fetch, based on Promise and simple syntax

JavaScript Data Types: Primitive vs Reference JavaScript Data Types: Primitive vs Reference Jul 13, 2025 am 02:43 AM

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

JavaScript time object, someone builds an eactexe, faster website on Google Chrome, etc. JavaScript time object, someone builds an eactexe, faster website on Google Chrome, etc. Jul 08, 2025 pm 02:27 PM

Hello, JavaScript developers! Welcome to this week's JavaScript news! This week we will focus on: Oracle's trademark dispute with Deno, new JavaScript time objects are supported by browsers, Google Chrome updates, and some powerful developer tools. Let's get started! Oracle's trademark dispute with Deno Oracle's attempt to register a "JavaScript" trademark has caused controversy. Ryan Dahl, the creator of Node.js and Deno, has filed a petition to cancel the trademark, and he believes that JavaScript is an open standard and should not be used by Oracle

React vs Angular vs Vue: which js framework is best? React vs Angular vs Vue: which js framework is best? Jul 05, 2025 am 02:24 AM

Which JavaScript framework is the best choice? The answer is to choose the most suitable one according to your needs. 1.React is flexible and free, suitable for medium and large projects that require high customization and team architecture capabilities; 2. Angular provides complete solutions, suitable for enterprise-level applications and long-term maintenance; 3. Vue is easy to use, suitable for small and medium-sized projects or rapid development. In addition, whether there is an existing technology stack, team size, project life cycle and whether SSR is needed are also important factors in choosing a framework. In short, there is no absolutely the best framework, the best choice is the one that suits your needs.

Understanding Immediately Invoked Function Expressions (IIFE) in JavaScript Understanding Immediately Invoked Function Expressions (IIFE) in JavaScript Jul 04, 2025 am 02:42 AM

IIFE (ImmediatelyInvokedFunctionExpression) is a function expression executed immediately after definition, used to isolate variables and avoid contaminating global scope. It is called by wrapping the function in parentheses to make it an expression and a pair of brackets immediately followed by it, such as (function(){/code/})();. Its core uses include: 1. Avoid variable conflicts and prevent duplication of naming between multiple scripts; 2. Create a private scope to make the internal variables invisible; 3. Modular code to facilitate initialization without exposing too many variables. Common writing methods include versions passed with parameters and versions of ES6 arrow function, but note that expressions and ties must be used.

What is the cache API and how is it used with Service Workers? What is the cache API and how is it used with Service Workers? Jul 08, 2025 am 02:43 AM

CacheAPI is a tool provided by the browser to cache network requests, which is often used in conjunction with ServiceWorker to improve website performance and offline experience. 1. It allows developers to manually store resources such as scripts, style sheets, pictures, etc.; 2. It can match cache responses according to requests; 3. It supports deleting specific caches or clearing the entire cache; 4. It can implement cache priority or network priority strategies through ServiceWorker listening to fetch events; 5. It is often used for offline support, speed up repeated access speed, preloading key resources and background update content; 6. When using it, you need to pay attention to cache version control, storage restrictions and the difference from HTTP caching mechanism.

Handling Promises: Chaining, Error Handling, and Promise Combinators in JavaScript Handling Promises: Chaining, Error Handling, and Promise Combinators in JavaScript Jul 08, 2025 am 02:40 AM

Promise is the core mechanism for handling asynchronous operations in JavaScript. Understanding chain calls, error handling and combiners is the key to mastering their applications. 1. The chain call returns a new Promise through .then() to realize asynchronous process concatenation. Each .then() receives the previous result and can return a value or a Promise; 2. Error handling should use .catch() to catch exceptions to avoid silent failures, and can return the default value in catch to continue the process; 3. Combinators such as Promise.all() (successfully successful only after all success), Promise.race() (the first completion is returned) and Promise.allSettled() (waiting for all completions)

See all articles