This commit is contained in:
IndieKKY
2024-10-06 13:38:08 +08:00
parent b690a7ef08
commit 3140b91f72
14 changed files with 35 additions and 35 deletions

View File

@@ -8,7 +8,7 @@ import OptionsPage from './pages/OptionsPage'
import {handleJson} from '@kky002/kky-util' import {handleJson} from '@kky002/kky-util'
import {useLocalStorage} from '@kky002/kky-hooks' import {useLocalStorage} from '@kky002/kky-hooks'
import {Toaster} from 'react-hot-toast' import {Toaster} from 'react-hot-toast'
import useMessagingService from './hooks/useMessagingService' import useMessagingService from './hooks/useMessageService'
import MainPage from './pages/MainPage' import MainPage from './pages/MainPage'
function App() { function App() {

View File

@@ -1,7 +1,7 @@
import {v4} from 'uuid' import {v4} from 'uuid'
import {handleTask, initTaskService, tasksMap} from './taskService' 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 { 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' import { TAG_TARGET_INJECT } from '@/messaging/const'
const setBadgeOk = async (tabId: number, ok: boolean) => { const setBadgeOk = async (tabId: number, ok: boolean) => {
@@ -75,8 +75,8 @@ const methods: {
}, },
} }
// 初始化backgroundMessage // 初始化backgroundMessage
const extensionMessage = new ExtensionMessage() const extensionMessaging = new ExtensionMessaging()
extensionMessage.init(methods) extensionMessaging.init(methods)
chrome.runtime.onMessage.addListener((event: MessageData, sender: chrome.runtime.MessageSender, sendResponse: (result: any) => void) => { chrome.runtime.onMessage.addListener((event: MessageData, sender: chrome.runtime.MessageSender, sendResponse: (result: any) => void) => {
// debug((sender.tab != null) ? `tab ${sender.tab.url ?? ''} => ` : 'extension => ', event) // debug((sender.tab != null) ? `tab ${sender.tab.url ?? ''} => ` : 'extension => ', event)
@@ -115,7 +115,7 @@ chrome.action.onClicked.addListener(async (tab) => {
}) })
} else { } else {
closeSidePanel() 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)
} }
}) })
}) })

View File

@@ -18,7 +18,7 @@ import {formatSrtTime, formatTime, formatVttTime} from '../utils/util'
import {downloadText, openUrl} from '@kky002/kky-util' import {downloadText, openUrl} from '@kky002/kky-util'
import toast from 'react-hot-toast' import toast from 'react-hot-toast'
import {getSummarize} from '../utils/biz_util' import {getSummarize} from '../utils/biz_util'
import useMessage from '../messaging/layer2/useMessage' import useMessage from '../messaging/layer2/useMessaging'
interface Props { interface Props {
placement: Placement placement: Placement
} }

View File

@@ -2,9 +2,9 @@ import { setCurFetched, setCurInfo, setData, setInfos, setTitle, setUrl } from '
import { useMemo } from 'react' import { useMemo } from 'react'
import { useAppDispatch } from './redux' import { useAppDispatch } from './redux'
import { MESSAGE_TO_APP_SET_INFOS, MESSAGE_TO_APP_SET_VIDEO_INFO } from '@/consts/const' 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() const dispatch = useAppDispatch()
//methods //methods
@@ -25,7 +25,7 @@ const useMessagingService = () => {
}, },
}), [dispatch]) }), [dispatch])
useMessageService(methods) useMessagingService(methods)
} }
export default useMessagingService export default useMessageService

View File

