1

I am currently in the process of producing a chrome extension that features numerous accessibility tools, one such tool is Text To Speech. So far for every tool I have made use of chrome.tabs.executeScript(null, code{''}, however text to speech requires the use of chrome.tts.speak(). Is it possible to use chrome.tts.speak() within chrome.tabs.executeScript(null, code{''})?

I am able to get selected text from the web as confirmed through the use of var text = window.getSelection(); alert(text); and the text to speech tool function does work as it is able to read aloud text I input manually. The problem therefore lies in carrying the variable taken from the web to `chrome.tts.speak()'.

I have made various attempts at combining both but have been unable to do so.

Attempt 1 - return text; does not work in this context

function getText(e) {
   chrome.tabs.executeScript(null,
   {code:'var text = window.getSelection(); return text;'});
}


function speakText(e) {
  chrome.tts.speak(GetText(), {'lang': 'en-GB', 'gender': 'female', 
 'rate': 1.0});
}

Attempt 2 - 'var text' does carrying out with chrome.tabs.executeScript()

   function getText(e) {
   chrome.tabs.executeScript(null,
   {code:'var text = window.getSelection();'});
   return text;
 }


 function speakText(e) {
  chrome.tts.speak(GetText(), {'lang': 'en-GB', 'gender': 'female', 
 'rate': 1.0});
 }

Attempt 3 - The selection from the web is not stored without the use of chrome.tabs.executeScript

function getText(e) {
   var text = window.getSelection();
   return text;
}


function speakText(e) {
  chrome.tts.speak(GetText(), {'lang': 'en-GB', 'gender': 'female', 
'rate': 1.0});
}

Attempt 4 - Breaks everything

function getText(e) {
  chrome.tabs.executeScript(null,
  {code:'var text = window.getSelection(); chrome.tts.speak(text, 
{'lang': 'en-GB', 'gender': 'female', 'rate': 1.0});'});
}

Any advice would be much appreciated.

  • 1. `code:'window.getSelection().toString()'` 2. use tts in the executeScript's callback because content scripts can use only [4 chrome API](https://developer.chrome.com/extensions/content_scripts). – wOxxOm Apr 07 '18 at 14:22
  • 3. chrome API is asynchronous. Usage example: [Chrome Extension get selected text](//stackoverflow.com/a/19165930) - – wOxxOm Apr 07 '18 at 14:24
  • Thanks for the help, from your advice i have change the code to this `function getText(e) { chrome.tabs.executeScript( { code: "window.getSelection().toString();"}, function(selection) { chrome.tts.speak(selection, {'lang': 'en-GB', 'gender': 'female','rate': 1.0}); }); }` however it still does not work, do you know where I am going wrong? – Giles Goat-boy Apr 07 '18 at 17:24
  • See the types of the callback parameter in the documentation - it's an array so you need to take its 0 element. – wOxxOm Apr 07 '18 at 17:28
  • It works perfectly now thank you! – Giles Goat-boy Apr 07 '18 at 17:35
  • Possible duplicate of [Chrome Extension get selected text](https://stackoverflow.com/questions/19164474/chrome-extension-get-selected-text) – rsanchez Apr 09 '18 at 17:40

0 Answers0