forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
130 lines (120 loc) · 4.07 KB
/
options.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
function load() {
var selectedElement = document.getElementById('selected');
var sel = window.getSelection();
sel.removeAllRanges();
var range = document.createRange();
range.selectNode(selectedElement);
sel.addRange(range);
var rateElement = document.getElementById('rate');
var pitchElement = document.getElementById('pitch');
var volumeElement = document.getElementById('volume');
var rate = localStorage['rate'] || 1.0;
var pitch = localStorage['pitch'] || 1.0;
var volume = localStorage['volume'] || 1.0;
rateElement.value = rate;
pitchElement.value = pitch;
volumeElement.value = volume;
function listener(evt) {
rate = rateElement.value;
localStorage['rate'] = rate;
pitch = pitchElement.value;
localStorage['pitch'] = pitch;
volume = volumeElement.value;
localStorage['volume'] = volume;
}
rateElement.addEventListener('keyup', listener, false);
pitchElement.addEventListener('keyup', listener, false);
volumeElement.addEventListener('keyup', listener, false);
rateElement.addEventListener('mouseup', listener, false);
pitchElement.addEventListener('mouseup', listener, false);
volumeElement.addEventListener('mouseup', listener, false);
var defaultsButton = document.getElementById('defaults');
defaultsButton.addEventListener('click', function(evt) {
rate = 1.0;
pitch = 1.0;
volume = 1.0;
localStorage['rate'] = rate;
localStorage['pitch'] = pitch;
localStorage['volume'] = volume;
rateElement.value = rate;
pitchElement.value = pitch;
volumeElement.value = volume;
}, false);
var voice = document.getElementById('voice');
var voiceArray = [];
chrome.tts.getVoices(function(va) {
voiceArray = va;
for (var i = 0; i < voiceArray.length; i++) {
var opt = document.createElement('option');
var name = voiceArray[i].voiceName;
if (name == localStorage['voice']) {
opt.setAttribute('selected', '');
}
opt.setAttribute('value', name);
opt.innerText = voiceArray[i].voiceName;
voice.appendChild(opt);
}
});
voice.addEventListener('change', function() {
var i = voice.selectedIndex;
localStorage['voice'] = voiceArray[i].voiceName;
}, false);
var defaultKeyString = getDefaultKeyString();
var keyString = localStorage['speakKey'];
if (keyString == undefined) {
keyString = defaultKeyString;
}
var testButton = document.getElementById('test');
testButton.addEventListener('click', function(evt) {
chrome.tts.speak(
'Testing speech synthesis',
{voiceName: localStorage['voice'],
rate: parseFloat(rate),
pitch: parseFloat(pitch),
volume: parseFloat(volume)});
});
var hotKeyElement = document.getElementById('hotkey');
hotKeyElement.value = keyString;
hotKeyElement.addEventListener('keydown', function(evt) {
switch (evt.keyCode) {
case 27: // Escape
evt.stopPropagation();
evt.preventDefault();
hotKeyElement.blur();
return false;
case 8: // Backspace
case 46: // Delete
evt.stopPropagation();
evt.preventDefault();
hotKeyElement.value = '';
localStorage['speakKey'] = '';
sendKeyToAllTabs('');
window.speakKeyStr = '';
return false;
case 9: // Tab
return false;
case 16: // Shift
case 17: // Control
case 18: // Alt/Option
case 91: // Meta/Command
evt.stopPropagation();
evt.preventDefault();
return false;
}
var keyStr = keyEventToString(evt);
if (keyStr) {
hotKeyElement.value = keyStr;
localStorage['speakKey'] = keyStr;
sendKeyToAllTabs(keyStr);
// Set the key used by the content script running in the options page.
window.speakKeyStr = keyStr;
}
evt.stopPropagation();
evt.preventDefault();
return false;
}, true);
}
document.addEventListener('DOMContentLoaded', load);