@@ -2,7 +2,7 @@ import {useAppDispatch, useAppSelector} from './redux'
import React, {useCallback} from 'react' import React, {useCallback} from 'react'
import {setNeedScroll, setReviewAction, setTempData} from '../redux/envReducer' import {setNeedScroll, setReviewAction, setTempData} from '../redux/envReducer'
import {MESSAGE_TO_INJECT_MOVE} from '../consts/const' import {MESSAGE_TO_INJECT_MOVE} from '../consts/const'
import useMessage from '../messaging/layer2/useMessage' import useMessage from '../messaging/layer2/useMessaging'
const useSubtitle = () => { const useSubtitle = () => {
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
const reviewed = useAppSelector(state => state.env.tempData.reviewed) const reviewed = useAppSelector(state => state.env.tempData.reviewed)

View File

@@ -20,8 +20,8 @@ import {EVENT_EXPAND, GEMINI_TOKENS, TOTAL_HEIGHT_MAX, TOTAL_HEIGHT_MIN, WORDS_M
import {useAsyncEffect, useInterval} from 'ahooks' import {useAsyncEffect, useInterval} from 'ahooks'
import {getModelMaxTokens, getWholeText} from '../utils/biz_util' import {getModelMaxTokens, getWholeText} from '../utils/biz_util'
import {MESSAGE_TO_INJECT_GET_SUBTITLE} from '../consts/const' import {MESSAGE_TO_INJECT_GET_SUBTITLE} from '../consts/const'
import useMessage from '../messaging/layer2/useMessage' import useMessage from '../messaging/layer2/useMessaging'
import { msgWaiter } from '@/messaging/layer2/useMessageService' import { msgWaiter } from '@/messaging/layer2/useMessagingService'
/** /**
* Service是单例类似后端的服务概念 * Service是单例类似后端的服务概念

View File

@@ -31,7 +31,7 @@ import toast from 'react-hot-toast'
import {useMemoizedFn} from 'ahooks/es' import {useMemoizedFn} from 'ahooks/es'
import {extractJsonArray, extractJsonObject, getModel} from '../utils/biz_util' import {extractJsonArray, extractJsonObject, getModel} from '../utils/biz_util'
import {formatTime} from '../utils/util' import {formatTime} from '../utils/util'
import useMessage from '@/messaging/layer2/useMessage' import useMessaging from '@/messaging/layer2/useMessaging'
const useTranslate = () => { const useTranslate = () => {
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
const data = useAppSelector(state => state.env.data) const data = useAppSelector(state => state.env.data)
@@ -45,7 +45,7 @@ const useTranslate = () => {
const reviewed = useAppSelector(state => state.env.tempData.reviewed) const reviewed = useAppSelector(state => state.env.tempData.reviewed)
const reviewAction = useAppSelector(state => state.env.reviewAction) const reviewAction = useAppSelector(state => state.env.reviewAction)
const reviewActions = useAppSelector(state => state.env.tempData.reviewActions) const reviewActions = useAppSelector(state => state.env.tempData.reviewActions)
const {sendExtension} = useMessage() const {sendExtension} = useMessaging()
/** /**
* 获取下一个需要翻译的行 * 获取下一个需要翻译的行
* 会检测冷却 * 会检测冷却

View File

@@ -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 { 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 { 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[]) => { const debug = (...args: any[]) => {
console.debug('[Inject]', ...args) console.debug('[Inject]', ...args)
@@ -30,7 +30,7 @@ const debug = (...args: any[]) => {
} }
const runtime: { const runtime: {
injectMessage: InjectMessage injectMessaging: InjectMessaging
// lastV?: string | null // lastV?: string | null
// lastVideoInfo?: VideoInfo // lastVideoInfo?: VideoInfo
@@ -42,7 +42,7 @@ const debug = (...args: any[]) => {
showTrans: boolean showTrans: boolean
curTrans?: string curTrans?: string
} = { } = {
injectMessage: new InjectMessage(), injectMessaging: new InjectMessaging(),
fold: true, fold: true,
videoElementHeight: TOTAL_HEIGHT_DEF, videoElementHeight: TOTAL_HEIGHT_DEF,
showTrans: false, showTrans: false,
@@ -98,7 +98,7 @@ const debug = (...args: any[]) => {
danmukuBox?.insertBefore(iframe, danmukuBox?.firstChild) danmukuBox?.insertBefore(iframe, danmukuBox?.firstChild)
// show badge // show badge
runtime.injectMessage.sendExtension(MESSAGE_TO_EXTENSION_SHOW_FLAG, { runtime.injectMessaging.sendExtension(MESSAGE_TO_EXTENSION_SHOW_FLAG, {
show: true show: true
}) })
@@ -188,7 +188,7 @@ const debug = (...args: any[]) => {
debug('refreshVideoInfo: ', aid, cid, pages, subtitles) debug('refreshVideoInfo: ', aid, cid, pages, subtitles)
//send setVideoInfo //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, url: location.origin + location.pathname,
title, title,
aid, aid,
@@ -225,7 +225,7 @@ const debug = (...args: any[]) => {
.then(res => res.json()) .then(res => res.json())
.then(res => { .then(res => {
// console.log('refreshSubtitles: ', aid, cid, 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 infos: res.data.subtitle.subtitles
}) })
}) })
@@ -247,7 +247,7 @@ const debug = (...args: any[]) => {
const iframe = document.getElementById(IFRAME_ID) as HTMLIFrameElement | undefined const iframe = document.getElementById(IFRAME_ID) as HTMLIFrameElement | undefined
if (iframe != null) { if (iframe != null) {
iframe.style.display = iframe.style.display === 'none' ? 'block' : 'none' 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' show: iframe.style.display !== 'none'
}) })
} else { } else {
@@ -356,7 +356,7 @@ const debug = (...args: any[]) => {
} }
// 初始化injectMessage // 初始化injectMessage
runtime.injectMessage.init(methods) runtime.injectMessaging.init(methods)
setInterval(() => { setInterval(() => {
if (!sidePanel) { if (!sidePanel) {

View File

@@ -17,7 +17,7 @@ type L2MethodHandlers<L2ReqMsg, L2ResMsg> = {
[key: string]: L2MethodHandler<L2ReqMsg, L2ResMsg> [key: string]: L2MethodHandler<L2ReqMsg, L2ResMsg>
} }
class ExtensionMessage { class ExtensionMessaging {
portIdToPort: Map<string, PortContext<L2ReqMsg, L2ResMsg>> = new Map() portIdToPort: Map<string, PortContext<L2ReqMsg, L2ResMsg>> = new Map()
methods?: L2MethodHandlers<L2ReqMsg, L2ResMsg> methods?: L2MethodHandlers<L2ReqMsg, L2ResMsg>
@@ -130,4 +130,4 @@ class ExtensionMessage {
} }
} }
export default ExtensionMessage export default ExtensionMessaging

View File

@@ -1,7 +1,7 @@
import Layer1Protocol from '../layer1/Layer1Protocol' import Layer1Protocol from '../layer1/Layer1Protocol'
import { L2ReqMsg, L2ResMsg, MESSAGE_TO_EXTENSION_HANDSHAKE, MESSAGE_TO_EXTENSION_ROUTE, TAG_TARGET_APP, TAG_TARGET_INJECT } from '../const' 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 port?: chrome.runtime.Port
portMessageHandler?: Layer1Protocol<L2ReqMsg, L2ResMsg> portMessageHandler?: Layer1Protocol<L2ReqMsg, L2ResMsg>
//类实例 //类实例
@@ -103,4 +103,4 @@ class InjectMessage {
} }
export default InjectMessage export default InjectMessaging

View File

@@ -1,9 +1,9 @@
import { msgWaiter } from './useMessageService' import { msgWaiter } from './useMessagingService'
import { useCallback } from 'react' import { useCallback } from 'react'
import Layer1Protocol from '../layer1/Layer1Protocol' import Layer1Protocol from '../layer1/Layer1Protocol'
import { L2ReqMsg, L2ResMsg, MESSAGE_TO_EXTENSION_ROUTE, TAG_TARGET_INJECT } from '../const' import { L2ReqMsg, L2ResMsg, MESSAGE_TO_EXTENSION_ROUTE, TAG_TARGET_INJECT } from '../const'
const useMessage = () => { const useMessaging = () => {
const sendExtension = useCallback(async <T = any>(method: string, params?: any) => { const sendExtension = useCallback(async <T = any>(method: string, params?: any) => {
// wait // wait
const pmh = await msgWaiter.wait() as Layer1Protocol<L2ReqMsg, L2ResMsg> const pmh = await msgWaiter.wait() as Layer1Protocol<L2ReqMsg, L2ResMsg>
@@ -34,4 +34,4 @@ const useMessage = () => {
} }
} }
export default useMessage export default useMessaging

View File

@@ -16,7 +16,7 @@ export const msgWaiter = new Waiter<Layer1Protocol<L2ReqMsg, L2ResMsg>>(() => ({
data: portMessageHandler!, data: portMessageHandler!,
}), 100, 15000) }), 100, 15000)
const useMessageService = (methods?: { const useMessagingService = (methods?: {
[key: string]: (params: any, context: MethodContext) => Promise<any> [key: string]: (params: any, context: MethodContext) => Promise<any>
}) => { }) => {
const messageHandler = useCallback(async (req: L2ReqMsg): Promise<L2ResMsg> => { const messageHandler = useCallback(async (req: L2ReqMsg): Promise<L2ResMsg> => {
@@ -90,4 +90,4 @@ const useMessageService = (methods?: {
}, [messageHandler, port]) }, [messageHandler, port])
} }
export default useMessageService export default useMessagingService

View File

@@ -8,7 +8,7 @@ import {EventBusContext} from '../Router'
import useTranslateService from '../hooks/useTranslateService' import useTranslateService from '../hooks/useTranslateService'
import {setTheme} from '../utils/biz_util' import {setTheme} from '../utils/biz_util'
import useSearchService from '../hooks/useSearchService' import useSearchService from '../hooks/useSearchService'
import useMessage from '../messaging/layer2/useMessage' import useMessage from '../messaging/layer2/useMessaging'
import {setFold} from '../redux/envReducer' import {setFold} from '../redux/envReducer'
function App() { function App() {

View File

@@ -29,7 +29,7 @@ import classNames from 'classnames'
import toast from 'react-hot-toast' import toast from 'react-hot-toast'
import {useBoolean, useEventTarget} from 'ahooks' import {useBoolean, useEventTarget} from 'ahooks'
import {useEventChecked} from '@kky002/kky-hooks' import {useEventChecked} from '@kky002/kky-hooks'
import useMessage from '@/messaging/layer2/useMessage' import useMessaging from '@/messaging/layer2/useMessaging'
const Section = (props: { const Section = (props: {
title: ShowElement title: ShowElement
@@ -61,7 +61,7 @@ const FormItem = (props: {
const OptionsPage = () => { const OptionsPage = () => {
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
const envData = useAppSelector(state => state.env.envData) const envData = useAppSelector(state => state.env.envData)
const {sendExtension} = useMessage() const {sendExtension} = useMessaging()
const {value: sidePanelValue, onChange: setSidePanelValue} = useEventChecked(envData.sidePanel) const {value: sidePanelValue, onChange: setSidePanelValue} = useEventChecked(envData.sidePanel)
const {value: autoInsertValue, onChange: setAutoInsertValue} = useEventChecked(!envData.manualInsert) const {value: autoInsertValue, onChange: setAutoInsertValue} = useEventChecked(!envData.manualInsert)
const {value: autoExpandValue, onChange: setAutoExpandValue} = useEventChecked(envData.autoExpand) const {value: autoExpandValue, onChange: setAutoExpandValue} = useEventChecked(envData.autoExpand)