1

現在,這是一個使用鉻文本到語音引擎的demo of a chrome app在Chrome擴展中使用Chrome文本到語音

而且,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

任何幫助表示讚賞。

回答

2

https://developer.chrome.com/extensions/content_scripts,內容腳本有一定的侷限性。他們不能

使用Chrome *的API,以除外:

  • 擴展(使用getURL,inIncognitoContext,lastError,onRequest,sendRequest將)
  • 國際化
  • 運行時(連接,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() {}); 
}); 
+0

感謝您的解決方案。這爲我做了。但是,現在我正面臨另一個問題。擴展在我的開發機器上正常工作。但是,在其他機器上不起作用。只要將壓縮後的擴展名拖放到'chrome:// extensions'窗口,它會立即安裝它,但立即禁用它 - 表示從Web商店下載的唯一擴展名將可用。 [爲了運行已關閉的擴展程序,請使用Chrome的預發佈版本。](https://support.google.com/chrome/answer/2811969?p=ui_remove_non_cws_extensions&rd=1&hl=zh-CN)。可以你建議一些解決這個問題? – 2014-09-04 08:53:44

+0

如果可以,只需將解壓後的擴展文件夾加載到chrome(而不是crx)。 – 2014-09-04 12:38:32

+0

這是我最初的計劃。但是,當你在開發者模式下將一個解壓縮擴展添加到chrome中時,每次啓動chrome時,它都會給你一個警告消息,如'一個未打包的第三方擴展已經添加到你的瀏覽器。你想禁用它嗎?' 因此,不是一個可以發佈給用戶的乾淨解決方案! – 2014-09-05 07:30:07