gpt4 book ai didi

javascript - 需要麦克风访问权限的用户脚本多次提示 Chrome 中的权限

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

我为一款名为 TagPro 的游戏编写了一个在页面上运行的用户脚本,该游戏允许进行语音识别。它会监听后面跟有短语的关键词,并根据单词将短语放入不同的聊天 channel 。

我在 Chrome 的 Tampermonkey 中运行脚本扩大。我使用的语音识别库是 Annyang .

在每场比赛开始时,Chrome 都会通过如下提示确认您要允许网站使用您的麦克风:

http://tagpro-radius.koalabeast.com:8005/ wants to use your microphone. Allow/Deny

我遇到的问题是,有时在游戏进行到一半时,提示会再次出现。它不会在每场比赛中发生,但当它确实发生时,它通常会发生不止一次。我还没有注意到任何可以重现它的模式。这使得诊断非常困难,更不用说修复了。我的脚本是否存在可能导致此问题的错误?

// ==UserScript==
// @name TagPro Speech To Text
// @namespace http://www.reddit.com/u/undergroundmonorail
// @description Say a message out loud to say it into chat.
// @include http://tagpro-*.koalabeast.com:*
// @include http://tangent.jukejuice.com:*
// @include http://maptest.newcompte.fr:*
// @include http://justletme.be*
// @license MIT
// @author monorail
// @version 0.2
// ==/UserScript==

(function() {
// https://github.com/TalAter/annyang
// I couldn't figure out how to load it dynamically, so I just copypasted
// the minified version.
// @require works in Firefox, but not Chrome, and this is way easier than
// any alternative I found.
(function(a){"use strict";var b=this,c=b.SpeechRecognition||b.webkitSpeechRecognition||b.mozSpeechRecognition||b.msSpeechRecognition||b.oSpeechRecognition;if(!c)return b.annyang=null,a;var d,e,f=[],g={start:[],error:[],end:[],result:[],resultMatch:[],resultNoMatch:[],errorNetwork:[],errorPermissionBlocked:[],errorPermissionDenied:[]},h=0,i=!1,j="font-weight: bold; color: #00f;",k=/\s*\((.*?)\)\s*/g,l=/(\(\?:[^)]+\))\?/g,m=/(\(\?)?:\w+/g,n=/\*\w+/g,o=/[\-{}\[\]+?.,\\\^$|#]/g,p=function(a){return a=a.replace(o,"\\$&").replace(k,"(?:$1)?").replace(m,function(a,b){return b?a:"([^\\s]+)"}).replace(n,"(.*?)").replace(l,"\\s*$1?\\s*"),new RegExp("^"+a+"$","i")},q=function(a){a.forEach(function(a){a.callback.apply(a.context)})},r=function(){d===a&&b.annyang.init({},!1)};b.annyang={init:function(k,l){l=l===a?!0:!!l,d&&d.abort&&d.abort(),d=new c,d.maxAlternatives=5,d.continuous=!0,d.lang="en-US",d.onstart=function(){q(g.start)},d.onerror=function(a){switch(q(g.error),a.error){case"network":q(g.errorNetwork);break;case"not-allowed":case"service-not-allowed":e=!1,(new Date).getTime()-h<200?q(g.errorPermissionBlocked):q(g.errorPermissionDenied)}},d.onend=function(){if(q(g.end),e){var a=(new Date).getTime()-h;1e3>a?setTimeout(b.annyang.start,1e3-a):b.annyang.start()}},d.onresult=function(a){q(g.result);for(var c,d=a.results[a.resultIndex],e=0;e<d.length;e++){c=d[e].transcript.trim(),i&&b.console.log("Speech recognized: %c"+c,j);for(var h=0,k=f.length;k>h;h++){var l=f[h].command.exec(c);if(l){var m=l.slice(1);return i&&(b.console.log("command matched: %c"+f[h].originalPhrase,j),m.length&&b.console.log("with parameters",m)),f[h].callback.apply(this,m),q(g.resultMatch),!0}}}return q(g.resultNoMatch),!1},l&&(f=[]),k.length&&this.addCommands(k)},start:function(b){r(),b=b||{},e=b.autoRestart!==a?!!b.autoRestart:!0,h=(new Date).getTime(),d.start()},abort:function(){r(),e=!1,d.abort()},debug:function(a){i=arguments.length>0?!!a:!0},setLanguage:function(a){r(),d.lang=a},addCommands:function(a){var c,d;r();for(var e in a)if(a.hasOwnProperty(e)){if(c=b[a[e]]||a[e],"function"!=typeof c)continue;d=p(e),f.push({command:d,callback:c,originalPhrase:e})}i&&b.console.log("Commands successfully loaded: %c"+f.length,j)},removeCommands:function(a){a=Array.isArray(a)?a:[a],f=f.filter(function(b){for(var c=0;c<a.length;c++)if(a[c]===b.originalPhrase)return!1;return!0})},addCallback:function(c,d,e){if(g[c]!==a){var f=b[d]||d;"function"==typeof f&&g[c].push({callback:f,context:e||this})}}}}).call(this);

// The following code is the function for sending a chat message. This is
// how every userscript that touches chat does it. It's almost definitely
// not related to the problem.

var lastMessage = 0;

var chat = function(message, all) {
var limit = 500 + 10;
var now = new Date();
var timeDiff = now - lastMessage;
if (timeDiff > limit) {
tagpro.socket.emit("chat", {
message: message,
toAll: all
});
lastMessage = new Date();
} else if (timeDiff >= 0) {
setTimeout(chat, limit - timeDiff, chatMessage)
}
}

// Code that I wrote begins here.

var team = function(message) { chat(message, 0); };
var all = function(message) { chat(message, 1); };
var group = function(message) {
if (tagpro.group.socket) {tagpro.group.socket.emit('chat', message);}
};

commands = { 'say *message': all,
'team *message': team,
'group *message': group };

annyang.addCommands(commands);

annyang.start();

})();

最佳答案

Chrome 的语音识别实现有时会停止。为了处理这个问题,annyang 重新启动它(除非它被您或用户手动停止)。由于您的网页不使用 HTTPS,因此您授予 Chrome 在该网页上使用语音识别的权限不会持续存在,它会一次又一次地请求用户的许可。这就是为什么在页面上使用语音识别时建议使用 HTTPS 的原因。

关于javascript - 需要麦克风访问权限的用户脚本多次提示 Chrome 中的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25149645/

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