gpt4 book ai didi

javascript - 在 chrome 扩展中使用 Chrome 文字转语音

转载 作者:行者123 更新时间:2023-11-30 10:12:35 25 4
gpt4 key购买 nike

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

并且,here's the source我已经修改了这个应用程序作为一个“扩展”而不是一个应用程序。但是,tts 似乎不可用。我在我的 list 文件中的“权限”下添加了“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

感谢任何帮助。

最佳答案

根据 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() {});
});

关于javascript - 在 chrome 扩展中使用 Chrome 文字转语音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25641521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com