-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
74 lines (67 loc) · 2.4 KB
/
script.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
selectTag = document.querySelectorAll("select");
translateButton = document.querySelector('button');
fromText = document.querySelector('.from-text');
toText = document.querySelector('.to-text');
exchangeButton = document.querySelector('.exchange');
icons = document.querySelectorAll(".row i");
const copyContent = (text) => {
navigator.clipboard.writeText(text);
}
const utterText = (text, language) => {
const synth = window.speechSynthesis
console.log(text, language);
const utterance = new SpeechSynthesisUtterance(text);
utterance.lang = language;
synth.speak(utterance);
}
selectTag.forEach((tag, id) => {
console.log(tag, id);
for(let language in languages) {
let selected = id == 0 ? language == "en-GB" ? "selected" : "" : language == "hi-IN" ? "selected" : "";
let option = `<option ${selected} value="${language}">${languages[language]}</option>`;
tag.insertAdjacentHTML("beforeend", option);
}
})
exchangeButton.addEventListener("click", () => {
let tempValue = fromText.value;
fromText.value = toText.value;
toText.value = tempValue;
let tempLang = selectTag[0].value;
selectTag[0].value = selectTag[1].value;
selectTag[1].value = tempLang;
});
translateButton.addEventListener("click", () => {
fromLanguage = selectTag[0].value
toLanguage = selectTag[1].value
console.log(fromLanguage, toLanguage);
let text = fromText.value;
if(!text) {
toText.value = "";
toText.setAttribute("placeholder", "");
return;
}
toText.setAttribute("placeholder", "Translating...");
let url = `https://api.mymemory.translated.net/get?q=${text}&langpair=${fromLanguage}|${toLanguage}`;
fetch(url).then(res => res.json()).then(data => {
console.log(data.responseData);
toText.value = data.responseData.translatedText;
});
})
icons.forEach(icon => {
icon.addEventListener("click", ({target}) => {
if(!fromText.value || !toText.value) return;
if(target.classList.contains("fa-copy")) {
if(target.id == "from") {
copyContent(fromText.value);
} else {
copyContent(toText.value);
}
} else {
if(target.id == "from") {
utterText(fromText.value, selectTag[0].value);
} else {
utterText(toText.value, selectTag[1].value);
}
}
})
})