diff --git a/src/components/MoreBtn.tsx b/src/components/MoreBtn.tsx index 555c91c..55e46fd 100644 --- a/src/components/MoreBtn.tsx +++ b/src/components/MoreBtn.tsx @@ -19,6 +19,7 @@ import {downloadText, openUrl} from '@kky002/kky-util' import toast from 'react-hot-toast' import {getSummarize} from '../utils/bizUtil' import useMessaging from '../messaging/layer2/useMessaging' +import { useMessage } from '@/hooks/message' interface Props { placement: Placement } @@ -70,7 +71,7 @@ const MoreBtn = (props: Props) => { const title = useAppSelector(state => state.env.title) const curSummaryType = useAppSelector(state => state.env.tempData.curSummaryType) - const {sendInject} = useMessaging() + const {sendInject} = useMessage() const downloadCallback = useCallback((download: boolean) => { if (data == null) { diff --git a/src/hooks/message.ts b/src/hooks/message.ts new file mode 100644 index 0000000..72a7a0d --- /dev/null +++ b/src/hooks/message.ts @@ -0,0 +1,3 @@ +import useMessaging from "@/messaging/layer2/useMessaging"; + +export const useMessage = useMessaging diff --git a/src/hooks/useSubtitle.ts b/src/hooks/useSubtitle.ts index bae5223..7ddfa6f 100644 --- a/src/hooks/useSubtitle.ts +++ b/src/hooks/useSubtitle.ts @@ -1,13 +1,13 @@ import {useAppDispatch, useAppSelector} from './redux' import React, {useCallback} from 'react' import {setNeedScroll, setReviewAction, setTempData} from '../redux/envReducer' -import useMessaging from '../messaging/layer2/useMessaging' +import { useMessage } from './message' const useSubtitle = () => { const dispatch = useAppDispatch() const reviewed = useAppSelector(state => state.env.tempData.reviewed) const reviewAction = useAppSelector(state => state.env.reviewAction) const reviewActions = useAppSelector(state => state.env.tempData.reviewActions) - const {sendInject} = useMessaging() + const {sendInject} = useMessage() const move = useCallback((time: number, togglePause: boolean) => { sendInject('MOVE', {time, togglePause}) diff --git a/src/hooks/useSubtitleService.ts b/src/hooks/useSubtitleService.ts index 4686705..2178390 100644 --- a/src/hooks/useSubtitleService.ts +++ b/src/hooks/useSubtitleService.ts @@ -20,6 +20,7 @@ import {EVENT_EXPAND, GEMINI_TOKENS, TOTAL_HEIGHT_MAX, TOTAL_HEIGHT_MIN, WORDS_M import {useAsyncEffect, useInterval} from 'ahooks' import {getModelMaxTokens, getWholeText} from '../utils/bizUtil' import useMessaging from '../messaging/layer2/useMessaging' +import { useMessage } from './message' /** * Service是单例,类似后端的服务概念 @@ -43,7 +44,7 @@ const useSubtitleService = () => { const autoTranslate = useAppSelector(state => state.env.autoTranslate) const reviewed = useAppSelector(state => state.env.tempData.reviewed) const reviewActions = useAppSelector(state => state.env.tempData.reviewActions) - const {sendInject} = useMessaging() + const {sendInject} = useMessage() //如果reviewActions达到15次,则设置reviewed为false useEffect(() => { diff --git a/src/hooks/useTranslate.ts b/src/hooks/useTranslate.ts index ebf3d13..e51ff61 100644 --- a/src/hooks/useTranslate.ts +++ b/src/hooks/useTranslate.ts @@ -30,6 +30,7 @@ import {useMemoizedFn} from 'ahooks/es' import {extractJsonArray, extractJsonObject, getModel} from '../utils/bizUtil' import {formatTime} from '../utils/util' import useMessaging from '@/messaging/layer2/useMessaging' +import { useMessage } from './message' const useTranslate = () => { const dispatch = useAppDispatch() const data = useAppSelector(state => state.env.data) @@ -43,7 +44,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} = useMessaging() + const {sendExtension} = useMessage() /** * 获取下一个需要翻译的行 * 会检测冷却 diff --git a/src/inject/inject.ts b/src/inject/inject.ts index 1125669..44cbca2 100644 --- a/src/inject/inject.ts +++ b/src/inject/inject.ts @@ -29,7 +29,7 @@ const debug = (...args: any[]) => { } const runtime: { - injectMessaging: InjectMessaging + injectMessaging: InjectMessaging // lastV?: string | null // lastVideoInfo?: VideoInfo diff --git a/src/messaging/layer2/ExtensionMessaging.ts b/src/messaging/layer2/ExtensionMessaging.ts index ee8e2b3..a4748db 100644 --- a/src/messaging/layer2/ExtensionMessaging.ts +++ b/src/messaging/layer2/ExtensionMessaging.ts @@ -17,7 +17,7 @@ type L2MethodHandlers = { [K in M['method']]: L2MethodHandler } -class ExtensionMessaging { +class ExtensionMessaging { portIdToPort: Map> = new Map() methods?: L2MethodHandlers @@ -98,7 +98,7 @@ class ExtensionMessaging { //tags 如果为null,则不检查tags,如果为空数组,则不会发送消息 //返回:最后一个响应(因此如果只发送给一个tab,则返回的是该tab的响应) - broadcastMessageExact = async (tabIds: number[], tags: string[] | null, method: K, params?: Extract['params']): Promise['return']> => { + broadcastMessageExact = async (tabIds: number[], tags: string[] | null, method: K, params?: Extract['params']): Promise['return']> => { // 如果tags为空数组,则不会发送消息 if (tags != null && tags.length === 0) { return @@ -121,7 +121,7 @@ class ExtensionMessaging { return res?.data } - broadcastMessage = async (ignoreTabIds: number[] | undefined | null, tags: string[], method: K, params?: Extract['params']): Promise['return']> => { + broadcastMessage = async (ignoreTabIds: number[] | undefined | null, tags: string[], method: K, params?: Extract['params']): Promise['return']> => { const tabs = await chrome.tabs.query({ discarded: false, }) diff --git a/src/messaging/layer2/InjectMessaging.ts b/src/messaging/layer2/InjectMessaging.ts index a62a692..3408dd6 100644 --- a/src/messaging/layer2/InjectMessaging.ts +++ b/src/messaging/layer2/InjectMessaging.ts @@ -1,12 +1,12 @@ 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 InjectMessaging { +class InjectMessaging { port?: chrome.runtime.Port l1protocol?: Layer1Protocol //类实例 methods?: { - [K in AllInjectMessages['method']]: (params: Extract['params'], context: MethodContext) => Promise + [K in AllInjectMessagesType['method']]: (params: Extract['params'], context: MethodContext) => Promise } debug = (...args: any[]) => { @@ -59,7 +59,7 @@ class InjectMessaging { } init(methods: { - [K in AllInjectMessages['method']]: (params: Extract['params'], context: MethodContext) => Promise + [K in AllInjectMessagesType['method']]: (params: Extract['params'], context: MethodContext) => Promise }) { this.methods = methods this.port = chrome.runtime.connect(import.meta.env.VITE_EXTENSION_ID, { @@ -76,7 +76,7 @@ class InjectMessaging { }) } - sendExtension = async (method: K, params?: Extract['params']): Promise['return']> => { + sendExtension = async (method: K, params?: Extract['params']): Promise['return']> => { return await this.l1protocol!.sendMessage({ from: 'inject', method, @@ -90,8 +90,8 @@ class InjectMessaging { }) } - sendApp = async (method: K, params?: Extract['params']): Promise['return']> => { - return this.sendExtension('ROUTE', { + sendApp = async (method: K, params?: Extract['params']): Promise['return']> => { + return this.sendExtension('ROUTE' as any, { tags: [TAG_TARGET_APP], method, params, diff --git a/src/messaging/layer2/useMessaging.ts b/src/messaging/layer2/useMessaging.ts index be1d68f..eff66cb 100644 --- a/src/messaging/layer2/useMessaging.ts +++ b/src/messaging/layer2/useMessaging.ts @@ -3,8 +3,8 @@ import { useCallback } from 'react' import Layer1Protocol from '../layer1/Layer1Protocol' import { L2ReqMsg, L2ResMsg, TAG_TARGET_INJECT } from '../const' -const useMessaging = () => { - const sendExtension = useCallback(async (method: K, params?: Extract['params']): Promise['return']> => { +const useMessaging = () => { + const sendExtension = useCallback(async (method: K, params?: Extract['params']): Promise['return']> => { // wait const pmh = await msgWaiter.wait() as Layer1Protocol // send message @@ -20,8 +20,8 @@ const useMessaging = () => { } }, []) - const sendInject = useCallback(async (method: K, params?: Extract['params']): Promise['return']> => { - return await sendExtension('ROUTE', { + const sendInject = useCallback(async (method: K, params?: Extract['params']): Promise['return']> => { + return await sendExtension('ROUTE' as any, { tags: [TAG_TARGET_INJECT], method, params: params ?? {}, diff --git a/src/messaging/layer2/useMessagingService.ts b/src/messaging/layer2/useMessagingService.ts index 8e82b02..d5e2ec9 100644 --- a/src/messaging/layer2/useMessagingService.ts +++ b/src/messaging/layer2/useMessagingService.ts @@ -1,7 +1,7 @@ import { useCallback, useMemo } from 'react' import { Waiter } from '@kky002/kky-util' import Layer1Protocol from '../layer1/Layer1Protocol' -import { L2ReqMsg, L2ResMsg, MESSAGE_TO_EXTENSION_HANDSHAKE, TAG_TARGET_APP } from '../const' +import { L2ReqMsg, L2ResMsg, TAG_TARGET_APP } from '../const' const debug = (...args: any[]) => { console.debug('[App Messaging]', ...args) @@ -16,8 +16,8 @@ export const msgWaiter = new Waiter>(() => ({ data: l1protocol!, }), 100, 15000) -const useMessagingService = (methods?: { - [K in AllAPPMessages['method']]: (params: Extract['params'], context: MethodContext) => Promise +const useMessagingService = (methods?: { + [K in AllAPPMessagesType['method']]: (params: Extract['params'], context: MethodContext) => Promise }) => { const messageHandler = useCallback(async (req: L2ReqMsg): Promise => { debug(`[${req.from}] ${req.method}`, JSON.stringify(req)) diff --git a/src/pages/MainPage.tsx b/src/pages/MainPage.tsx index 76d63de..5ed2474 100644 --- a/src/pages/MainPage.tsx +++ b/src/pages/MainPage.tsx @@ -8,15 +8,15 @@ import {EventBusContext} from '../Router' import useTranslateService from '../hooks/useTranslateService' import {setTheme} from '../utils/bizUtil' import useSearchService from '../hooks/useSearchService' -import useMessaging from '../messaging/layer2/useMessaging' import {setFold} from '../redux/envReducer' +import { useMessage } from '@/hooks/message' function App() { const dispatch = useAppDispatch() const fold = useAppSelector(state => state.env.fold) const eventBus = useContext(EventBusContext) const totalHeight = useAppSelector(state => state.env.totalHeight) - const {sendInject} = useMessaging() + const {sendInject} = useMessage() const envData = useAppSelector(state => state.env.envData) const foldCallback = useCallback(() => { diff --git a/src/pages/OptionsPage.tsx b/src/pages/OptionsPage.tsx index b5ff66b..9127867 100644 --- a/src/pages/OptionsPage.tsx +++ b/src/pages/OptionsPage.tsx @@ -27,6 +27,7 @@ import toast from 'react-hot-toast' import {useBoolean, useEventTarget} from 'ahooks' import {useEventChecked} from '@kky002/kky-hooks' import useMessaging from '@/messaging/layer2/useMessaging' +import { useMessage } from '@/hooks/message' const Section = (props: { title: ShowElement @@ -58,7 +59,7 @@ const FormItem = (props: { const OptionsPage = () => { const dispatch = useAppDispatch() const envData = useAppSelector(state => state.env.envData) - const {sendExtension} = useMessaging() + const {sendExtension} = useMessage() const {value: sidePanelValue, onChange: setSidePanelValue} = useEventChecked(envData.sidePanel) const {value: autoInsertValue, onChange: setAutoInsertValue} = useEventChecked(!envData.manualInsert) const {value: autoExpandValue, onChange: setAutoExpandValue} = useEventChecked(envData.autoExpand)