我有以下问题我轮询文件中的单词,找到后,我想使用触发函数触发匹配并将匹配定向到 molly map 函数,当前脚本使用 webKitSpeechRecon 从语音触发。
// inject fun into trigger
function callmic(){
var randomnumber=Math.floor(Math.random()*100001)
jQuery.get("https://localhost/newfile.txt?" + randomnumber, function(callingmic) {
console.log(callingmic);
console.log("Mobile App");
// route to molly
});
}
这是完整的脚本
var getSpeech = (function () {
if (window.location.protocol != "https:")
window.location.href = "https:" + window.location.href.substring(window.location.protocol.length);
if (typeof webkitSpeechRecognition === "undefined") return null;
var escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g,
optionalParam = /\((.*?)\)/g,
namedParam = /(\(\?)?:\w+/g,
splatParam = /\*\w+/g,
convertFormatToRegExp = function (format) {
format = format
.replace(escapeRegExp, "\\$&")
.replace(optionalParam, "(?:$1)?")
.replace(namedParam, function (match, optional) {
return optional ? match : "(\\w+)";
})
.replace(splatParam, "(.*?)");
return new RegExp("^" + format + "$", "i");
},
trigger = function (transcript) {
console.log(transcript);
for (var i = 0; i < molly.length; i++) {
var result = molly[i].regex.exec(transcript);
if (result && result.length) {
result.splice(0, 1);
molly[i].callback.apply(molly[i].context, result);
}
}
};
var speechRecognition = new webkitSpeechRecognition(),
molly = [],
recognizing = false;
speechRecognition.continuous = true;
speechRecognition.lang = "en-GB";
speechRecognition.onerror = function (event) {
speechRecognition.start();
$('#micset').attr('src', 'fade.png');
}
speechRecognition.onend = function (event) {
speechRecognition.start();
$('#micset').attr('src', 'fade.png');
}
$('#send').click( function() {
speechRecognition.start();
});
speechRecognition.onresult = function (event) {
var finalTranscript = ""; //, interimTranscript = "";
for (var i = event.resultIndex; i < event.results.length; i++) {
finalTranscript += event.results[i][0].transcript;
}
$('#micset').attr('src', 'fade.png');
trigger(finalTranscript.trim()); // + interimTranscript);
};
function getSpeech(properties) {
$('#micset').attr('src', 'chromemicred.png');
if (!properties) return;
for (var property in properties) {
this[property] = properties[property];
}
this.route(properties["molly"]);
}
getSpeech.prototype = {
route: function (format, callback) {
if (typeof format === "object") {
var mollyObj = format;
for (var format in mollyObj) {
this.route(format, mollyObj[format]);
}
} else {
molly.push({
regex: convertFormatToRegExp(format),
callback: this[callback] || callback,
context: this
});
}
},
// insert new trigger here from ape refresh every 2read
trigger: function (transcript) {
trigger(transcript);
},
start: function () {
if (!recognizing) {
recognizing = true;
speechRecognition.start();
}
},
stop: function () {
if (recognizing) {
recognizing = false;
speechRecognition.start();
}
}
};
return getSpeech;
})();
var mapp = new getSpeech({
molly: {
"help": "help",
"go to youtube": "youtube",
"say hello to :name": "sayHello",
"find *query on :engine": "find",
"question *words": "quests",
"search *wordsearch": "fluidsearch",
"define *lookup": "define",
"hello": "hello",
"tell me a joke" : "joke",
"joke" : "joke",
"search" : "search",
"add :item to my shopping list" : "trolley",
":item to my shopping list" : "trolley",
":item on shopping list" : "trolley",
"put :item to my shopping list" : "trolley",
"switch light on" : "pwron",
"lights on" : "pwron",
"light on" : "pwron",
"test" : "callin",
"show me text messages" : "sms",
"call alerts" : "callin",
"lights off" : "pwro",
"light off" : "pwro",
"switch off light" : "pwro",
"switch off the light" : "pwro",
"switch light off" : "pwro",
"what time is it" : "time",
"question" : "q",
"time" : "time",
"spell *mollyword" : "spell",
"play radio *station" : "one",
"radio *station" : "one",
"play music by *artist" : "music",
"find me pictures of *image" : "images",
"set timer *inmins" : "timer",
"set timer for *inmins" : "timer"
},
help: function () {
window.location.href = "help.php";
},
search: function () {
$('body').load('altx.php');
},
q: function () {
var quest = new Audio('/spk/knowjumpinnen.mp3');
quest.play();
$('body').load('altq.php');
},
hello: function () {
var hi = new Audio('/spk/hitheregen.mp3');
hi.play();
},
trolley: function (item) {
//send trolley to supermarket and store in users crandle++
$.ajax({
type: 'GET',
url: "https://xxx/spk/gu.php?filename=item" + item + "&sw=sw=1&txt=" + item + " is now on the shopping list",
success: function(resp) {
console.log("shopping list update");
var trolley= new Audio("/spk/item" + item + "en.mp3");
trolley.play();
$('#notificationsBody').html('<a id=\'listitems\' href=\'/\'>View shopping list</a>');
},
error: function() {
console.log("shopping list error");
}
});
},
time: function () {
var timelook= new Audio('/spk/lookingupresen.mp3');
timelook.play();
window.location.href = "i.php?a=web&m=1&oq=1&sw=sw%3D1&q=time";
},
pwro: function () {
$.ajax({
type: 'GET',
url: 'xxx',
success: function(resp) {
console.log("im hitting the power function");
$( "div.off" ).fadeIn( 300 ).delay( 1500 ).fadeOut( 400 );
},
error: function() {
$( "div.failure" ).fadeIn( 300 ).delay( 1500 ).fadeOut( 400 );
}
});
},
pwron: function () {
$.ajax({
type: 'GET',
url: 'xxx',
success: function(resp) {
$( "div.on" ).fadeIn( 300 ).delay( 1500 ).fadeOut( 400 );
},
error: function() {
$( "div.failure" ).fadeIn( 300 ).delay( 1500 ).fadeOut( 400 );
}
});
},
spell: function (mollyword) {
$('body').load("xxx" + mollyword + "&mp3=1");
},
music: function (artist) {
window.location.href = "xxx?search_query=" + artist + "&askquickly=1";
},
joke: function () {
var jokeArray = ["/spk/ajokeen.mp3", "/spk/ajokenameen.mp3", "/spk/ajokespaceen.mp3", "/spk/ajokeanten.mp3"];
var choice = Math.floor(Math.random() * jokeArray.length);
var aajoke = jokeArray[choice];
var ajoke = new Audio(aajoke);
var coins = new Audio('/spk/flipcoindsen.mp3');
ajoke.play();
},
sayHello: function (name) {
alert("Hi, " + (name || "there") + "!");
},
define: function (lookup) {
var defineaudio= new Audio("/spk/" + lookup + "en.mp3");
defineaudio.play();
},
one: function (station) {
var prestation= new Audio("//xxx" + station + "en.mp3");
prestation.play();
$('#instructions').html('<div id=\'stations\'><br><br><br><br><img id=\'testimgstation\' src=\'/images/bbc' + station + '.png\' /><a id=\'teststationmute\' href=\'/\'><img id=\'mute\' src=\'/images/mute.png\'></a></div>');
var oneradio= new Audio("xxx");
oneradio.play();
},
sms: function () {
smsload();
setInterval(function(){
smsload()
}, 20000);
},
callin: function () {
callload();
setInterval(function(){
callload()
}, 10000);
},
quests: function (words) {
window.open("xxx?sw=sw=1&a=web&m=1&q=" + words);
},
timer: function (inmins) {
window.location.href = "xxx" + inmins;
},
fluidsearch: function (wordsearch) {
window.open("xxx?a=web&sw=sw=1&m=1&q=" + wordsearch);
},
images: function (image) {
window.open("https://askquickly.org/i.php?a=images&sw=sw=1&m=1&q=" + image);
},
find: function (query, engine) {
window.open("xxx);
}
});
mapp.start();
function smsrefreshload(){
$('#notificationTitle').load('xxx',function () {
$(this).unwrap();
});
}
function smsload(){
$('#notificationsBody').load('xxx');
$('#notificationTitle').html('Text messages');
$('#notificationFooter').html('Speak');
$('#notificationAu').html('<span id="notification_count">SMS</span>');
}
function callrefreshload(){
$('#notificationTitle').load('xxx?demoauth=1',function () {
$(this).unwrap();
});
}
function callload(){
$('#notificationsBody').load('callin.php?demoauth=1');
$('#notificationFooter').html('Speak');
$('#notificationTitle').html('Incoming calls');
$('#notificationAu').html('<span id="notification_count">Calls</span>');
}
setInterval(function(){
callmic()
}, 5000);
// inject fun in trigger
function callmic(){
var randomnumber=Math.floor(Math.random()*100001)
jQuery.get("http://localhost/newfile.txt?" + randomnumber, function(callingmic) {
console.log(callingmic);
console.log("Mobile App");
// route to molly for action
});
}