Skip to content

Commit

Permalink
Adapt to the array response format
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnie97 committed Feb 18, 2022
1 parent 0ddce68 commit 6afeb52
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions katakana-terminator.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// @grant GM_addStyle
// @connect translate.google.com
// @connect translate.google.cn
// @version 2022.01.05
// @version 2022.02.18
// @name:ja-JP カタカナターミネーター
// @name:zh-CN 片假名终结者
// @description:zh-CN 在网页中的日语外来语上方标注英文原词
Expand Down Expand Up @@ -133,7 +133,23 @@ function googleTranslate(srcLang, destLang, phrases) {
method: "GET",
url: api + buildQueryString(params),
onload: function(dom) {
JSON.parse(dom.responseText).sentences.forEach(function(s) {
var resp = JSON.parse(dom.responseText);

// ["katakana\nterminator"]
if (!resp.sentences) {
var translated = resp[0].split('\n');
if (translated.length !== phrases.length) {
throw [phrases, resp];
}
translated.forEach(function(trans, i) {
var orig = phrases[i];
cachedTranslations[orig] = trans;
updateRubyByCachedTranslations(orig);
});
return;
}

resp.sentences.forEach(function(s) {
if (!s.orig) {
return;
}
Expand Down

0 comments on commit 6afeb52

Please sign in to comment.