1// Copyright (c) 2012 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.
4
5function load() {
6  var selectedElement = document.getElementById('selected');
7  var sel = window.getSelection();
8  sel.removeAllRanges();
9  var range = document.createRange();
10  range.selectNode(selectedElement);
11  sel.addRange(range);
12
13  var rateElement = document.getElementById('rate');
14  var pitchElement = document.getElementById('pitch');
15  var volumeElement = document.getElementById('volume');
16  var rate = localStorage['rate'] || 1.0;
17  var pitch = localStorage['pitch'] || 1.0;
18  var volume = localStorage['volume'] || 1.0;
19  rateElement.value = rate;
20  pitchElement.value = pitch;
21  volumeElement.value = volume;
22  function listener(evt) {
23    rate = rateElement.value;
24    localStorage['rate'] = rate;
25    pitch = pitchElement.value;
26    localStorage['pitch'] = pitch;
27    volume = volumeElement.value;
28    localStorage['volume'] = volume;
29  }
30  rateElement.addEventListener('keyup', listener, false);
31  pitchElement.addEventListener('keyup', listener, false);
32  volumeElement.addEventListener('keyup', listener, false);
33  rateElement.addEventListener('mouseup', listener, false);
34  pitchElement.addEventListener('mouseup', listener, false);
35  volumeElement.addEventListener('mouseup', listener, false);
36
37  var defaultsButton = document.getElementById('defaults');
38  defaultsButton.addEventListener('click', function(evt) {
39    rate = 1.0;
40    pitch = 1.0;
41    volume = 1.0;
42    localStorage['rate'] = rate;
43    localStorage['pitch'] = pitch;
44    localStorage['volume'] = volume;
45    rateElement.value = rate;
46    pitchElement.value = pitch;
47    volumeElement.value = volume;
48  }, false);
49
50  var voice = document.getElementById('voice');
51  var voiceArray = [];
52  chrome.tts.getVoices(function(va) {
53    voiceArray = va;
54    for (var i = 0; i < voiceArray.length; i++) {
55      var opt = document.createElement('option');
56      var name = voiceArray[i].voiceName;
57      if (name == localStorage['voice']) {
58        opt.setAttribute('selected', '');
59      }
60      opt.setAttribute('value', name);
61      opt.innerText = voiceArray[i].voiceName;
62      voice.appendChild(opt);
63    }
64  });
65  voice.addEventListener('change', function() {
66    var i = voice.selectedIndex;
67    localStorage['voice'] = voiceArray[i].voiceName;
68  }, false);
69
70  var defaultKeyString = getDefaultKeyString();
71
72  var keyString = localStorage['speakKey'];
73  if (keyString == undefined) {
74    keyString = defaultKeyString;
75  }
76
77  var testButton = document.getElementById('test');
78  testButton.addEventListener('click', function(evt) {
79    chrome.tts.speak(
80        'Testing speech synthesis',
81        {voiceName: localStorage['voice'],
82         rate: parseFloat(rate),
83         pitch: parseFloat(pitch),
84         volume: parseFloat(volume)});
85  });
86
87  var hotKeyElement = document.getElementById('hotkey');
88  hotKeyElement.value = keyString;
89  hotKeyElement.addEventListener('keydown', function(evt) {
90    switch (evt.keyCode) {
91      case 27:  // Escape
92        evt.stopPropagation();
93        evt.preventDefault();
94        hotKeyElement.blur();
95        return false;
96      case 8:   // Backspace
97      case 46:  // Delete
98        evt.stopPropagation();
99        evt.preventDefault();
100        hotKeyElement.value = '';
101        localStorage['speakKey'] = '';
102        sendKeyToAllTabs('');
103        window.speakKeyStr = '';
104        return false;
105      case 9:  // Tab
106        return false;
107      case 16:  // Shift
108      case 17:  // Control
109      case 18:  // Alt/Option
110      case 91:  // Meta/Command
111        evt.stopPropagation();
112        evt.preventDefault();
113        return false;
114    }
115    var keyStr = keyEventToString(evt);
116    if (keyStr) {
117      hotKeyElement.value = keyStr;
118      localStorage['speakKey'] = keyStr;
119      sendKeyToAllTabs(keyStr);
120
121      // Set the key used by the content script running in the options page.
122      window.speakKeyStr = keyStr;
123    }
124    evt.stopPropagation();
125    evt.preventDefault();
126    return false;
127  }, true);
128}
129
130document.addEventListener('DOMContentLoaded', load);
131