From 3140b91f723e8e833d5d3b1a9e1e42f988ecfbc7 Mon Sep 17 00:00:00 2001 From: IndieKKY Date: Sun, 6 Oct 2024 13:38:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.tsx | 2 +- src/chrome/background.ts | 8 ++++---- src/components/MoreBtn.tsx | 2 +- ...eMessagingService.ts => useMessageService.ts} | 8 ++++---- src/hooks/useSubtitle.ts | 2 +- src/hooks/useSubtitleService.ts | 4 ++-- src/hooks/useTranslate.ts | 4 ++-- src/inject/inject.ts | 16 ++++++++-------- ...ExtensionMessage.ts => ExtensionMessaging.ts} | 4 ++-- .../{InjectMessage.ts => InjectMessaging.ts} | 4 ++-- .../layer2/{useMessage.ts => useMessaging.ts} | 6 +++--- ...eMessageService.ts => useMessagingService.ts} | 4 ++-- src/pages/MainPage.tsx | 2 +- src/pages/OptionsPage.tsx | 4 ++-- 14 files changed, 35 insertions(+), 35 deletions(-) rename src/hooks/{useMessagingService.ts => useMessageService.ts} (84%) rename src/messaging/layer2/{ExtensionMessage.ts => ExtensionMessaging.ts} (98%) rename src/messaging/layer2/{InjectMessage.ts => InjectMessaging.ts} (98%) rename src/messaging/layer2/{useMessage.ts => useMessaging.ts} (90%) rename src/messaging/layer2/{useMessageService.ts => useMessagingService.ts} (97%) diff --git a/src/App.tsx b/src/App.tsx index ae9546d..89cd89f 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -8,7 +8,7 @@ import OptionsPage from './pages/OptionsPage' import {handleJson} from '@kky002/kky-util' import {useLocalStorage} from '@kky002/kky-hooks' import {Toaster} from 'react-hot-toast' -import useMessagingService from './hooks/useMessagingService' +import useMessagingService from './hooks/useMessageService' import MainPage from './pages/MainPage' function App() { diff --git a/src/chrome/background.ts b/src/chrome/background.ts index f25d6ec..1c79c7d 100644 --- a/src/chrome/background.ts +++ b/src/chrome/background.ts @@ -1,7 +1,7 @@ import {v4} from 'uuid' import {handleTask, initTaskService, tasksMap} from './taskService' import { MESSAGE_TO_EXTENSION_ADD_TASK, MESSAGE_TO_EXTENSION_CLOSE_SIDE_PANEL, MESSAGE_TO_EXTENSION_GET_TASK, MESSAGE_TO_EXTENSION_SHOW_FLAG, MESSAGE_TO_INJECT_TOGGLE_DISPLAY, STORAGE_ENV} from '@/consts/const' -import ExtensionMessage from '@/messaging/layer2/ExtensionMessage' +import ExtensionMessaging from '@/messaging/layer2/ExtensionMessaging' import { TAG_TARGET_INJECT } from '@/messaging/const' const setBadgeOk = async (tabId: number, ok: boolean) => { @@ -75,8 +75,8 @@ const methods: { }, } // 初始化backgroundMessage -const extensionMessage = new ExtensionMessage() -extensionMessage.init(methods) +const extensionMessaging = new ExtensionMessaging() +extensionMessaging.init(methods) chrome.runtime.onMessage.addListener((event: MessageData, sender: chrome.runtime.MessageSender, sendResponse: (result: any) => void) => { // debug((sender.tab != null) ? `tab ${sender.tab.url ?? ''} => ` : 'extension => ', event) @@ -115,7 +115,7 @@ chrome.action.onClicked.addListener(async (tab) => { }) } else { closeSidePanel() - extensionMessage.broadcastMessageExact([tab.id!], [TAG_TARGET_INJECT], MESSAGE_TO_INJECT_TOGGLE_DISPLAY).catch(console.error) + extensionMessaging.broadcastMessageExact([tab.id!], [TAG_TARGET_INJECT], MESSAGE_TO_INJECT_TOGGLE_DISPLAY).catch(console.error) } }) }) diff --git a/src/components/MoreBtn.tsx b/src/components/MoreBtn.tsx index 296e53e..634a84d 100644 --- a/src/components/MoreBtn.tsx +++ b/src/components/MoreBtn.tsx @@ -18,7 +18,7 @@ import {formatSrtTime, formatTime, formatVttTime} from '../utils/util' import {downloadText, openUrl} from '@kky002/kky-util' import toast from 'react-hot-toast' import {getSummarize} from '../utils/biz_util' -import useMessage from '../messaging/layer2/useMessage' +import useMessage from '../messaging/layer2/useMessaging' interface Props { placement: Placement } diff --git a/src/hooks/useMessagingService.ts b/src/hooks/useMessageService.ts similarity index 84% rename from src/hooks/useMessagingService.ts rename to src/hooks/useMessageService.ts index 5455c04..7994964 100644 --- a/src/hooks/useMessagingService.ts +++ b/src/hooks/useMessageService.ts @@ -2,9 +2,9 @@ import { setCurFetched, setCurInfo, setData, setInfos, setTitle, setUrl } from ' import { useMemo } from 'react' import { useAppDispatch } from './redux' import { MESSAGE_TO_APP_SET_INFOS, MESSAGE_TO_APP_SET_VIDEO_INFO } from '@/consts/const' -import useMessageService from '@/messaging/layer2/useMessageService' +import useMessagingService from '@/messaging/layer2/useMessagingService' -const useMessagingService = () => { +const useMessageService = () => { const dispatch = useAppDispatch() //methods @@ -25,7 +25,7 @@ const useMessagingService = () => { }, }), [dispatch]) - useMessageService(methods) + useMessagingService(methods) } -export default useMessagingService \ No newline at end of file +export default useMessageService \ No newline at end of file diff --git a/src/hooks/useSubtitle.ts b/src/hooks/useSubtitle.ts index 8432679..01164e7 100644 --- a/src/hooks/useSubtitle.ts +++ b/src/hooks/useSubtitle.ts @@ -2,7 +2,7 @@ import {useAppDispatch, useAppSelector} from './redux' import React, {useCallback} from 'react' import {setNeedScroll, setReviewAction, setTempData} from '../redux/envReducer' import {MESSAGE_TO_INJECT_MOVE} from '../consts/const' -import useMessage from '../messaging/layer2/useMessage' +import useMessage from '../messaging/layer2/useMessaging' const useSubtitle = () => { const dispatch = useAppDispatch() const reviewed = useAppSelector(state => state.env.tempData.reviewed) diff --git a/src/hooks/useSubtitleService.ts b/src/hooks/useSubtitleService.ts index 5a14009..c5e8be0 100644 --- a/src/hooks/useSubtitleService.ts +++ b/src/hooks/useSubtitleService.ts @@ -20,8 +20,8 @@ import {EVENT_EXPAND, GEMINI_TOKENS, TOTAL_HEIGHT_MAX, TOTAL_HEIGHT_MIN, WORDS_M import {useAsyncEffect, useInterval} from 'ahooks' import {getModelMaxTokens, getWholeText} from '../utils/biz_util' import {MESSAGE_TO_INJECT_GET_SUBTITLE} from '../consts/const' -import useMessage from '../messaging/layer2/useMessage' -import { msgWaiter } from '@/messaging/layer2/useMessageService' +import useMessage from '../messaging/layer2/useMessaging' +import { msgWaiter } from '@/messaging/layer2/useMessagingService' /** * Service是单例,类似后端的服务概念 diff --git a/src/hooks/useTranslate.ts b/src/hooks/useTranslate.ts index 6cf1f0e..9d33a3e 100644 --- a/src/hooks/useTranslate.ts +++ b/src/hooks/useTranslate.ts @@ -31,7 +31,7 @@ import toast from 'react-hot-toast' import {useMemoizedFn} from 'ahooks/es' import {extractJsonArray, extractJsonObject, getModel} from '../utils/biz_util' import {formatTime} from '../utils/util' -import useMessage from '@/messaging/layer2/useMessage' +import useMessaging from '@/messaging/layer2/useMessaging' const useTranslate = () => { const dispatch = useAppDispatch() const data = useAppSelector(state => state.env.data) @@ -45,7 +45,7 @@ const useTranslate = () => { const reviewed = useAppSelector(state => state.env.tempData.reviewed) const reviewAction = useAppSelector(state => state.env.reviewAction) const reviewActions = useAppSelector(state => state.env.tempData.reviewActions) - const {sendExtension} = useMessage() + const {sendExtension} = useMessaging() /** * 获取下一个需要翻译的行 * 会检测冷却 diff --git a/src/inject/inject.ts b/src/inject/inject.ts index 8becd93..5101822 100644 --- a/src/inject/inject.ts +++ b/src/inject/inject.ts @@ -1,6 +1,6 @@ import { TOTAL_HEIGHT_DEF, HEADER_HEIGHT, TOTAL_HEIGHT_MIN, TOTAL_HEIGHT_MAX, IFRAME_ID, MESSAGE_TO_INJECT_DOWNLOAD_AUDIO, MESSAGE_TARGET_INJECT, MESSAGE_TO_APP_SET_INFOS, MESSAGE_TO_INJECT_TOGGLE_DISPLAY, STORAGE_ENV, MESSAGE_TO_EXTENSION_SHOW_FLAG } from '@/consts/const' import { MESSAGE_TO_INJECT_FOLD, MESSAGE_TO_INJECT_MOVE, MESSAGE_TO_APP_SET_VIDEO_INFO, MESSAGE_TO_INJECT_GET_SUBTITLE, MESSAGE_TO_INJECT_GET_VIDEO_STATUS, MESSAGE_TO_INJECT_GET_VIDEO_ELEMENT_INFO, MESSAGE_TO_INJECT_UPDATETRANSRESULT, MESSAGE_TO_INJECT_PLAY, MESSAGE_TO_INJECT_HIDE_TRANS, MESSAGE_TO_INJECT_REFRESH_VIDEO_INFO } from '@/consts/const' -import InjectMessage from '@/messaging/layer2/InjectMessage' +import InjectMessaging from '@/messaging/layer2/InjectMessaging' const debug = (...args: any[]) => { console.debug('[Inject]', ...args) @@ -30,7 +30,7 @@ const debug = (...args: any[]) => { } const runtime: { - injectMessage: InjectMessage + injectMessaging: InjectMessaging // lastV?: string | null // lastVideoInfo?: VideoInfo @@ -42,7 +42,7 @@ const debug = (...args: any[]) => { showTrans: boolean curTrans?: string } = { - injectMessage: new InjectMessage(), + injectMessaging: new InjectMessaging(), fold: true, videoElementHeight: TOTAL_HEIGHT_DEF, showTrans: false, @@ -98,7 +98,7 @@ const debug = (...args: any[]) => { danmukuBox?.insertBefore(iframe, danmukuBox?.firstChild) // show badge - runtime.injectMessage.sendExtension(MESSAGE_TO_EXTENSION_SHOW_FLAG, { + runtime.injectMessaging.sendExtension(MESSAGE_TO_EXTENSION_SHOW_FLAG, { show: true }) @@ -188,7 +188,7 @@ const debug = (...args: any[]) => { debug('refreshVideoInfo: ', aid, cid, pages, subtitles) //send setVideoInfo - runtime.injectMessage.sendApp(MESSAGE_TO_APP_SET_VIDEO_INFO, { + runtime.injectMessaging.sendApp(MESSAGE_TO_APP_SET_VIDEO_INFO, { url: location.origin + location.pathname, title, aid, @@ -225,7 +225,7 @@ const debug = (...args: any[]) => { .then(res => res.json()) .then(res => { // console.log('refreshSubtitles: ', aid, cid, res) - runtime.injectMessage.sendApp(MESSAGE_TO_APP_SET_INFOS, { + runtime.injectMessaging.sendApp(MESSAGE_TO_APP_SET_INFOS, { infos: res.data.subtitle.subtitles }) }) @@ -247,7 +247,7 @@ const debug = (...args: any[]) => { const iframe = document.getElementById(IFRAME_ID) as HTMLIFrameElement | undefined if (iframe != null) { iframe.style.display = iframe.style.display === 'none' ? 'block' : 'none' - runtime.injectMessage.sendExtension(MESSAGE_TO_EXTENSION_SHOW_FLAG, { + runtime.injectMessaging.sendExtension(MESSAGE_TO_EXTENSION_SHOW_FLAG, { show: iframe.style.display !== 'none' }) } else { @@ -356,7 +356,7 @@ const debug = (...args: any[]) => { } // 初始化injectMessage - runtime.injectMessage.init(methods) + runtime.injectMessaging.init(methods) setInterval(() => { if (!sidePanel) { diff --git a/src/messaging/layer2/ExtensionMessage.ts b/src/messaging/layer2/ExtensionMessaging.ts similarity index 98% rename from src/messaging/layer2/ExtensionMessage.ts rename to src/messaging/layer2/ExtensionMessaging.ts index 144a3a7..7931ba2 100644 --- a/src/messaging/layer2/ExtensionMessage.ts +++ b/src/messaging/layer2/ExtensionMessaging.ts @@ -17,7 +17,7 @@ type L2MethodHandlers = { [key: string]: L2MethodHandler } -class ExtensionMessage { +class ExtensionMessaging { portIdToPort: Map> = new Map() methods?: L2MethodHandlers @@ -130,4 +130,4 @@ class ExtensionMessage { } } -export default ExtensionMessage \ No newline at end of file +export default ExtensionMessaging \ No newline at end of file diff --git a/src/messaging/layer2/InjectMessage.ts b/src/messaging/layer2/InjectMessaging.ts similarity index 98% rename from src/messaging/layer2/InjectMessage.ts rename to src/messaging/layer2/InjectMessaging.ts index 6cbaa51..48b3a2a 100644 --- a/src/messaging/layer2/InjectMessage.ts +++ b/src/messaging/layer2/InjectMessaging.ts @@ -1,7 +1,7 @@ import Layer1Protocol from '../layer1/Layer1Protocol' import { L2ReqMsg, L2ResMsg, MESSAGE_TO_EXTENSION_HANDSHAKE, MESSAGE_TO_EXTENSION_ROUTE, TAG_TARGET_APP, TAG_TARGET_INJECT } from '../const' -class InjectMessage { +class InjectMessaging { port?: chrome.runtime.Port portMessageHandler?: Layer1Protocol //类实例 @@ -103,4 +103,4 @@ class InjectMessage { } -export default InjectMessage \ No newline at end of file +export default InjectMessaging \ No newline at end of file diff --git a/src/messaging/layer2/useMessage.ts b/src/messaging/layer2/useMessaging.ts similarity index 90% rename from src/messaging/layer2/useMessage.ts rename to src/messaging/layer2/useMessaging.ts index f50a797..167f3f1 100644 --- a/src/messaging/layer2/useMessage.ts +++ b/src/messaging/layer2/useMessaging.ts @@ -1,9 +1,9 @@ -import { msgWaiter } from './useMessageService' +import { msgWaiter } from './useMessagingService' import { useCallback } from 'react' import Layer1Protocol from '../layer1/Layer1Protocol' import { L2ReqMsg, L2ResMsg, MESSAGE_TO_EXTENSION_ROUTE, TAG_TARGET_INJECT } from '../const' -const useMessage = () => { +const useMessaging = () => { const sendExtension = useCallback(async (method: string, params?: any) => { // wait const pmh = await msgWaiter.wait() as Layer1Protocol @@ -34,4 +34,4 @@ const useMessage = () => { } } -export default useMessage \ No newline at end of file +export default useMessaging diff --git a/src/messaging/layer2/useMessageService.ts b/src/messaging/layer2/useMessagingService.ts similarity index 97% rename from src/messaging/layer2/useMessageService.ts rename to src/messaging/layer2/useMessagingService.ts index 3c5e43b..f24510c 100644 --- a/src/messaging/layer2/useMessageService.ts +++ b/src/messaging/layer2/useMessagingService.ts @@ -16,7 +16,7 @@ export const msgWaiter = new Waiter>(() => ({ data: portMessageHandler!, }), 100, 15000) -const useMessageService = (methods?: { +const useMessagingService = (methods?: { [key: string]: (params: any, context: MethodContext) => Promise }) => { const messageHandler = useCallback(async (req: L2ReqMsg): Promise => { @@ -90,4 +90,4 @@ const useMessageService = (methods?: { }, [messageHandler, port]) } -export default useMessageService +export default useMessagingService diff --git a/src/pages/MainPage.tsx b/src/pages/MainPage.tsx index 6e08419..f4cc3d1 100644 --- a/src/pages/MainPage.tsx +++ b/src/pages/MainPage.tsx @@ -8,7 +8,7 @@ import {EventBusContext} from '../Router' import useTranslateService from '../hooks/useTranslateService' import {setTheme} from '../utils/biz_util' import useSearchService from '../hooks/useSearchService' -import useMessage from '../messaging/layer2/useMessage' +import useMessage from '../messaging/layer2/useMessaging' import {setFold} from '../redux/envReducer' function App() { diff --git a/src/pages/OptionsPage.tsx b/src/pages/OptionsPage.tsx index 128a654..33dd28a 100644 --- a/src/pages/OptionsPage.tsx +++ b/src/pages/OptionsPage.tsx @@ -29,7 +29,7 @@ import classNames from 'classnames' import toast from 'react-hot-toast' import {useBoolean, useEventTarget} from 'ahooks' import {useEventChecked} from '@kky002/kky-hooks' -import useMessage from '@/messaging/layer2/useMessage' +import useMessaging from '@/messaging/layer2/useMessaging' const Section = (props: { title: ShowElement @@ -61,7 +61,7 @@ const FormItem = (props: { const OptionsPage = () => { const dispatch = useAppDispatch() const envData = useAppSelector(state => state.env.envData) - const {sendExtension} = useMessage() + const {sendExtension} = useMessaging() const {value: sidePanelValue, onChange: setSidePanelValue} = useEventChecked(envData.sidePanel) const {value: autoInsertValue, onChange: setAutoInsertValue} = useEventChecked(!envData.manualInsert) const {value: autoExpandValue, onChange: setAutoExpandValue} = useEventChecked(envData.autoExpand)