You've already forked bilibili-subtitle
优化
This commit is contained in:
@@ -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() {
|
||||
|
@@ -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)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -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
|
||||
export default useMessageService
|
@@ -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)
|
||||
|
@@ -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是单例,类似后端的服务概念
|
||||
|
@@ -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()
|
||||
/**
|
||||
* 获取下一个需要翻译的行
|
||||
* 会检测冷却
|
||||
|
@@ -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) {
|
||||
|
@@ -17,7 +17,7 @@ type L2MethodHandlers<L2ReqMsg, L2ResMsg> = {
|
||||
[key: string]: L2MethodHandler<L2ReqMsg, L2ResMsg>
|
||||
}
|
||||
|
||||
class ExtensionMessage {
|
||||
class ExtensionMessaging {
|
||||
portIdToPort: Map<string, PortContext<L2ReqMsg, L2ResMsg>> = new Map()
|
||||
methods?: L2MethodHandlers<L2ReqMsg, L2ResMsg>
|
||||
|
||||
@@ -130,4 +130,4 @@ class ExtensionMessage {
|
||||
}
|
||||
}
|
||||
|
||||
export default ExtensionMessage
|
||||
export default ExtensionMessaging
|
@@ -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<L2ReqMsg, L2ResMsg>
|
||||
//类实例
|
||||
@@ -103,4 +103,4 @@ class InjectMessage {
|
||||
|
||||
}
|
||||
|
||||
export default InjectMessage
|
||||
export default InjectMessaging
|
@@ -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 <T = any>(method: string, params?: any) => {
|
||||
// wait
|
||||
const pmh = await msgWaiter.wait() as Layer1Protocol<L2ReqMsg, L2ResMsg>
|
||||
@@ -34,4 +34,4 @@ const useMessage = () => {
|
||||
}
|
||||
}
|
||||
|
||||
export default useMessage
|
||||
export default useMessaging
|
@@ -16,7 +16,7 @@ export const msgWaiter = new Waiter<Layer1Protocol<L2ReqMsg, L2ResMsg>>(() => ({
|
||||
data: portMessageHandler!,
|
||||
}), 100, 15000)
|
||||
|
||||
const useMessageService = (methods?: {
|
||||
const useMessagingService = (methods?: {
|
||||
[key: string]: (params: any, context: MethodContext) => Promise<any>
|
||||
}) => {
|
||||
const messageHandler = useCallback(async (req: L2ReqMsg): Promise<L2ResMsg> => {
|
||||
@@ -90,4 +90,4 @@ const useMessageService = (methods?: {
|
||||
}, [messageHandler, port])
|
||||
}
|
||||
|
||||
export default useMessageService
|
||||
export default useMessagingService
|
@@ -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() {
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user