-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodealpha_translator script.
More file actions
53 lines (36 loc) · 1.49 KB
/
Copy pathcodealpha_translator script.
File metadata and controls
53 lines (36 loc) · 1.49 KB
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
async function translateText() {
const inputText = document.getElementById('inputText').value.trim();
const sourceLang = document.getElementById('source').value;
const targetLang = document.getElementById('target').value;
const outputText = document.getElementById('outputText');
if (!inputText) {
outputText.value = "Please enter text to translate.";
return;
}
outputText.value = "Translating...";
try {
const proxyUrl = 'https://api.codetabs.com/v1/proxy?quest=';
const googleUrl = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sourceLang}&tl=${targetLang}&dt=t&q=${encodeURIComponent(inputText)}`;
const response = await fetch(proxyUrl + encodeURIComponent(googleUrl));
if (!response.ok) {
throw new Error("Network error");
}
const data = await response.json();
const translated = data[0][0][0];
outputText.value = translated || "Translation unavailable.";
} catch (error) {
outputText.value = "Translation failed. Check connection.";
console.error(error);
}
}
function copyText() {
const outputText = document.getElementById("outputText");
if (!outputText.value) {
alert("Nothing to copy");
return;
}
outputText.select();
outputText.setSelectionRange(0, 99999); // mobile support
document.execCommand("copy");
alert("Copied to clipboard!");
}