1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 var LOG = function(msg) {
6 window.console.log(msg);
9 var embederWindowChannel = null;
11 var notifyEmbedder = function(msg_array) {
12 embederWindowChannel.postMessage(JSON.stringify(msg_array), '*');
15 var runSpeechRecognitionAPI = function() {
16 LOG('runSpeechRecognitionAPI');
17 var r = new webkitSpeechRecognition();
18 var succeeded = false;
19 r.onstart = function() {
22 notifyEmbedder(['recognition', 'onstart', '']);
25 r.onerror = function() {
30 notifyEmbedder(['recognition', 'onerror', '']);
32 r.onresult = function(e) {
33 if (!e.results || !e.results.length) {
34 notifyEmbedder(['recognition', 'unknown', '']);
37 var transcript = e.results[0][0].transcript;
38 notifyEmbedder(['recognition', 'onresult', transcript]);
43 var onPostMessageReceived = function(e) {
44 embederWindowChannel = e.source;
45 var data = JSON.parse(e.data);
46 if (data[0] == 'create-channel') {
47 runSpeechRecognitionAPI();
51 window.addEventListener('message', onPostMessageReceived);