diff --git a/src/hooks/FavWords.js b/src/hooks/FavWords.js index c781279..edfd361 100644 --- a/src/hooks/FavWords.js +++ b/src/hooks/FavWords.js @@ -3,6 +3,7 @@ import { useCallback, useEffect, useState } from "react"; import { trySyncWords } from "../libs/sync"; import { getWordsWithDefault, setWords } from "../libs/storage"; import { useSyncMeta } from "./Sync"; +import { kissLog } from "../libs/log"; export function useFavWords() { const [loading, setLoading] = useState(false); @@ -56,7 +57,7 @@ export function useFavWords() { const favWords = await getWordsWithDefault(); setFavWords(favWords); } catch (err) { - console.log("[query fav]", err); + kissLog(err, "query fav"); } finally { setLoading(false); } diff --git a/src/hooks/Storage.js b/src/hooks/Storage.js index 07e6524..aa8a316 100644 --- a/src/hooks/Storage.js +++ b/src/hooks/Storage.js @@ -1,11 +1,12 @@ import { useCallback, useEffect, useState } from "react"; import { storage } from "../libs/storage"; +import { kissLog } from "../libs/log"; /** - * - * @param {*} key + * + * @param {*} key * @param {*} defaultVal 需为调用hook外的常量 - * @returns + * @returns */ export function useStorage(key, defaultVal) { const [loading, setLoading] = useState(false); @@ -40,7 +41,7 @@ export function useStorage(key, defaultVal) { setData(val); } } catch (err) { - console.log("[storage reload]", err.message); + kissLog(err, "storage reload"); } finally { setLoading(false); } @@ -58,7 +59,7 @@ export function useStorage(key, defaultVal) { await storage.setObj(key, defaultVal); } } catch (err) { - console.log("[storage load]", err.message); + kissLog(err, "storage load"); } finally { setLoading(false); } diff --git a/src/hooks/SubRules.js b/src/hooks/SubRules.js index da42723..ee74e2c 100644 --- a/src/hooks/SubRules.js +++ b/src/hooks/SubRules.js @@ -3,6 +3,7 @@ import { useSetting } from "./Setting"; import { useCallback, useEffect, useMemo, useState } from "react"; import { loadOrFetchSubRules } from "../libs/subRules"; import { delSubRules } from "../libs/storage"; +import { kissLog } from "../libs/log"; /** * 订阅规则 @@ -72,7 +73,7 @@ export function useSubRules() { const rules = await loadOrFetchSubRules(selectedUrl); setSelectedRules(rules); } catch (err) { - console.log("[loadOrFetchSubRules]", err); + kissLog(err, "loadOrFetchSubRules"); } finally { setLoading(false); } diff --git a/src/hooks/Translate.js b/src/hooks/Translate.js index f8a79ac..79d1cde 100644 --- a/src/hooks/Translate.js +++ b/src/hooks/Translate.js @@ -3,6 +3,7 @@ import { useState } from "react"; import { tryDetectLang } from "../libs"; import { apiTranslate } from "../apis"; import { DEFAULT_TRANS_APIS } from "../config"; +import { kissLog } from "../libs/log"; /** * 翻译hook @@ -45,7 +46,7 @@ export function useTranslate(q, rule, setting) { setSamelang(isSame); } } catch (err) { - console.log("[translate]", err); + kissLog(err, "translate"); } finally { setLoading(false); } diff --git a/src/libs/auth.js b/src/libs/auth.js index 72b17c5..51cfc23 100644 --- a/src/libs/auth.js +++ b/src/libs/auth.js @@ -1,12 +1,13 @@ import { getMsauth, setMsauth } from "./storage"; import { URL_MICROSOFT_AUTH } from "../config"; import { fetchData } from "./fetch"; +import { kissLog } from "./log"; const parseMSToken = (token) => { try { return JSON.parse(atob(token.split(".")[1])).exp; } catch (err) { - console.log("[parseMSToken]", err); + kissLog(err, "parseMSToken"); } return 0; }; diff --git a/src/libs/browser.js b/src/libs/browser.js index af6e78a..0be44cb 100644 --- a/src/libs/browser.js +++ b/src/libs/browser.js @@ -8,7 +8,7 @@ function _browser() { try { return require("webextension-polyfill"); } catch (err) { - // console.log("[browser]", err.message); + // kissLog(err, "browser"); } } diff --git a/src/libs/fetch.js b/src/libs/fetch.js index 8ec1a17..15215f8 100644 --- a/src/libs/fetch.js +++ b/src/libs/fetch.js @@ -11,6 +11,7 @@ import { } from "../config"; import { isBg } from "./browser"; import { newCacheReq, newTransReq } from "./req"; +import { kissLog } from "./log"; const TIMEOUT = 5000; @@ -125,7 +126,7 @@ export const fetchData = async ( const cache = await caches.open(CACHE_NAME); res = await cache.match(cacheReq); } catch (err) { - console.log("[cache match]", err.message); + kissLog(err, "cache match"); } } @@ -154,7 +155,7 @@ export const fetchData = async ( const cache = await caches.open(CACHE_NAME); await cache.put(cacheReq, res.clone()); } catch (err) { - console.log("[cache put]", err.message); + kissLog(err, "cache put"); } } } diff --git a/src/libs/index.js b/src/libs/index.js index 652b002..13ed33e 100644 --- a/src/libs/index.js +++ b/src/libs/index.js @@ -1,6 +1,7 @@ import { CACHE_NAME } from "../config"; import { browser } from "./browser"; import { apiBaiduLangdetect } from "../apis"; +import { kissLog } from "./log"; /** * 清除缓存数据 @@ -9,7 +10,7 @@ export const tryClearCaches = async () => { try { caches.delete(CACHE_NAME); } catch (err) { - console.log("[clean caches]", err.message); + kissLog(err, "clean caches"); } }; @@ -25,7 +26,7 @@ export const tryDetectLang = async (q, useRemote = false) => { try { lang = await apiBaiduLangdetect(q); } catch (err) { - console.log("[detect lang remote]", err.message); + kissLog(err, "detect lang remote"); } } @@ -34,7 +35,7 @@ export const tryDetectLang = async (q, useRemote = false) => { const res = await browser?.i18n?.detectLanguage(q); lang = res?.languages?.[0]?.language; } catch (err) { - console.log("[detect lang local]", err.message); + kissLog(err, "detect lang local"); } } diff --git a/src/libs/inputTranslate.js b/src/libs/inputTranslate.js index 44469ca..5aed7b9 100644 --- a/src/libs/inputTranslate.js +++ b/src/libs/inputTranslate.js @@ -8,6 +8,7 @@ import { genEventName, removeEndchar, matchInputStr, sleep } from "./utils"; import { stepShortcutRegister } from "./shortcut"; import { apiTranslate } from "../apis"; import { loadingSvg } from "./svg"; +import { kissLog } from "./log"; function isInputNode(node) { return node.nodeName === "INPUT" || node.nodeName === "TEXTAREA"; @@ -187,7 +188,7 @@ export default function inputTranslate({ collapseToEnd(node); } } catch (err) { - console.log("[translate input]", err.message); + kissLog(err, "translate input"); } finally { removeLoading(node, loadingId); } diff --git a/src/libs/log.js b/src/libs/log.js new file mode 100644 index 0000000..e70dfb9 --- /dev/null +++ b/src/libs/log.js @@ -0,0 +1,12 @@ +/** + * 日志函数 + * @param {*} msg + * @param {*} type + */ +export const kissLog = (msg, type) => { + let prefix = `[KISS-Translator]`; + if (type) { + prefix += `[${type}]`; + } + console.log(`${prefix} ${msg}`); +}; diff --git a/src/libs/pool.js b/src/libs/pool.js index f6270a2..7f28f97 100644 --- a/src/libs/pool.js +++ b/src/libs/pool.js @@ -1,3 +1,5 @@ +import { kissLog } from "./log"; + /** * 任务池 * @param {*} fn @@ -35,7 +37,7 @@ export const taskPool = ( const res = await fn({ ...args, ...preArgs }); resolve(res); } catch (err) { - console.log("[task]", retry, err); + kissLog(err, "task"); if (retry < maxRetry) { const retryTimer = setTimeout(() => { clearTimeout(retryTimer); diff --git a/src/libs/rules.js b/src/libs/rules.js index 488398a..6aa8f4f 100644 --- a/src/libs/rules.js +++ b/src/libs/rules.js @@ -13,6 +13,7 @@ import { loadOrFetchSubRules } from "./subRules"; import { getRulesWithDefault, setRules } from "./storage"; import { trySyncRules } from "./sync"; import { FIXER_ALL } from "./webfix"; +import { kissLog } from "./log"; /** * 根据href匹配规则 @@ -49,7 +50,7 @@ export const matchRule = async ( rules.splice(-1, 0, ...subRules); } } catch (err) { - console.log("[load injectRules]", err); + kissLog(err, "load injectRules"); } } diff --git a/src/libs/storage.js b/src/libs/storage.js index a1b81fd..0824187 100644 --- a/src/libs/storage.js +++ b/src/libs/storage.js @@ -14,6 +14,7 @@ import { } from "../config"; import { isExt, isGm } from "./client"; import { browser } from "./browser"; +import { kissLog } from "./log"; async function set(key, val) { if (isExt) { @@ -155,6 +156,6 @@ export const tryInitDefaultData = async () => { BUILTIN_RULES ); } catch (err) { - console.log("[init default]", err); + kissLog(err, "init default"); } }; diff --git a/src/libs/subRules.js b/src/libs/subRules.js index 3d75d2b..f8fedc7 100644 --- a/src/libs/subRules.js +++ b/src/libs/subRules.js @@ -8,6 +8,7 @@ import { import { apiFetch } from "../apis"; import { checkRules } from "./rules"; import { isAllchar } from "./utils"; +import { kissLog } from "./log"; /** * 更新缓存同步时间 @@ -46,7 +47,7 @@ export const syncAllSubRules = async (subrulesList) => { await syncSubRules(subrules.url); await updateSyncDataCache(subrules.url); } catch (err) { - console.log(`[sync subrule error]: ${subrules.url}`, err); + kissLog(err, `sync subrule error: ${subrules.url}`); } } }; @@ -67,7 +68,7 @@ export const trySyncAllSubRules = async ({ subrulesList }) => { await updateSync({ subRulesSyncAt: now }); } } catch (err) { - console.log("[try sync all subrules]", err); + kissLog(err, "try sync all subrules"); } }; diff --git a/src/libs/sync.js b/src/libs/sync.js index 7d9743a..42f0c5d 100644 --- a/src/libs/sync.js +++ b/src/libs/sync.js @@ -21,6 +21,7 @@ import { apiSyncData } from "../apis"; import { sha256, removeEndchar } from "./utils"; import { createClient, getPatcher } from "webdav"; import { fetchApi } from "./fetch"; +import { kissLog } from "./log"; getPatcher().patch("request", (opts) => { return fetchApi({ @@ -115,7 +116,7 @@ export const trySyncSetting = async () => { try { await syncSetting(); } catch (err) { - console.log("[sync setting]", err); + kissLog(err, "sync setting"); } }; @@ -134,7 +135,7 @@ export const trySyncRules = async () => { try { await syncRules(); } catch (err) { - console.log("[sync user rules]", err); + kissLog(err, "sync user rules"); } }; @@ -153,7 +154,7 @@ export const trySyncWords = async () => { try { await syncWords(); } catch (err) { - console.log("[sync fav words]", err); + kissLog(err, "sync fav words"); } }; diff --git a/src/libs/translator.js b/src/libs/translator.js index 6d63e09..fbfa521 100644 --- a/src/libs/translator.js +++ b/src/libs/translator.js @@ -22,6 +22,7 @@ import { apiTranslate } from "../apis"; import { sendBgMsg } from "./msg"; import { isExt } from "./client"; import { injectInlineJs, injectInternalCss } from "./injector"; +import { kissLog } from "./log"; /** * 翻译类 @@ -193,7 +194,7 @@ export class Translator { try { return Array.from(node.querySelectorAll(selector)); } catch (err) { - console.log(`[querySelectorAll err]: ${selector}`); + kissLog(selector, "querySelectorAll err"); } return []; }; diff --git a/src/views/Action/index.js b/src/views/Action/index.js index cb8b74a..580551a 100644 --- a/src/views/Action/index.js +++ b/src/views/Action/index.js @@ -21,6 +21,7 @@ import { } from "../../config"; import { shortcutRegister } from "../../libs/shortcut"; import { sendIframeMsg } from "../../libs/iframe"; +import { kissLog } from "../../libs/log"; export default function Action({ translator, fab }) { const fabWidth = 40; @@ -138,7 +139,7 @@ export default function Action({ translator, fab }) { }); }; } catch (err) { - console.log("[registerMenuCommand]", err); + kissLog(err, "registerMenuCommand"); } }, [translator]); diff --git a/src/views/Options/FavWords.js b/src/views/Options/FavWords.js index 5e45415..311fbf4 100644 --- a/src/views/Options/FavWords.js +++ b/src/views/Options/FavWords.js @@ -18,6 +18,7 @@ import UploadButton from "./UploadButton"; import Button from "@mui/material/Button"; import ClearAllIcon from "@mui/icons-material/ClearAll"; import { isValidWord } from "../../libs/utils"; +import { kissLog } from "../../libs/log"; function DictField({ word }) { const [dictResult, setDictResult] = useState(null); @@ -93,7 +94,7 @@ export default function FavWords() { .filter(isValidWord); await mergeWords(newWords); } catch (err) { - console.log("[import rules]", err); + kissLog(err, "import rules"); } }; diff --git a/src/views/Options/Rules.js b/src/views/Options/Rules.js index 42efb5f..742da3e 100644 --- a/src/views/Options/Rules.js +++ b/src/views/Options/Rules.js @@ -59,6 +59,7 @@ import AddIcon from "@mui/icons-material/Add"; import EditIcon from "@mui/icons-material/Edit"; import CancelIcon from "@mui/icons-material/Cancel"; import SaveIcon from "@mui/icons-material/Save"; +import { kissLog } from "../../libs/log"; function RuleFields({ rule, rules, setShow, setKeyword }) { const initFormValues = { @@ -701,7 +702,7 @@ function ShareButton({ rules, injectRules, selectedUrl }) { window.open(url, "_blank"); } catch (err) { alert.warning(i18n("error_got_some_wrong")); - console.log("[share rules]", err); + kissLog(err, "share rules"); } }; @@ -731,7 +732,7 @@ function UserRules({ subRules }) { try { await rules.merge(JSON.parse(data)); } catch (err) { - console.log("[import rules]", err); + kissLog(err, "import rules"); } }; @@ -864,7 +865,7 @@ function SubRulesItem({ await delSubRules(url); await deleteDataCache(url); } catch (err) { - console.log("[del subrules]", err); + kissLog(err, "del subrules"); } }; @@ -877,7 +878,7 @@ function SubRulesItem({ } await updateDataCache(url); } catch (err) { - console.log("[sync sub rules]", err); + kissLog(err, "sync sub rules"); } finally { setLoading(false); } @@ -956,7 +957,7 @@ function SubRulesEdit({ subList, addSub, updateDataCache }) { setShowInput(false); setInputText(""); } catch (err) { - console.log("[fetch rules]", err); + kissLog(err, "fetch rules"); setInputError(i18n("error_fetch_url")); } finally { setLoading(false); diff --git a/src/views/Options/Setting.js b/src/views/Options/Setting.js index 822cb6b..d82d435 100644 --- a/src/views/Options/Setting.js +++ b/src/views/Options/Setting.js @@ -28,6 +28,7 @@ import { useShortcut } from "../../hooks/Shortcut"; import ShortcutInput from "./ShortcutInput"; import { useFab } from "../../hooks/Fab"; import { sendBgMsg } from "../../libs/msg"; +import { kissLog } from "../../libs/log"; function ShortcutItem({ action, label }) { const { shortcut, setShortcut } = useShortcut(action); @@ -82,7 +83,7 @@ export default function Settings() { caches.delete(CACHE_NAME); alert.success(i18n("clear_success")); } catch (err) { - console.log("[clear cache]", err); + kissLog(err, "clear cache"); } }; diff --git a/src/views/Options/SyncSetting.js b/src/views/Options/SyncSetting.js index 6bb4206..a8d1fc0 100644 --- a/src/views/Options/SyncSetting.js +++ b/src/views/Options/SyncSetting.js @@ -19,6 +19,7 @@ import { useAlert } from "../../hooks/Alert"; import SyncIcon from "@mui/icons-material/Sync"; import CircularProgress from "@mui/material/CircularProgress"; import { useSetting } from "../../hooks/Setting"; +import { kissLog } from "../../libs/log"; export default function SyncSetting() { const i18n = useI18n(); @@ -43,7 +44,7 @@ export default function SyncSetting() { await reloadSetting(); alert.success(i18n("sync_success")); } catch (err) { - console.log("[sync all]", err); + kissLog(err, "sync all"); alert.error(i18n("sync_failed")); } finally { setLoading(false); diff --git a/src/views/Popup/index.js b/src/views/Popup/index.js index 9c01f8d..5c64571 100644 --- a/src/views/Popup/index.js +++ b/src/views/Popup/index.js @@ -27,6 +27,7 @@ import { import { sendIframeMsg } from "../../libs/iframe"; import { saveRule } from "../../libs/rules"; import { tryClearCaches } from "../../libs"; +import { kissLog } from "../../libs/log"; export default function Popup({ setShowPopup, translator: tran }) { const i18n = useI18n(); @@ -55,7 +56,7 @@ export default function Popup({ setShowPopup, translator: tran }) { sendIframeMsg(MSG_TRANS_TOGGLE); } } catch (err) { - console.log("[toggle trans]", err); + kissLog(err, "toggle trans"); } }; @@ -71,7 +72,7 @@ export default function Popup({ setShowPopup, translator: tran }) { sendIframeMsg(MSG_TRANS_PUTRULE, { [name]: value }); } } catch (err) { - console.log("[update rule]", err); + kissLog(err, "update rule"); } }; @@ -93,7 +94,7 @@ export default function Popup({ setShowPopup, translator: tran }) { saveRule(newRule); } } catch (err) { - console.log("[save rule]", err); + kissLog(err, "save rule"); } }; @@ -108,7 +109,7 @@ export default function Popup({ setShowPopup, translator: tran }) { setRule(res.data); } } catch (err) { - console.log("[query rule]", err); + kissLog(err, "query rule"); } })(); }, [tran]); @@ -132,7 +133,7 @@ export default function Popup({ setShowPopup, translator: tran }) { } setCommands(commands); } catch (err) { - console.log("[query cmds]", err); + kissLog(err, "query cmds"); } })(); }, [tran]); diff --git a/src/views/Selection/FavBtn.js b/src/views/Selection/FavBtn.js index 0f0f352..417b900 100644 --- a/src/views/Selection/FavBtn.js +++ b/src/views/Selection/FavBtn.js @@ -3,6 +3,7 @@ import FavoriteIcon from "@mui/icons-material/Favorite"; import FavoriteBorderIcon from "@mui/icons-material/FavoriteBorder"; import { useState } from "react"; import { useFavWords } from "../../hooks/FavWords"; +import { kissLog } from "../../libs/log"; export default function FavBtn({ word }) { const { favWords, toggleFav } = useFavWords(); @@ -13,7 +14,7 @@ export default function FavBtn({ word }) { setLoading(true); await toggleFav(word); } catch (err) { - console.log("[set fav]", err); + kissLog(err, "set fav"); } finally { setLoading(false); } diff --git a/src/views/Selection/index.js b/src/views/Selection/index.js index b328f3d..3ba2caa 100644 --- a/src/views/Selection/index.js +++ b/src/views/Selection/index.js @@ -6,6 +6,7 @@ import { sleep, limitNumber } from "../../libs/utils"; import { isGm, isExt } from "../../libs/client"; import { MSG_OPEN_TRANBOX, DEFAULT_TRANBOX_SHORTCUT } from "../../config"; import { isMobile } from "../../libs/mobile"; +import { kissLog } from "../../libs/log"; export default function Slection({ contextMenuType, @@ -127,7 +128,7 @@ export default function Slection({ }); }; } catch (err) { - console.log("[registerMenuCommand]", err); + kissLog(err, "registerMenuCommand"); } }, [handleTranbox, contextMenuType]);