Add log to translate.js

This commit is contained in:
sienori 2019-02-23 01:13:11 +09:00
parent dc546ddcb4
commit 7a39dee3a1
3 changed files with 16 additions and 1 deletions

View file

@ -1,5 +1,8 @@
import log from "loglevel";
let translationHistory = []; let translationHistory = [];
const logDir = "common/translate";
const getHistory = (sourceWord, sourceLang, targetLang) => { const getHistory = (sourceWord, sourceLang, targetLang) => {
const history = translationHistory.find( const history = translationHistory.find(
history => history =>
@ -21,6 +24,7 @@ const setHistory = (sourceWord, sourceLang, targetLang, formattedResult) => {
}; };
const sendRequest = (word, sourceLang, targetLang) => { const sendRequest = (word, sourceLang, targetLang) => {
log.log(logDir, "sendRequest()");
const url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sourceLang}&tl=${targetLang}&dt=t&dt=bd&dj=1&q=${encodeURIComponent( const url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sourceLang}&tl=${targetLang}&dt=t&dt=bd&dj=1&q=${encodeURIComponent(
word word
)}`; )}`;
@ -49,7 +53,10 @@ const formatResult = result => {
}; };
resultData.statusText = result.statusText; resultData.statusText = result.statusText;
if (resultData.statusText !== "OK") return resultData; if (resultData.statusText !== "OK") {
log.error(logDir, "formatResult()", resultData);
return resultData;
}
resultData.sourceLanguage = result.response.src; resultData.sourceLanguage = result.response.src;
resultData.percentage = result.response.confidence; resultData.percentage = result.response.confidence;
@ -60,10 +67,12 @@ const formatResult = result => {
.join(""); .join("");
} }
log.log(logDir, "formatResult()", resultData);
return resultData; return resultData;
}; };
export default async (sourceWord, sourceLang = "auto", targetLang) => { export default async (sourceWord, sourceLang = "auto", targetLang) => {
log.log(logDir, "tranlate()", sourceWord, targetLang);
sourceWord = sourceWord.trim(); sourceWord = sourceWord.trim();
if (sourceWord === "") if (sourceWord === "")
return { return {

View file

@ -2,6 +2,7 @@ import React, { Component } from "react";
import browser from "webextension-polyfill"; import browser from "webextension-polyfill";
import translate from "src/common/translate"; import translate from "src/common/translate";
import { initSettings, getSettings, handleSettingsChange } from "src/settings/settings"; import { initSettings, getSettings, handleSettingsChange } from "src/settings/settings";
import { updateLogLevel, overWriteLogLevel } from "src/common/log";
import TranslateButton from "./TranslateButton"; import TranslateButton from "./TranslateButton";
import TranslatePanel from "./TranslatePanel"; import TranslatePanel from "./TranslatePanel";
import "../styles/TranslateContainer.scss"; import "../styles/TranslateContainer.scss";
@ -74,6 +75,8 @@ export default class TranslateContainer extends Component {
document.addEventListener("keydown", this.handleKeyDown); document.addEventListener("keydown", this.handleKeyDown);
browser.storage.onChanged.addListener(handleSettingsChange); browser.storage.onChanged.addListener(handleSettingsChange);
browser.runtime.onMessage.addListener(this.handleMessage); browser.runtime.onMessage.addListener(this.handleMessage);
overWriteLogLevel();
updateLogLevel();
}; };
handleMessage = async request => { handleMessage = async request => {

View file

@ -1,6 +1,7 @@
import React, { Component } from "react"; import React, { Component } from "react";
import browser from "webextension-polyfill"; import browser from "webextension-polyfill";
import { initSettings, getSettings } from "src/settings/settings"; import { initSettings, getSettings } from "src/settings/settings";
import { updateLogLevel, overWriteLogLevel } from "src/common/log";
import translate from "src/common/translate"; import translate from "src/common/translate";
import Header from "./Header"; import Header from "./Header";
import InputArea from "./InputArea"; import InputArea from "./InputArea";
@ -35,6 +36,8 @@ export default class PopupPage extends Component {
init = async () => { init = async () => {
await initSettings(); await initSettings();
overWriteLogLevel();
updateLogLevel();
const targetLang = getSettings("targetLang"); const targetLang = getSettings("targetLang");
this.setState({ this.setState({