-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.html
More file actions
113 lines (90 loc) · 3.96 KB
/
test.html
File metadata and controls
113 lines (90 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<html>
<script type="text/javascript" src="jquery-1.5.min.js"></script>
Chrome Speech Search and TTS Example v2<br>
<form id="rateform" onsubmit="setSpeechRate();">
Speech Rate: <input type="text" value="" name="rate"><input type="submit" value="Set"><br><br>
</form>
<div class="introMsg" id="introMsg"></div><br><br>
<form id="searchform" onsubmit='return searchBKS();'>
<input type="text" name="search" x-webkit-speech=true onwebkitspeechchange='searchBKS(event);'>
<input type="submit">
</form>
<br>
<div class="latestBooks" id="latestBooks"></div><br>
<div class="redirectFlag" id="redirectFlag"></div><br>
<div class="synopsys" id="synopsys"></div><br>
<script language="JavaScript">
var apiKey = ''; // get your API key from http://developer.bookshare.org by creating an account and then obtaining an PAPI key
$('#rateform input[name="rate"]').val(getSpeechRate());
var i = 0;
var introMsg = "This app enables voice search and uses TTS to speak the search results. After you hear the title and author of a book, you can hit any key to retrieve the synopsys of a book.";
// NEED TO REFACTOR AND MAKE SPEAKANDHIGHLIGHT TAKE A DIV AND CONTENT
$('div.introMsg').html(introMsg);
speakandHighlight("introMsg");
function speak() {
if ($('div.redirectFlag').text().length > 2) {
$(document).unbind("keypress");
var lastBook = i-1;
var redirectMsg = "Synopsys for " + books[lastBook].title + " by " + books[lastBook].author;
$('div.redirectFlag').html(redirectMsg);
// chrome.tts.speak(redirectMsg,{'rate': 1},function(){
$('div.synopsys').text(books[lastBook].briefSynopsis);
speakandHighlight("synopsys");
i = 0;
$('div.redirectFlag').text(" ");
// window.location = "http://www.bookshare.org/browse/book/" + books[lastBook].id;
//});
}
else {
if (i++ < books.length) {
description = books[i].title +" by " + books[i].author;
$('div.latestBooks').html("<a href='http://www.bookshare.org/browse/book/" + books[i].id + "'>" + description + "</a><br>");
chrome.tts.speak(description,{'rate': getSpeechRate(), 'desiredEventTypes': ["word"], 'onEvent': function(event){if(event.charIndex>=description.length){speak();}}});
//chrome.tts.speak(books[i].title + " by " + books[i].author,{'rate': speechRate, 'enqueue': true});
//speak();
}
}
}
function speakandHighlight(speakDiv) {
var elem = document.getElementById(speakDiv);
var sentence = elem.innerHTML;
chrome.tts.speak(sentence, {'rate': getSpeechRate(), 'desiredEventTypes': ["word"], 'onEvent': function(event){
//determine next word to highlight since the tts only returns the index of the word just spoken
var currSentence = sentence.substr(event.charIndex, sentence.length);
var words = currSentence.split(/\s/);
//determine location of next word in the sentence
var range = document.createRange();
range.setStart(elem.firstChild, event.charIndex);
range.setEnd(elem.firstChild, event.charIndex + words[0].length);
// unhighlight last word and highlight next word
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
}});
}
function searchBKS() {
$(document).keypress(function() {
$('div.redirectFlag').text("Processing...");
// NEED TO FIGURE OUT HOW TO BRING THIS COMMAND BACK
//chrome.tts.stop();
});
$('div.synopsys').text(" ");
$('div.latestBooks').text(" ");
var jqxhr = $.getJSON('http://api.bookshare.org/book/search/' + encodeURI($('#searchform input[name="search"]').val()) + '/format/json?api_key=' + apiKey, function(json) {
books = json.bookshare.book.list.result;
speak();
});
return false;
}
function getSpeechRate() {
var firstRun = (parseFloat(localStorage['speechrate']) > 0);
if (!firstRun) {
localStorage['speechrate'] = '1';
}
return (parseFloat(localStorage['speechrate']));
}
function setSpeechRate() {
localStorage['speechrate'] = $('#rateform input[name="rate"]').val();
return;
}
</script>
</html>