Show error on popup

This commit is contained in:
sienori 2018-08-01 00:50:10 +09:00
parent bb6bf77df7
commit a1e0a82244
3 changed files with 40 additions and 4 deletions

View file

@ -180,6 +180,16 @@
"message": "https://addons.mozilla.org/en-US/firefox/addon/simple-translate/?src=optionpage" "message": "https://addons.mozilla.org/en-US/firefox/addon/simple-translate/?src=optionpage"
}, },
"networkError": {
"message": "Error: Check network connection status."
},
"unavailableError": {
"message": "Error: Service usage limit reached. Please wait a while and try again."
},
"unknownError": {
"message": "Error: Unknown error"
},
"translatePageMenu": { "translatePageMenu": {
"message": "Translate this page" "message": "Translate this page"
}, },

View file

@ -67,7 +67,7 @@ async function changeLang() {
if (sourceWord !== "") { if (sourceWord !== "") {
const resultData = await T.translate(sourceWord, undefined, langList.value); const resultData = await T.translate(sourceWord, undefined, langList.value);
showResult(resultData.resultText, resultData.candidateText); showResult(resultData.resultText, resultData.candidateText, resultData.statusText);
} }
} }
@ -166,15 +166,34 @@ async function runTranslation() {
const resultData = await T.translate(sourceWord, "auto", langList.value); const resultData = await T.translate(sourceWord, "auto", langList.value);
changeSecondLang(defaultTargetLang, resultData.sourceLanguage, resultData.percentage); changeSecondLang(defaultTargetLang, resultData.sourceLanguage, resultData.percentage);
showResult(resultData.resultText, resultData.candidateText); showResult(resultData.resultText, resultData.candidateText, resultData.statusText);
} }
function showResult(resultText, candidateText) { function showResult(resultText, candidateText, statusText = "OK") {
const resultArea = target.getElementsByClassName("result")[0]; const resultArea = target.getElementsByClassName("result")[0];
const candidateArea = target.getElementsByClassName("candidate")[0]; const candidateArea = target.getElementsByClassName("candidate")[0];
resultArea.innerText = resultText; resultArea.innerText = resultText;
if (S.get().ifShowCandidate) candidateArea.innerText = candidateText; if (S.get().ifShowCandidate) candidateArea.innerText = candidateText;
if (statusText != "OK") showError(statusText);
}
function showError(statusText) {
let errorMessage = "";
switch (statusText) {
case "":
errorMessage = browser.i18n.getMessage("networkError");
break;
case "Service Unavailable":
errorMessage = browser.i18n.getMessage("unavailableError");
break;
default:
errorMessage = `${browser.i18n.getMessage("unknownError")} [${statusText}]`;
break;
}
const candidateArea = target.getElementsByClassName("candidate")[0];
candidateArea.innerText = errorMessage;
} }
let changeLangFlag = false; let changeLangFlag = false;

View file

@ -39,6 +39,9 @@ class Translate {
xhr.onload = () => { xhr.onload = () => {
resolve(xhr); resolve(xhr);
}; };
xhr.onerror = () => {
resolve(xhr);
};
}); });
} }
@ -47,9 +50,13 @@ class Translate {
resultText: "", resultText: "",
candidateText: "", candidateText: "",
sourceLanguage: "", sourceLanguage: "",
percentage: 0 percentage: 0,
statusText: ""
}; };
resultData.statusText = results[0].statusText;
if (resultData.statusText !== "OK") return resultData;
//翻訳元言語を取得 //翻訳元言語を取得
resultData.sourceLanguage = results[0].response[2]; resultData.sourceLanguage = results[0].response[2];
resultData.percentage = results[0].response[6]; resultData.percentage = results[0].response[6];