Skip to content

Instantly share code, notes, and snippets.

@HelveticaScenario
Created August 25, 2015 08:07
Show Gist options
  • Save HelveticaScenario/ba24e7a425e2389b0c18 to your computer and use it in GitHub Desktop.
Save HelveticaScenario/ba24e7a425e2389b0c18 to your computer and use it in GitHub Desktop.
a script that does speech recognition and creates notifications of the transcripts.
function createListener() {
console.log('creating new listener');
var recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
var res = [];
recognition.onresult = function(event) {
for (var i=0; i < event.results.length; i++) {
var resultList = event.results[i];
if (!resultList.isFinal) {
continue;
}
for (var j=0; j < event.results[i].length; j++) {
var message = event.results[i][j].transcript;
if (res.indexOf(message) === -1){
res.push(message);
notifyMe(message);
}
}
}
};
recognition.onend = createListener;
recognition.start();
}
function notifyMe(message, timeout) {
var n;
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
console.error("This browser does not support system notifications");
return;
}
// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
n = new Notification(message);
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
n = new Notification(message);
}
});
}
setTimeout(n.close.bind(n), timeout || 5000);
// Finally, if the user has denied notifications and you
// want to be respectful there is no need to bother them any more.
}
createListener();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment