From e5a645611b5c768d94edfa0ea2aec41581515f3f Mon Sep 17 00:00:00 2001 From: IndieKKY Date: Sun, 6 Oct 2024 18:48:52 +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/chrome/background.ts | 2 +- src/components/MoreBtn.tsx | 2 +- src/consts/const.ts | 20 --------------- src/hooks/useMessageService.ts | 7 +++--- src/hooks/useSubtitle.ts | 1 - src/hooks/useSubtitleService.ts | 3 +-- src/hooks/useTranslate.ts | 2 -- src/inject/inject.ts | 27 ++++++++++----------- src/messaging/layer2/InjectMessaging.ts | 2 +- src/messaging/layer2/useMessagingService.ts | 4 +-- src/pages/OptionsPage.tsx | 5 +--- 11 files changed, 23 insertions(+), 52 deletions(-) diff --git a/src/chrome/background.ts b/src/chrome/background.ts index d074070..dd646f5 100644 --- a/src/chrome/background.ts +++ b/src/chrome/background.ts @@ -1,6 +1,6 @@ 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 { STORAGE_ENV} from '@/consts/const' import ExtensionMessaging from '@/messaging/layer2/ExtensionMessaging' import { TAG_TARGET_INJECT } from '@/messaging/const' diff --git a/src/components/MoreBtn.tsx b/src/components/MoreBtn.tsx index 1d2037a..555c91c 100644 --- a/src/components/MoreBtn.tsx +++ b/src/components/MoreBtn.tsx @@ -13,7 +13,7 @@ import {Placement} from '@popperjs/core/lib/enums' import {useAppDispatch, useAppSelector} from '../hooks/redux' import {setEnvData, setTempData} from '../redux/envReducer' import {EventBusContext} from '../Router' -import {EVENT_EXPAND, MESSAGE_TO_INJECT_DOWNLOAD_AUDIO} from '../consts/const' +import {EVENT_EXPAND} from '../consts/const' import {formatSrtTime, formatTime, formatVttTime} from '../utils/util' import {downloadText, openUrl} from '@kky002/kky-util' import toast from 'react-hot-toast' diff --git a/src/consts/const.ts b/src/consts/const.ts index d1daf7d..4be1b11 100644 --- a/src/consts/const.ts +++ b/src/consts/const.ts @@ -1,23 +1,3 @@ -export const MESSAGE_TO_EXTENSION_CLOSE_SIDE_PANEL = 'closeSidePanel' -export const MESSAGE_TO_EXTENSION_ADD_TASK = 'addTask' -export const MESSAGE_TO_EXTENSION_GET_TASK = 'getTask' -export const MESSAGE_TO_EXTENSION_SHOW_FLAG = 'showFlag' - -export const MESSAGE_TO_INJECT_TOGGLE_DISPLAY = 'toggleDisplay' -export const MESSAGE_TO_INJECT_FOLD = 'fold' -export const MESSAGE_TO_INJECT_MOVE = 'move' -export const MESSAGE_TO_INJECT_PLAY = 'play' -export const MESSAGE_TO_INJECT_DOWNLOAD_AUDIO = 'downloadAudio' -export const MESSAGE_TO_INJECT_GET_VIDEO_STATUS = 'getVideoStatus' -export const MESSAGE_TO_INJECT_GET_VIDEO_ELEMENT_INFO = 'getVideoElementInfo' -export const MESSAGE_TO_INJECT_REFRESH_VIDEO_INFO = 'refreshVideoInfo' -export const MESSAGE_TO_INJECT_UPDATETRANSRESULT = 'updateTransResult' -export const MESSAGE_TO_INJECT_HIDE_TRANS = 'hideTrans' -export const MESSAGE_TO_INJECT_GET_SUBTITLE = 'getSubtitle' - -export const MESSAGE_TO_APP_SET_INFOS = 'setInfos' -export const MESSAGE_TO_APP_SET_VIDEO_INFO = 'setVideoInfo' - export const EVENT_EXPAND = 'expand' export const APP_DOM_ID = 'bilibili-subtitle' diff --git a/src/hooks/useMessageService.ts b/src/hooks/useMessageService.ts index 7994964..f0f7bdd 100644 --- a/src/hooks/useMessageService.ts +++ b/src/hooks/useMessageService.ts @@ -1,7 +1,6 @@ import { setCurFetched, setCurInfo, setData, setInfos, setTitle, setUrl } from '@/redux/envReducer' import { useMemo } from 'react' import { useAppDispatch } from './redux' -import { MESSAGE_TO_APP_SET_INFOS, MESSAGE_TO_APP_SET_VIDEO_INFO } from '@/consts/const' import useMessagingService from '@/messaging/layer2/useMessagingService' const useMessageService = () => { @@ -9,15 +8,15 @@ const useMessageService = () => { //methods const methods: { - [key: string]: (params: any, context: MethodContext) => Promise + [K in AllAPPMessages['method']]: (params: Extract['params'], context: MethodContext) => Promise } = useMemo(() => ({ - [MESSAGE_TO_APP_SET_INFOS]: async (params: any, context: MethodContext) => { + SET_INFOS: async (params, context: MethodContext) => { dispatch(setInfos(params.infos)) dispatch(setCurInfo(undefined)) dispatch(setCurFetched(false)) dispatch(setData(undefined)) }, - [MESSAGE_TO_APP_SET_VIDEO_INFO]: async (params: any, context: MethodContext) => { + SET_VIDEO_INFO: async (params, context: MethodContext) => { dispatch(setInfos(params.infos)) dispatch(setUrl(params.url)) dispatch(setTitle(params.title)) diff --git a/src/hooks/useSubtitle.ts b/src/hooks/useSubtitle.ts index 6399a4a..bae5223 100644 --- a/src/hooks/useSubtitle.ts +++ b/src/hooks/useSubtitle.ts @@ -1,7 +1,6 @@ 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 useMessaging from '../messaging/layer2/useMessaging' const useSubtitle = () => { const dispatch = useAppDispatch() diff --git a/src/hooks/useSubtitleService.ts b/src/hooks/useSubtitleService.ts index dc742ea..4686705 100644 --- a/src/hooks/useSubtitleService.ts +++ b/src/hooks/useSubtitleService.ts @@ -16,10 +16,9 @@ import { setTempData, } from '../redux/envReducer' import {EventBusContext} from '../Router' -import {EVENT_EXPAND, GEMINI_TOKENS, TOTAL_HEIGHT_MAX, TOTAL_HEIGHT_MIN, WORDS_MIN, WORDS_RATE, MESSAGE_TO_INJECT_GET_VIDEO_STATUS, MESSAGE_TO_INJECT_GET_VIDEO_ELEMENT_INFO, MESSAGE_TO_INJECT_REFRESH_VIDEO_INFO, MESSAGE_TO_INJECT_HIDE_TRANS, MESSAGE_TO_INJECT_UPDATETRANSRESULT, TOTAL_HEIGHT_DEF} from '../consts/const' +import {EVENT_EXPAND, GEMINI_TOKENS, TOTAL_HEIGHT_MAX, TOTAL_HEIGHT_MIN, WORDS_MIN, WORDS_RATE} from '../consts/const' import {useAsyncEffect, useInterval} from 'ahooks' import {getModelMaxTokens, getWholeText} from '../utils/bizUtil' -import {MESSAGE_TO_INJECT_GET_SUBTITLE} from '../consts/const' import useMessaging from '../messaging/layer2/useMessaging' /** diff --git a/src/hooks/useTranslate.ts b/src/hooks/useTranslate.ts index 05953e8..ebf3d13 100644 --- a/src/hooks/useTranslate.ts +++ b/src/hooks/useTranslate.ts @@ -16,8 +16,6 @@ import { import { LANGUAGE_DEFAULT, LANGUAGES_MAP, - MESSAGE_TO_EXTENSION_ADD_TASK, - MESSAGE_TO_EXTENSION_GET_TASK, PROMPT_DEFAULTS, PROMPT_TYPE_ASK, PROMPT_TYPE_TRANSLATE, diff --git a/src/inject/inject.ts b/src/inject/inject.ts index 5e4b324..1125669 100644 --- a/src/inject/inject.ts +++ b/src/inject/inject.ts @@ -1,5 +1,4 @@ -import { TOTAL_HEIGHT_DEF, HEADER_HEIGHT, TOTAL_HEIGHT_MIN, TOTAL_HEIGHT_MAX, IFRAME_ID, MESSAGE_TO_INJECT_DOWNLOAD_AUDIO, 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_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 { TOTAL_HEIGHT_DEF, HEADER_HEIGHT, TOTAL_HEIGHT_MIN, TOTAL_HEIGHT_MAX, IFRAME_ID, STORAGE_ENV } from '@/consts/const' import InjectMessaging from '@/messaging/layer2/InjectMessaging' const debug = (...args: any[]) => { @@ -241,9 +240,9 @@ const debug = (...args: any[]) => { } const methods: { - [key: string]: (params: any, context: MethodContext) => Promise + [K in AllInjectMessages['method']]: (params: Extract['params'], context: MethodContext) => Promise } = { - [MESSAGE_TO_INJECT_TOGGLE_DISPLAY]: async (params) => { + TOGGLE_DISPLAY: async (params) => { const iframe = document.getElementById(IFRAME_ID) as HTMLIFrameElement | undefined if (iframe != null) { iframe.style.display = iframe.style.display === 'none' ? 'block' : 'none' @@ -254,11 +253,11 @@ const debug = (...args: any[]) => { createIframe() } }, - [MESSAGE_TO_INJECT_FOLD]: async (params) => { + FOLD: async (params) => { runtime.fold = params.fold updateIframeHeight() }, - [MESSAGE_TO_INJECT_MOVE]: async (params) => { + MOVE: async (params) => { const video = getVideoElement() if (video != null) { video.currentTime = params.time @@ -267,14 +266,14 @@ const debug = (...args: any[]) => { } } }, - [MESSAGE_TO_INJECT_GET_SUBTITLE]: async (params) => { + GET_SUBTITLE: async (params) => { let url = params.info.subtitle_url if (url.startsWith('http://')) { url = url.replace('http://', 'https://') } return await fetch(url).then(res => res.json()) }, - [MESSAGE_TO_INJECT_GET_VIDEO_STATUS]: async (params) => { + GET_VIDEO_STATUS: async (params) => { const video = getVideoElement() if (video != null) { return { @@ -283,17 +282,17 @@ const debug = (...args: any[]) => { } } }, - [MESSAGE_TO_INJECT_GET_VIDEO_ELEMENT_INFO]: async (params) => { + GET_VIDEO_ELEMENT_INFO: async (params) => { refreshVideoElement() return { noVideo: runtime.videoElement == null, totalHeight: runtime.videoElementHeight, } }, - [MESSAGE_TO_INJECT_REFRESH_VIDEO_INFO]: async (params) => { + REFRESH_VIDEO_INFO: async (params) => { refreshVideoInfo(params.force) }, - [MESSAGE_TO_INJECT_UPDATETRANSRESULT]: async (params) => { + UPDATE_TRANS_RESULT: async (params) => { runtime.showTrans = true runtime.curTrans = params?.result @@ -321,7 +320,7 @@ const debug = (...args: any[]) => { } text && (text.style.display = runtime.curTrans ? 'block' : 'none') }, - [MESSAGE_TO_INJECT_HIDE_TRANS]: async (params) => { + HIDE_TRANS: async (params) => { runtime.showTrans = false runtime.curTrans = undefined @@ -330,7 +329,7 @@ const debug = (...args: any[]) => { text.style.display = 'none' } }, - [MESSAGE_TO_INJECT_PLAY]: async (params) => { + PLAY: async (params) => { const { play } = params const video = getVideoElement() if (video != null) { @@ -341,7 +340,7 @@ const debug = (...args: any[]) => { } } }, - [MESSAGE_TO_INJECT_DOWNLOAD_AUDIO]: async (params) => { + DOWNLOAD_AUDIO: async (params) => { const html = document.getElementsByTagName('html')[0].innerHTML const playInfo = JSON.parse(html.match(/window.__playinfo__=(.+?)<\/script/)?.[1] ?? '{}') const audioUrl = playInfo.data.dash.audio[0].baseUrl diff --git a/src/messaging/layer2/InjectMessaging.ts b/src/messaging/layer2/InjectMessaging.ts index 5230d19..5b14346 100644 --- a/src/messaging/layer2/InjectMessaging.ts +++ b/src/messaging/layer2/InjectMessaging.ts @@ -59,7 +59,7 @@ class InjectMessaging { } init(methods: { - [key: string]: (params: any, context: MethodContext) => Promise + [K in AllInjectMessages['method']]: (params: Extract['params'], context: MethodContext) => Promise }) { this.methods = methods this.port = chrome.runtime.connect(import.meta.env.VITE_EXTENSION_ID, { diff --git a/src/messaging/layer2/useMessagingService.ts b/src/messaging/layer2/useMessagingService.ts index 069ab45..8e82b02 100644 --- a/src/messaging/layer2/useMessagingService.ts +++ b/src/messaging/layer2/useMessagingService.ts @@ -17,7 +17,7 @@ export const msgWaiter = new Waiter>(() => ({ }), 100, 15000) const useMessagingService = (methods?: { - [key: string]: (params: any, context: MethodContext) => Promise + [K in AllAPPMessages['method']]: (params: Extract['params'], context: MethodContext) => Promise }) => { const messageHandler = useCallback(async (req: L2ReqMsg): Promise => { debug(`[${req.from}] ${req.method}`, JSON.stringify(req)) @@ -28,7 +28,7 @@ const useMessagingService = (methods?: { // msg: 'Target Error: ' + req.target, // } - const method = methods?.[req.method] + const method = methods?.[req.method as keyof typeof methods] if (method != null) { return method(req.params, { from: req.from, diff --git a/src/pages/OptionsPage.tsx b/src/pages/OptionsPage.tsx index dbe2452..b5ff66b 100644 --- a/src/pages/OptionsPage.tsx +++ b/src/pages/OptionsPage.tsx @@ -1,20 +1,17 @@ import React, {PropsWithChildren, useCallback, useMemo, useState} from 'react' -import {setEnvData, setTempData} from '../redux/envReducer' +import {setEnvData} from '../redux/envReducer' import {useAppDispatch, useAppSelector} from '../hooks/redux' import { ASK_ENABLED_DEFAULT, CUSTOM_MODEL_TOKENS, DEFAULT_SERVER_URL_OPENAI, GEMINI_TOKENS, - HEADER_HEIGHT, LANGUAGE_DEFAULT, LANGUAGES, - MESSAGE_TO_EXTENSION_CLOSE_SIDE_PANEL, MODEL_DEFAULT, MODEL_MAP, MODEL_TIP, MODELS, - PAGE_MAIN, PROMPT_DEFAULTS, PROMPT_TYPES, SUMMARIZE_LANGUAGE_DEFAULT,