-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbackground.js
212 lines (187 loc) · 5.22 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { browser } from "webextension-polyfill-ts";
const GOOGLE_SEARCH_QUERY_URL = "http://google.com/search?q=";
let micTabId = -1;
let extensionState = "init";
const MIC_EXTENSION_URL = browser.runtime.getURL("/mic/mic.html");
let micOnInit;
const urlRegex = new RegExp("chrome.*\:\/\/.*");
async function updateIcon(condition) {
let path = "../icons/pico-blue-16.png";
switch (condition) {
case "init":
path = "../icons/pico-grey-16.png";
break;
case "on":
path = "../icons/pico-blue-16.png";
break;
case "wake":
path = "../icons/pico-teal-16.png";
break;
case "mic-error":
path = "../icons/pico-pink-16.png";
break;
case "off":
path = "../icons/pico-grey-16.png";
break;
default:
break;
}
await browser.action.setIcon({ path: path });
}
const setExtensionState = async (newState) => {
extensionState = newState;
browser.action.setBadgeText({ text: extensionState });
if (newState === "off") {
await browser.storage.local.set({ micOn: false });
}
updateIcon(extensionState);
};
const action = async (event) => {
switch (extensionState) {
case "init":
if (micOnInit) {
await getOrCreateMicTab();
await setExtensionState("on");
} else {
await setExtensionState("off");
}
break;
case "on":
await setExtensionState("off");
await closeMicTab();
await browser.storage.local.set({ micOn: false });
break;
case "off":
await setExtensionState("on");
await getOrCreateMicTab();
break;
case "mic-error":
await closeMicTab();
await getOrCreateMicTab();
break;
}
};
/** Respond to user clicking the extension icon (i.e. toggling on/off) */
browser.action.onClicked.addListener(action);
/** Listen for tab closing (for Mic tab) */
browser.tabs.onRemoved.addListener(async (tabId) => {
if (tabId === micTabId) {
await setExtensionState("off");
}
});
/** Listen for voice messages sent from the microphone tab */
browser.runtime.onMessage.addListener(async (request) => {
switch (request.command) {
case "ready":
await setExtensionState("on");
break;
case "error":
await setExtensionState("mic-error");
break;
case "ppn-keyword":
updateIcon("wake");
messageActiveTab("[Say your search query]");
break;
case "wsr-onend":
// If the user said something, open a Google search tab with their query
if (request.transcript !== undefined) {
messageActiveTab(request.transcript.trim());
const encodedQueryParams = encodeURIComponent(
request.transcript.trim()
);
browser.tabs.create({
url: `${GOOGLE_SEARCH_QUERY_URL}${encodedQueryParams}`,
});
}
// Back to idle state
await setExtensionState("on");
break;
case "wsr-onresult":
if (request.transcript !== undefined) {
messageActiveTab(request.transcript.trim());
}
break;
}
});
/** Receive keyboard shortcut commands from browser
Simulate voice events for testing purposes
(or, for push-to-talk experience) */
browser.commands.onCommand.addListener((command) => {
switch (command) {
case "simWakeWord":
{
messageActiveTab("[Say your search query]");
}
break;
default:
console.log("Unhandled command: " + command);
break;
}
});
async function messageActiveTab(message) {
const activeTabs = await browser.tabs.query({
currentWindow: true,
active: true,
});
for (const activeTab of activeTabs) {
if (!urlRegex.test(activeTab.url)) {
browser.scripting.executeScript({
target: {tabId: activeTab.id},
func: writeMessage,
args: [message]
});
}
}
}
function writeMessage(message) {
let element = document.getElementById("pv_transcript");
if (element === null) {
element = document.createElement("div");
element.className = "transcript";
element.id = "pv_transcript";
document.body.appendChild(element);
}
element.innerHTML = message;
clearInterval(element.timeout);
element.timeout = setTimeout(() => {
element.remove();
}, 3000);
}
async function closeMicTab() {
// Although we're tracking the micTabId,
// query again for mic tab(s), if for any reason
// it's out of sync with background.js, we want to make sure
// we actually destroy any/all extant mic tabs.
const extantMicTabs = await browser.tabs.query({
url: MIC_EXTENSION_URL,
});
for (const extantMicTab of extantMicTabs) {
browser.tabs.remove(extantMicTab.id);
}
micTabId = -1;
}
async function getOrCreateMicTab() {
const extantMicTabs = await browser.tabs.query({
url: MIC_EXTENSION_URL,
});
if (extantMicTabs.length === 0) {
const micTab = await browser.tabs.create({
url: MIC_EXTENSION_URL,
pinned: true,
});
micTabId = micTab.id;
}
}
const init = async () => {
// Persist whether the extension is on/off when Chrome restarts
const dataMicOn = await browser.storage.local.get("micOn");
if (dataMicOn.micOn === undefined) {
await browser.storage.local.set({ micOn: true });
micOnInit = true;
action();
} else {
micOnInit = dataMicOn.micOn;
action();
}
};
init();