Skip to content

Commit

Permalink
adapt translation XHR response parsing for March 2021 Google Translat…
Browse files Browse the repository at this point in the history
…e API updates
  • Loading branch information
rioam2 committed Mar 20, 2021
1 parent 9f7f55f commit e2ff2fb
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ function getTranslation(text, source_lang, target_lang) {
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
const res = JSON.parse(xhr.responseText);
const translation = res.sentences.map((sentence) => sentence.trans).join('');
resolve(translation);
if (res.sentences && Array.isArray(res.sentences)) {
// Undocumented API response schema until March 2021
resolve(res.sentences.map((sentence) => sentence.trans).join(''));
} else if (Array.isArray(res)) {
// Undocumented API response schema as of March 2021+
resolve(res.flat(1e9)[0]);
}
clearTimeout(timeoutRejection);
}
};
Expand Down

0 comments on commit e2ff2fb

Please sign in to comment.