-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackground.js
69 lines (58 loc) · 1.79 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
var Selected = {};
var Activity = null;
function getSelected() {
if (Activity) {
return Selected[Activity];
} else {
return null;
}
}
function ttsSpeak(utterance) {
if (utterance !== undefined && utterance !== null && utterance.length > 0) {
if (areEnglish(utterance) && preference.get().TtsSpeakOut) {
chrome.tts.speak(utterance, {'rate': 0.8});
}
}
}
function loadContentScriptInAllTabs() {
chrome.windows.getAll({'populate': true}, function(wins) {
for (var i = 0; i < wins.length; i++) {
var tabs = wins[i].tabs;
for (var j = 0; j < tabs.length; j++) {
chrome.tabs.executeScript(tabs[j].id, {file: 'content_script.js', allFrames: true});
}
}
});
}
function initBackground() {
loadContentScriptInAllTabs();
chrome.windows.getAll({'populate' : true }, function(wins) {
wins.forEach(function(win) {
win.tabs.forEach(function(tab) {
if (tab != undefined && tab.url.indexOf('chrome') !== 0) {
if (tab.highlighted) {
Activity = tab.id;
}
}
});
});
});
chrome.tabs.onActivated.addListener(function(info) {
Activity = info.tabId;
});
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request['select'] != undefined && Activity) {
Selected[Activity] = request['select'];
// console.log("background.js select : " + Selected[Activity]);
ttsSpeak(Selected[Activity]);
}
});
chrome.runtime.onInstalled.addListener(function(details) {
if (details.reason == "install") {
chrome.tabs.create({url : 'help.html', selected : true});
} else if (details.reason == "update") {
// console.log("Updated from " + details.previousVersion + " to " + chrome.runtime.getManifest().version);
}
});
}
initBackground();