Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / test / data / speech / web_speech_api_test.html
blobb6dcd091cdac15c56caeeea927a5956f709cb633
1 <html>
2 <head>
3 <title>speechreco test</title>
4 <script>
5 function cls() {
6 document.getElementById('log').innerHTML = '';
9 function log(msg) {
10 var d = new Date();
11 var t = d.getHours() + ':' +
12 d.getMinutes() + ':' +
13 d.getSeconds() + '.' +
14 d.getMilliseconds();
15 var table = document.getElementById('log');
16 var row = table.insertRow(-1);
17 var cell1 = row.insertCell(-1);
18 var time = document.createElement("pre");
19 time.innerHTML = t;
20 cell1.appendChild(time);
21 var cell2 = row.insertCell(-1);
22 var message = document.createElement("pre");
23 message.innerHTML = msg;
24 cell2.appendChild(message);
27 function res2str(results) {
28 if (!results || !results.length)
29 return "<no items>";
30 var s = '';
31 for (var i = 0; i < results.length; ++i) {
32 s += "&nbsp;" + i + ":{";
33 var r = results[i];
34 s += typeof(r) + ",";
35 if (r.final)
36 s += '(final) ';
37 s += '[';
38 for (var i = 0; i < r.length; ++i) {
39 s += r.item(i).transcript + ' (' + r.item(i).confidence + ')';
40 if (i != r.length - 1)
41 s += ',<br>&nbsp;&nbsp;&nbsp;';
43 s += ']';
44 s += "}<br>";
46 return s;
49 function setup() {
50 try {
51 window.reco = new webkitSpeechRecognition();
52 } catch(e) {
53 document.write("The Web Speech API is not supported by this browser.");
54 return;
57 reco.maxAlternatives = 25;
59 // Set default handlers.
60 for (var prop in reco) {
61 if (!prop.match('^on'))
62 continue;
63 reco[prop] = function() {
64 log('on' + event.type);
68 // Set specific handlers.
69 reco.onerror = function(e) {
70 log('onerror ' + e.error);
72 reco.onresult = function(e) {
73 try {
74 log('onresult ' + res2str(e.results));
75 } catch(ex) {
76 log('onresult - exception');
80 if (reco.onresults != undefined) {
81 log('onresults exists');
84 log('created reco object');
87 function start() {
88 log('start()');
89 reco.continuous = document.getElementById('continuous').checked;
90 reco.interimResults = document.getElementById('interim').checked;
91 log('reco.continuous = ' + reco.continuous);
92 log('reco.interimResults = ' + reco.interimResults);
93 try {
94 reco.start();
95 } catch(e) {
96 log('exception: ' + e);
100 function stop() {
101 log('stop()');
102 reco.stop();
105 function abort() {
106 log('abort()');
107 reco.abort();
110 function startOther() {
111 log('startOther()');
112 var other = new webkitSpeechRecognition();
113 other.continuous = true;
115 // Set default handlers.
116 for (var prop in other) {
117 if (!prop.match('^on'))
118 continue;
119 other[prop] = function() {
120 log('other on' + event.type);
124 other.start();
126 </script>
127 </head>
128 <body onload="setup()">
129 <input type="button" onclick="cls()" value="cls">
130 <input type="button" onclick="start()" value="start()">
131 <input type="button" onclick="stop()" value="stop()">
132 <input type="button" onclick="abort()" value="abort()">
133 <input type="button" onclick="startOther()" value="startOther()">
134 <input type="checkbox" checked id="continuous">continuous
135 <input type="checkbox" checked id="interim">interimResults
136 <!-- <pre id="log"></pre> -->
137 <table id="log" width="350px" border="0"></table>
138 </body>
139 </html>