Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 | |
| 5 /** | |
| 6 * @constructor | |
| 7 */ | |
| 8 let SelectToSpeakOptionsPage = function() { | |
| 9 this.init_(); | |
| 10 }; | |
| 11 | |
| 12 SelectToSpeakOptionsPage.prototype = { | |
| 13 /** | |
| 14 * Translate the page and sync all of the control values to the | |
| 15 * values loaded from chrome.storage. | |
| 16 */ | |
| 17 init_: function() { | |
| 18 this.addTranslatedMessagesToDom_(); | |
| 19 this.populateVoiceList_('voice'); | |
| 20 window.speechSynthesis.onvoiceschanged = (function() { | |
|
David Tseng
2017/03/29 22:24:46
No need for outer closure here.
dmazzoni
2017/03/30 20:19:17
Done.
| |
| 21 this.populateVoiceList_('voice'); | |
| 22 }).bind(this); | |
| 23 this.syncSelectControlToPref_('voice', 'voice'); | |
| 24 this.syncSelectControlToPref_('rate', 'rate'); | |
| 25 }, | |
| 26 | |
| 27 /** | |
| 28 * Processes an HTML DOM, replacing text content with translated text messages | |
| 29 * on elements marked up for translation. Elements whose class attributes | |
| 30 * contain the 'i18n' class name are expected to also have an msgid | |
| 31 * attribute. The value of the msgid attributes are looked up as message | |
| 32 * IDs and the resulting text is used as the text content of the elements. | |
| 33 * @private | |
| 34 */ | |
| 35 addTranslatedMessagesToDom_: function() { | |
| 36 var elts = document.querySelectorAll('.i18n'); | |
| 37 for (var i = 0; i < elts.length; i++) { | |
| 38 var msgid = elts[i].getAttribute('msgid'); | |
| 39 if (!msgid) { | |
| 40 throw new Error('Element has no msgid attribute: ' + elts[i]); | |
| 41 } | |
| 42 var translated = chrome.i18n.getMessage('select_to_speak_' + msgid); | |
| 43 if (elts[i].tagName == 'INPUT') { | |
| 44 elts[i].setAttribute('placeholder', translated); | |
| 45 } else { | |
| 46 elts[i].textContent = translated; | |
| 47 } | |
| 48 elts[i].classList.add('i18n-processed'); | |
| 49 } | |
| 50 }, | |
| 51 | |
| 52 /** | |
| 53 * Populate a select element with the list of TTS voices. | |
| 54 * @param {string} selectId The id of the select element. | |
| 55 * @private | |
| 56 */ | |
| 57 populateVoiceList_: function(selectId) { | |
| 58 chrome.tts.getVoices(function(voices) { | |
| 59 var select = document.getElementById(selectId); | |
| 60 select.innerHTML = ''; | |
| 61 var option = document.createElement('option'); | |
| 62 option.voiceName = null; | |
| 63 option.innerText = option.voiceName; | |
| 64 | |
| 65 voices.forEach(function(voice) { | |
| 66 voice.voiceName = voice.voiceName || ''; | |
| 67 }); | |
| 68 voices.sort(function(a, b) { | |
| 69 return a.voiceName.localeCompare(b.voiceName); | |
| 70 }); | |
| 71 voices.forEach(function(voice) { | |
| 72 if (!voice.voiceName) | |
| 73 return; | |
| 74 var option = document.createElement('option'); | |
| 75 option.voiceName = voice.voiceName; | |
| 76 option.innerText = option.voiceName; | |
| 77 select.add(option); | |
| 78 }); | |
| 79 if (select.updateFunction) { | |
| 80 select.updateFunction(); | |
| 81 } | |
| 82 }); | |
| 83 }, | |
| 84 | |
| 85 /** | |
| 86 * Given the id of an HTML select element and the name of a chrome.storage | |
| 87 * pref, sync them both ways. | |
| 88 * @param {string} selectId The id of the select element. | |
| 89 * @param {string} pref The name of a chrome.storage pref. | |
| 90 */ | |
| 91 syncSelectControlToPref_: function(selectId, pref) { | |
| 92 var element = document.getElementById(selectId); | |
| 93 | |
| 94 function updateFromPref() { | |
| 95 chrome.storage.sync.get(pref, function(items) { | |
| 96 var value = items[pref]; | |
| 97 for (var i = 0; i < element.options.length; ++i) { | |
| 98 if (element.options[i].value == value) { | |
| 99 element.selectedIndex = i; | |
| 100 break; | |
| 101 } | |
| 102 } | |
| 103 }); | |
| 104 } | |
| 105 | |
| 106 element.addEventListener('change', function() { | |
| 107 var newValue = element.options[element.selectedIndex].value; | |
| 108 var setParams = {}; | |
| 109 setParams[pref] = newValue; | |
| 110 chrome.storage.sync.set(setParams); | |
| 111 }); | |
| 112 | |
| 113 element.updateFunction = updateFromPref; | |
| 114 updateFromPref(); | |
| 115 chrome.storage.onChanged.addListener(updateFromPref); | |
| 116 } | |
| 117 }; | |
| 118 | |
| 119 new SelectToSpeakOptionsPage(); | |
| OLD | NEW |