//! annyang //! version : 2.6.0 //! author : Tal Ater @TalAter //! license : MIT //! https://www.TalAter.com/annyang/ (function (root, factory) { "use strict"; if (typeof define === 'function' && define.amd) { // AMD + global define([], function () { return (root.annyang = factory(root)); }); } else if (typeof module === 'object' && module.exports) { // CommonJS module.exports = factory(root); } else { // Browser globals root.annyang = factory(root); } }(typeof window !== 'undefined' ? window : this, function (root, undefined) { "use strict"; /** * # Quick Tutorial, Intro and Demos * * The quickest way to get started is to visit the [annyang homepage](https://www.talater.com/annyang/). * * For a more in-depth look at annyang, read on. * * # API Reference */ var annyang; // Get the SpeechRecognition object, while handling browser prefixes var SpeechRecognition = root.SpeechRecognition || root.webkitSpeechRecognition || root.mozSpeechRecognition || root.msSpeechRecognition || root.oSpeechRecognition; // Check browser support // This is done as early as possible, to make it as fast as possible for unsupported browsers if (!SpeechRecognition) { return null; } var commandsList = []; var recognition; var callbacks = { start: [], error: [], end: [], soundstart: [], result: [], resultMatch: [], resultNoMatch: [], errorNetwork: [], errorPermissionBlocked: [], errorPermissionDenied: [] }; var autoRestart; var lastStartedAt = 0; var autoRestartCount = 0; var debugState = false; var debugStyle = 'font-weight: bold; color: #00f;'; var pauseListening = false; var isListening = false; // The command matching code is a modified version of Backbone.Router by Jeremy Ashkenas, under the MIT license. var optionalParam = /\s*\((.*?)\)\s*/g; var optionalRegex = /(\(\?:[^)]+\))\?/g; var namedParam = /(\(\?)?:\w+/g; var splatParam = /\*\w+/g; var escapeRegExp = /[\-{}\[\]+?.,\\\^$|#]/g; var commandToRegExp = function(command) { command = command.replace(escapeRegExp, '\\$&') .replace(optionalParam, '(?:$1)?') .replace(namedParam, function(match, optional) { return optional ? match : '([^\\s]+)'; }) .replace(splatParam, '(.*?)') .replace(optionalRegex, '\\s*$1?\\s*'); return new RegExp('^' + command + '$', 'i'); }; // This method receives an array of callbacks to iterate over, and invokes each of them var invokeCallbacks = function(callbacks, ...args) { callbacks.forEach(function(callback) { callback.callback.apply(callback.context, args); }); }; var isInitialized = function() { return recognition !== undefined; }; // method for logging in developer console when debug mode is on var logMessage = function(text, extraParameters) { if (text.indexOf('%c') === -1 && !extraParameters) { console.log(text); } else { console.log(text, extraParameters || debugStyle); } }; var initIfNeeded = function() { if (!isInitialized()) { annyang.init({}, false); } }; var registerCommand = function(command, callback, originalPhrase) { commandsList.push({ command, callback, originalPhrase }); if (debugState) { logMessage('Command successfully loaded: %c'+originalPhrase, debugStyle); } }; var parseResults = function(results) { invokeCallbacks(callbacks.result, results); var commandText; // go over each of the 5 results and alternative results received (we've set maxAlternatives to 5 above) for (let i = 0; i { for (let i = 0; i * var commands = { * // annyang will capture anything after a splat (*) and pass it to the function. * // e.g. saying "Show me Batman and Robin" will call showFlickr('Batman and Robin'); * 'show me *tag': showFlickr, * * // A named variable is a one word variable, that can fit anywhere in your command. * // e.g. saying "calculate October stats" will call calculateStats('October'); * 'calculate :month stats': calculateStats, * * // By defining a part of the following command as optional, annyang will respond * // to both: "say hello to my little friend" as well as "say hello friend" * 'say hello (to my little) friend': greeting * }; * * var showFlickr = function(tag) { * var url = 'http://api.flickr.com/services/rest/?tags='+tag; * $.getJSON(url); * } * * var calculateStats = function(month) { * $('#stats').text('Statistics for '+month); * } * * var greeting = function() { * $('#greeting').text('Hello!'); * } * * ```` * * ### Using Regular Expressions in commands * For advanced commands, you can pass a regular expression object, instead of * a simple string command. * * This is done by passing an object containing two properties: `regexp`, and * `callback` instead of the function. * * #### Examples: * ````javascript * var calculateFunction = function(month) { console.log(month); } * var commands = { * // This example will accept any word as the "month" * 'calculate :month stats': calculateFunction, * // This example will only accept months which are at the start of a quarter * 'calculate :quarter stats': {'regexp': /^calculate (January|April|July|October) stats$/, 'callback': calculateFunction} * } ```` * */