-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
26 lines (22 loc) · 1.11 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
let txtLookup = document.getElementById("txtLookup");
document.getElementById("lookupBtn").onclick = function() {
fetchResultsForPopupHtml(txtLookup.value);
}
async function fetchResultsForPopupHtml(selectedWord) {
const targetLang = 'si';
const dicAPI = `https://api.dictionaryapi.dev/api/v2/entries/en/${selectedWord}`;
const translationAPI = `https://api.mymemory.translated.net/get?q=${selectedWord}&langpair=en|${targetLang}`;
try {
const [dictionaryResp, translationResp] = await Promise.all([
fetch(dicAPI).then(response => response.json()),
fetch(translationAPI).then(response => response.json())
]);
const definition = dictionaryResp[0]?.meanings[0]?.definitions[0]?.definition || 'No definition found';
const translation = translationResp.responseData?.translatedText || 'No translation found.';
document.getElementById("lookupWord").textContent = selectedWord;
document.getElementById("sinhalaWord").textContent = translation;
document.querySelector(".definition p").textContent = definition;
} catch (error) {
console.error("Error fetching results:", error);
}
}