diff --git a/src/common/translate.js b/src/common/translate.js index 4177e4d..b21e76b 100644 --- a/src/common/translate.js +++ b/src/common/translate.js @@ -1,5 +1,8 @@ +import log from "loglevel"; let translationHistory = []; +const logDir = "common/translate"; + const getHistory = (sourceWord, sourceLang, targetLang) => { const history = translationHistory.find( history => @@ -21,6 +24,7 @@ const setHistory = (sourceWord, sourceLang, targetLang, formattedResult) => { }; 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( word )}`; @@ -49,7 +53,10 @@ const formatResult = result => { }; 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.percentage = result.response.confidence; @@ -60,10 +67,12 @@ const formatResult = result => { .join(""); } + log.log(logDir, "formatResult()", resultData); return resultData; }; export default async (sourceWord, sourceLang = "auto", targetLang) => { + log.log(logDir, "tranlate()", sourceWord, targetLang); sourceWord = sourceWord.trim(); if (sourceWord === "") return { diff --git a/src/content/components/TranslateContainer.js b/src/content/components/TranslateContainer.js index 2cd5320..43b739a 100644 --- a/src/content/components/TranslateContainer.js +++ b/src/content/components/TranslateContainer.js @@ -2,6 +2,7 @@ import React, { Component } from "react"; import browser from "webextension-polyfill"; import translate from "src/common/translate"; import { initSettings, getSettings, handleSettingsChange } from "src/settings/settings"; +import { updateLogLevel, overWriteLogLevel } from "src/common/log"; import TranslateButton from "./TranslateButton"; import TranslatePanel from "./TranslatePanel"; import "../styles/TranslateContainer.scss"; @@ -74,6 +75,8 @@ export default class TranslateContainer extends Component { document.addEventListener("keydown", this.handleKeyDown); browser.storage.onChanged.addListener(handleSettingsChange); browser.runtime.onMessage.addListener(this.handleMessage); + overWriteLogLevel(); + updateLogLevel(); }; handleMessage = async request => { diff --git a/src/popup/components/PopupPage.js b/src/popup/components/PopupPage.js index 9eda022..3a59155 100644 --- a/src/popup/components/PopupPage.js +++ b/src/popup/components/PopupPage.js @@ -1,6 +1,7 @@ import React, { Component } from "react"; import browser from "webextension-polyfill"; import { initSettings, getSettings } from "src/settings/settings"; +import { updateLogLevel, overWriteLogLevel } from "src/common/log"; import translate from "src/common/translate"; import Header from "./Header"; import InputArea from "./InputArea"; @@ -35,6 +36,8 @@ export default class PopupPage extends Component { init = async () => { await initSettings(); + overWriteLogLevel(); + updateLogLevel(); const targetLang = getSettings("targetLang"); this.setState({