LingoSynth/lingosynth/app/static/js/index.js

50 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-05-11 21:48:16 +02:00
function stopRephrasing() {
rephraseButton.classList.remove('loading');
textWrapper.classList.remove('loading');
rephraseIcon.className = 'ti ti-language';
}
function rephrase() {
if (textBox.classList.contains('loading')) {
return;
}
rephraseButton.classList.add('loading');
textWrapper.classList.add('loading');
rephraseIcon.className = 'ti ti-loader-2';
const data = {
text: textBox.value
};
fetch('/api/rephrase', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': navigator.userAgent
},
body: JSON.stringify(data),
})
.then(response => response.json())
.then(data => {
textBox.value = data.text;
stopRephrasing();
})
.catch((error) => {
stopRephrasing();
console.error('Error:', error);
const errorDiv = document.createElement('div');
errorDiv.className = 'error alert';
errorDiv.innerText = 'An error occurred. Please try again later.';
popups.appendChild(errorDiv);
});
};
document.addEventListener('keydown', function(event) {
if (event.ctrlKey && event.keyCode === 13) {
rephrase();
}
});