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.