【问题标题】:Using Chrome text-to-speech in a chrome extension在 chrome 扩展程序中使用 Chrome 文本转语音
【发布时间】:2014-10-27 18:56:05
【问题描述】:

现在,这是一个使用 chrome 文本转语音引擎的 demo of a chrome app

还有,here's the source 我已将此应用程序修改为“扩展”而不是应用程序。 但是,似乎 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 方法出现错误。

Uncaught TypeError: Cannot read property 'speak' of undefined

感谢任何帮助。

【问题讨论】:

    标签: javascript google-chrome google-chrome-extension text-to-speech


    【解决方案1】:

    根据https://developer.chrome.com/extensions/content_scripts,内容脚本有一些限制。他们不能

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

    • 扩展(getURL、inIncognitoContext、lastError、onRequest、sendRequest)
    • i18n
    • 运行时(connect、getManifest、getURL、id、onConnect、onMessage、sendMessage)
    • 存储

    您可能希望从内容脚本向后台页面发送一条消息,如 https://developer.chrome.com/extensions/messaging#simple 中所述

    // content script
    chrome.runtime.sendMessage({toSay: "hello Vikram"}, function() {});
    
    // background page
    chrome.runtime.onMessage.addListener(function(request) {
      chrome.tts.speak(request.toSay, 
                      { rate: 0.8, onEvent: function(event) {}}, function() {});
    });
    

    【讨论】:

    • 感谢您的解决方案。这为我做到了。但是,现在我面临另一个问题。该扩展在我的开发机器上运行良好。但是,在其他机器上不起作用。只要我将打包的扩展拖放到chrome://extensions 窗口上,它就会安装它,但会立即禁用它——说从网上商店下载的唯一扩展将起作用。 In order to run an extension that has been turned off, use a pre-release version of chrome..你能建议一些解决方法吗?
    • 如果可以,只需将解压后的扩展文件夹加载到 chrome(而不是 crx)中。
    • 这是我最初的计划。但是,当您在开发人员模式下将未打包的扩展程序添加到 chrome 时,每次启动 chrome 时它都会给您一条警告消息,例如“已将未打包的第三方扩展程序添加到您的浏览器。你想禁用它吗?所以不是一个可以发布给用户的真正干净的解决方案!
    • 如果你想分享你的扩展,我推荐 Chrome Web Store。见developer.chrome.com/webstore
    • 这是一个为客户定制的解决方案,供组织内部使用。所以,我没有在网上商店发布它。
    【解决方案2】:

    我认为您必须在后台页面中使用 chrome.tts,而不是在内容脚本中。

    查看示例:https://developer.chrome.com/extensions/samples#speak-selection

    【讨论】:

    • 谢谢@judgeja。这很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    相关资源
    最近更新 更多