forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
83 lines (72 loc) · 2.15 KB
/
background.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
/**
* Copyright (c) 2011 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.
*/
var lastUtterance = '';
var speaking = false;
var globalUtteranceIndex = 0;
if (localStorage['lastVersionUsed'] != '1') {
localStorage['lastVersionUsed'] = '1';
chrome.tabs.create({
url: chrome.extension.getURL('options.html')
});
}
function speak(utterance) {
if (speaking && utterance == lastUtterance) {
chrome.tts.stop();
return;
}
speaking = true;
lastUtterance = utterance;
globalUtteranceIndex++;
var utteranceIndex = globalUtteranceIndex;
chrome.browserAction.setIcon({path: 'SpeakSel19-active.png'});
var rate = localStorage['rate'] || 1.0;
var pitch = localStorage['pitch'] || 1.0;
var volume = localStorage['volume'] || 1.0;
var voice = localStorage['voice'];
chrome.tts.speak(
utterance,
{voiceName: voice,
rate: parseFloat(rate),
pitch: parseFloat(pitch),
volume: parseFloat(volume),
onEvent: function(evt) {
if (evt.type == 'end' ||
evt.type == 'interrupted' ||
evt.type == 'cancelled' ||
evt.type == 'error') {
if (utteranceIndex == globalUtteranceIndex) {
speaking = false;
chrome.browserAction.setIcon({path: 'SpeakSel19.png'});
}
}
}
});
}
function initBackground() {
loadContentScriptInAllTabs();
var defaultKeyString = getDefaultKeyString();
var keyString = localStorage['speakKey'];
if (keyString == undefined) {
keyString = defaultKeyString;
localStorage['speakKey'] = keyString;
}
sendKeyToAllTabs(keyString);
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
if (request['init']) {
sendResponse({'key': localStorage['speakKey']});
} else if (request['speak']) {
speak(request['speak']);
}
});
chrome.browserAction.onClicked.addListener(
function(tab) {
chrome.tabs.sendRequest(
tab.id,
{'speakSelection': true});
});
}
initBackground();