-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
47 lines (41 loc) · 1.59 KB
/
popup.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
document.addEventListener('DOMContentLoaded', function() {
var findMeaningButton = document.getElementById('findMeaningButton');
var wordInput = document.getElementById('wordInput');
var meaningDiv = document.getElementById('meaning');
var SpeakButton = document.getElementById('SpeakButton');
// Initially hide the meaning div
meaningDiv.style.display = 'none';
chrome.storage.local.get(['meaning'], function(result) {
if (result.meaning) {
meaningDiv.textContent = result.meaning;
meaningDiv.style.display = 'block';
}
});
// Event listener for the Find Meaning button
findMeaningButton.addEventListener('click', function() {
var word = wordInput.value;
chrome.runtime.sendMessage({ action: 'lookup', word: word }, function(response) {
if (chrome.runtime.lastError) {
console.error('Error details:', chrome.runtime.lastError);
} else if (response && response.meaning) {
meaningDiv.textContent = response.meaning;
meaningDiv.style.display = 'block'; // Show the div when meaning is displayed
} else {
console.error('Invalid or missing response:', response);
}
});
});
SpeakButton.addEventListener('click', function() {
var word = wordInput.value;
var synth = window.speechSynthesis;
var toSpeak = new SpeechSynthesisUtterance(word);
var voices = synth.getVoices();
var selectedVoiceName = "Microsoft Zira - English (United States)";
voices.forEach((voice) => {
if (voice.name === selectedVoiceName) {
toSpeak.voice = voice;
}
});
synth.speak(toSpeak);
});
});