From e2ff2fbf3717631ba17f25e40d837a30c61ff38e Mon Sep 17 00:00:00 2001 From: Rio Martinez Date: Sat, 20 Mar 2021 00:00:49 -0400 Subject: [PATCH] adapt translation XHR response parsing for March 2021 Google Translate API updates --- background.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/background.js b/background.js index 992859c..7c1b0bf 100644 --- a/background.js +++ b/background.js @@ -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); } };