-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
77 lines (67 loc) · 1.58 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
// Set up context menu items
var startSpeakingItem = {
"id": "start-speaking",
"title": "Start speaking",
"contexts": ["selection"]
}
var stopSpeakingItem = {
"id": "stop-speaking",
"title": "Stop speaking",
"contexts": ["selection"]
}
chrome.contextMenus.create(startSpeakingItem);
chrome.contextMenus.create(stopSpeakingItem);
// Read function
function read(selectionText) {
// Rate
chrome.storage.sync.get(['rate', 'voiceName', 'lang', 'pitch'], function(result) {
var rate = 1;
var voiceName = "";
var lang = "";
var pitch = 1;
// Rate
if (!result.rate) {
chrome.storage.sync.set({'rate': rate});
}
else {
rate = result.rate;
}
// Voice
if (result.voiceName) {
voiceName = result.voiceName;
}
// Language
if (result.lang) {
lang = result.lang;
}
// Pitch
if (result.pitch) {
pitch = result.pitch;
}
// Speak!
chrome.tts.speak(selectionText, {'rate': rate, 'voiceName': voiceName, 'lang': lang, 'pitch': pitch});
});
}
// Context menu speaking implementation
chrome.contextMenus.onClicked.addListener(function(clickData) {
if (clickData.menuItemId == "start-speaking") {
read(clickData.selectionText);
}
else if (clickData.menuItemId == "stop-speaking") {
chrome.tts.stop();
}
});
// Keyboard command speaking implementation
chrome.commands.onCommand.addListener(function(command) {
chrome.tabs.executeScript( {
code: "window.getSelection().toString();"
}, function(selection) {
chrome.tts.isSpeaking(function(speaking) {
if (speaking) {
chrome.tts.stop();
} else {
read(selection[0]);
}
});
});
});