Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UPGRADE GOOGLE TRANSLATE API #1166

Closed
wants to merge 4 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 43 additions & 8 deletions js/googletranslatejs.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
/* Google Translate JS*/
function googleTranslateElementInit() {
new google.translate.TranslateElement({
pageLanguage: 'en',
}, 'google_translate_element');
document.addEventListener('DOMContentLoaded', function() {
var popup = document.getElementById('translatePopup');
var btn = document.getElementById('translateButton');
var span = document.getElementsByClassName('close')[0];
var translateBtn = document.getElementById('performTranslation');

btn.onclick = function() {
popup.style.display = 'block';
}

span.onclick = function() {
popup.style.display = 'none';
}

function loadGoogleTranslate() {
new google.translate.TranslateElement("google_element");
}
window.onclick = function(event) {
if (event.target == popup) {
popup.style.display = 'none';
}
}

translateBtn.onclick = function() {
var text = document.getElementById('textToTranslate').value;

fetch('https://translation.googleapis.com/language/translate/v2', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_ACCESS_TOKEN' // Replace with your OAuth 2.0 token

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical

The hard-coded value "Bearer YOUR_ACCESS_TOKEN" is used as
authorization header
.
},
body: JSON.stringify({
q: text,
target: 'en', // Replace with the desired target language code
source: 'auto' // Detects the source language automatically
})
})
.then(response => response.json())
.then(data => {
document.getElementById('translatedText').innerText = data.data.translations[0].translatedText;
})
.catch(error => {
console.error('Error translating text:', error);
document.getElementById('translatedText').innerText = 'Translation failed.';
});
}
});
Loading