-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
37 lines (27 loc) · 1.06 KB
/
content.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
console.log("Running 'Wordnik Popup' CONTENT")
chrome.runtime.onMessage.addListener(gotMsg)
function gotMsg (request, sender, sendResponse) {
console.log("Popup TRIGGERED Content Script!!")
if (request.action == "get_user_voice_input") {
// @CONSIDER: This would require to turn on microphone permissions
// even on untrusted pages.
let speech = new webkitSpeechRecognition()
let recognizedText
speech.lang = "en-US"
speech.onresult = function (object) {
recognizedText = object.results[0][0].transcript
chrome.runtime.sendMessage({voice_text: recognizedText})
}
speech.onerror = function (e) {
chrome.runtime.sendMessage({voice_text: ""})
}
speech.onnomatch = function (e) {
chrome.runtime.sendMessage({voice_text: ""})
}
speech.start()
}
if (request.action == "get_selected_word") {
let word = window.getSelection().toString().trim()
chrome.runtime.sendMessage({word: word})
}
}