Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Chrome扩展中使用Chrome文本到语音_Javascript_Google Chrome_Google Chrome Extension_Text To Speech - Fatal编程技术网

Javascript 在Chrome扩展中使用Chrome文本到语音

Javascript 在Chrome扩展中使用Chrome文本到语音,javascript,google-chrome,google-chrome-extension,text-to-speech,Javascript,Google Chrome,Google Chrome Extension,Text To Speech,现在,这里有一个使用chrome文本到语音引擎的应用程序 以及, 我已将此应用程序修改为“扩展”而不是应用程序。 但是,tts似乎不可用。 我已在清单文件的“权限”下添加了“tts” { "manifest_version": 2, "name": "Text2Speech", "version": "1", "minimum_chrome_version": "23", "icons": { "16": "icon_16.png", "128": "icon

现在,这里有一个使用chrome文本到语音引擎的应用程序

以及, 我已将此应用程序修改为“扩展”而不是应用程序。 但是,tts似乎不可用。 我已在清单文件的“权限”下添加了“tts”

{
  "manifest_version": 2,
  "name": "Text2Speech",
  "version": "1",
  "minimum_chrome_version": "23",
  "icons": {
    "16": "icon_16.png",
    "128": "icon_128.png"
  },
  "permissions": ["tts"],
  "content_scripts": [
    {
      "matches": ["http://*/*"],
      "js": ["js/jquery-1.7.2.min.js", "js/app.js"]
    }
  ]
}
以下是我目前的代码:

$(document).ready(function(){
  $(document).on("keypress", function(e) { 
    if ( e.shiftKey && ( e.keyCode === 108 || e.keyCode === 76) ) {
      console.log( "You pressed SHIFT + L" , $(prevElement).text());
      saySomething($(prevElement).text());
    }
  });
});
var prevElement = null;
document.addEventListener('mousemove',
  function(e){
      var elem = e.target || e.srcElement;
      if (prevElement!= null) {prevElement.classList.remove("mouseOn");}
      elem.classList.add("mouseOn");
      prevElement = elem;
  },true);

function saySomething(toSay) {
  chrome.tts.speak(toSay, { rate: 0.8, onEvent: function(event) {}}, function(evt) {});
}
我在
saySomething
方法中遇到错误

未捕获类型错误:无法读取未定义的属性“speak”


非常感谢您的帮助。

我认为您必须从背景页面使用chrome.tts,而不是在内容脚本中使用

请参见此示例:

根据,内容脚本有一些限制。他们不能:

使用chrome.*API,但以下情况除外:

  • 扩展名(getURL、inIncognitoContext、lastError、onRequest、sendRequest)
  • i18n
  • 运行时(connect、getManifest、getURL、id、onConnect、onMessage、sendMessage)
  • 储藏
您可能希望将内容脚本中的消息发送到后台页面,如中所述

//内容脚本
sendMessage({toSay:“hello Vikram”},function(){});
//背景页
chrome.runtime.onMessage.addListener(函数(请求){
chrome.tts.speak(request.toSay,
{rate:0.8,onEvent:function(event){},function(){});
});

感谢您的解决方案。这是为了我。但是,现在我面临另一个问题。扩展在我的开发机器上运行良好。但是,在其他机器上不起作用。我会尽快将打包的扩展拖放到
chrome://extensions
window,它会安装它,但会立即禁用它-表示只有从Web应用商店下载的扩展才能工作。你能建议一些解决方法吗?如果可以,只需将未打包的扩展文件夹加载到chrome(而不是crx)。这是我最初的计划。但是,当你在开发者模式下将一个未打包的扩展添加到chrome中时,每次你启动chrome时,它都会给你一条警告消息,比如,“一个未打包的第三方扩展已经添加到你的浏览器中。”。你想禁用它吗?所以这不是一个可以发布给用户的干净解决方案!如果你想分享你的扩展,我推荐Chrome网络商店。请参阅。这是一个为客户定制的解决方案,供组织内部使用。所以,我没有在网上商店发布。谢谢@judgeja。这很有帮助。