fix消息通信

This commit is contained in:
IndieKKY
2025-02-24 09:13:25 +08:00
parent 5bcedf0c9d
commit ae60303a15
25 changed files with 140 additions and 135 deletions

View File

@@ -4,3 +4,4 @@ node_modules/
postcss.config.cjs postcss.config.cjs
vite.config.ts vite.config.ts
vite-env.d.ts vite-env.d.ts
manifest.config.ts

View File

@@ -25,9 +25,12 @@ module.exports = {
"@typescript-eslint/no-empty-interface": "warn", "@typescript-eslint/no-empty-interface": "warn",
"@typescript-eslint/no-unused-vars": "warn", "@typescript-eslint/no-unused-vars": "warn",
"@typescript-eslint/strict-boolean-expressions": "warn", "@typescript-eslint/strict-boolean-expressions": "warn",
"@typescript-eslint/prefer-nullish-coalescing": "off",
"@typescript-eslint/no-non-null-assertion": "warn",
"@typescript-eslint/object-curly-spacing": "off", "@typescript-eslint/object-curly-spacing": "off",
"@typescript-eslint/no-misused-promises": "off", "@typescript-eslint/no-misused-promises": "off",
"@typescript-eslint/space-infix-ops": "off", "@typescript-eslint/space-infix-ops": "off",
"@typescript-eslint/no-floating-promises": "off",
"operator-linebreak": "off", "operator-linebreak": "off",
"@typescript-eslint/space-before-function-paren": "off", "@typescript-eslint/space-before-function-paren": "off",
"@typescript-eslint/comma-dangle": "off" "@typescript-eslint/comma-dangle": "off"

View File

@@ -15,7 +15,7 @@
"dependencies": { "dependencies": {
"@crxjs/vite-plugin": "^1.0.14", "@crxjs/vite-plugin": "^1.0.14",
"@kky002/kky-hooks": "^1.2.1", "@kky002/kky-hooks": "^1.2.1",
"@kky002/kky-message": "^0.0.7", "@kky002/kky-message": "^0.1.8",
"@kky002/kky-ui": "^1.0.9", "@kky002/kky-ui": "^1.0.9",
"@kky002/kky-util": "^1.13.13", "@kky002/kky-util": "^1.13.13",
"@logto/react": "1.0.0-beta.13", "@logto/react": "1.0.0-beta.13",

10
pnpm-lock.yaml generated
View File

@@ -15,8 +15,8 @@ importers:
specifier: ^1.2.1 specifier: ^1.2.1
version: 1.2.1 version: 1.2.1
'@kky002/kky-message': '@kky002/kky-message':
specifier: ^0.0.7 specifier: ^0.1.8
version: 0.0.7 version: 0.1.8
'@kky002/kky-ui': '@kky002/kky-ui':
specifier: ^1.0.9 specifier: ^1.0.9
version: 1.0.9 version: 1.0.9
@@ -397,8 +397,8 @@ packages:
'@kky002/kky-hooks@1.2.1': '@kky002/kky-hooks@1.2.1':
resolution: {integrity: sha512-AJM2gm75TtkZqOPd/WX0+4h/dfjSb8HlDQLwAJsYIHRNw8MQUKu32Ih6OxhKL2l9pdPsuj+S9/lBc4Cwu4tP+Q==} resolution: {integrity: sha512-AJM2gm75TtkZqOPd/WX0+4h/dfjSb8HlDQLwAJsYIHRNw8MQUKu32Ih6OxhKL2l9pdPsuj+S9/lBc4Cwu4tP+Q==}
'@kky002/kky-message@0.0.7': '@kky002/kky-message@0.1.8':
resolution: {integrity: sha512-0qyamYhR+TT79TMbTL4frKTdOZZIObdvbDbmIT1Wu2GafxsIg249FxnW65ERqPNCZ+w5eNCK121EB11+txNFHQ==} resolution: {integrity: sha512-NgGrM7pxxlENPAOTnN+2POAJ7YTRLppC5s++Jkox7sh7f1P+imz+HpeFXjqQmgW1NivHJTHt3ZUVXvRMJ3mELw==}
'@kky002/kky-ui@1.0.9': '@kky002/kky-ui@1.0.9':
resolution: {integrity: sha512-pepfRcLfC1eIQ1lsSJLWNr4PgdLqFLuvQMlitJy7W668yZ7qu8yAHSjg8A20R7HB4mFkJ+B96WETalOar1e/kA==} resolution: {integrity: sha512-pepfRcLfC1eIQ1lsSJLWNr4PgdLqFLuvQMlitJy7W668yZ7qu8yAHSjg8A20R7HB4mFkJ+B96WETalOar1e/kA==}
@@ -2846,7 +2846,7 @@ snapshots:
lodash-es: 4.17.21 lodash-es: 4.17.21
react: 18.2.0 react: 18.2.0
'@kky002/kky-message@0.0.7': '@kky002/kky-message@0.1.8':
dependencies: dependencies:
'@kky002/kky-util': 1.13.13 '@kky002/kky-util': 1.13.13
ahooks: 3.8.4(react@18.2.0) ahooks: 3.8.4(react@18.2.0)

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/useMessageService' import useMessageService from './hooks/useMessageService'
import MainPage from './pages/MainPage' import MainPage from './pages/MainPage'
function App() { function App() {
@@ -44,7 +44,7 @@ function App() {
useLocalStorage<TempData>('chrome_client', STORAGE_TEMP, savedTempData, onLoadTemp) useLocalStorage<TempData>('chrome_client', STORAGE_TEMP, savedTempData, onLoadTemp)
// services // services
useMessagingService() useMessageService()
return <div> return <div>
<Toaster position={path === 'app'?'bottom-center':'top-center'}/> <Toaster position={path === 'app'?'bottom-center':'top-center'}/>

View File

@@ -34,6 +34,9 @@ const methods: {
CLOSE_SIDE_PANEL: async (params, context) => { CLOSE_SIDE_PANEL: async (params, context) => {
closeSidePanel() closeSidePanel()
}, },
GET_TAB_ID: async (params, context) => {
return context.tabId
},
ADD_TASK: async (params, context) => { ADD_TASK: async (params, context) => {
// 新建任务 // 新建任务
const task: Task = { const task: Task = {
@@ -89,14 +92,12 @@ chrome.runtime.onMessage.addListener((event: any, sender: chrome.runtime.Message
return true return true
} else if (event.type === 'syncSet') { // sync.set } else if (event.type === 'syncSet') { // sync.set
chrome.storage.sync.set(event.items).catch(console.error) chrome.storage.sync.set(event.items).catch(console.error)
return
} else if (event.type === 'syncRemove') { // sync.remove } else if (event.type === 'syncRemove') { // sync.remove
chrome.storage.sync.remove(event.keys).catch(console.error) chrome.storage.sync.remove(event.keys).catch(console.error)
return
} }
}) })
//点击扩展图标 // 点击扩展图标
chrome.action.onClicked.addListener(async (tab) => { chrome.action.onClicked.addListener(async (tab) => {
chrome.storage.sync.get(STORAGE_ENV, (envDatas) => { chrome.storage.sync.get(STORAGE_ENV, (envDatas) => {
const envDataStr = envDatas[STORAGE_ENV] const envDataStr = envDatas[STORAGE_ENV]
@@ -115,7 +116,7 @@ chrome.action.onClicked.addListener(async (tab) => {
}) })
} else { } else {
closeSidePanel() closeSidePanel()
extensionMessaging.sendMessage(null, tab.id!, TAG_TARGET_INJECT, 'TOGGLE_DISPLAY').catch(console.error) extensionMessaging.sendMessage(false, tab.id!, TAG_TARGET_INJECT, 'TOGGLE_DISPLAY').catch(console.error)
} }
}) })
}) })

View File

@@ -1,14 +1,14 @@
import React from 'react'; import React from 'react'
import { useAppSelector } from '../hooks/redux'; import { useAppSelector } from '../hooks/redux'
import { openOptionsPage } from '../utils/chromeUtils'; import { openOptionsPage } from '../utils/chromeUtils'
const ApiKeyReminder: React.FC = () => { const ApiKeyReminder: React.FC = () => {
const apiKey = useAppSelector(state => state.env.envData.apiKey); const apiKey = useAppSelector(state => state.env.envData.apiKey)
const geminiApiKey = useAppSelector(state => state.env.envData.geminiApiKey); const geminiApiKey = useAppSelector(state => state.env.envData.geminiApiKey)
const aiType = useAppSelector(state => state.env.envData.aiType); const aiType = useAppSelector(state => state.env.envData.aiType)
if ((aiType === 'gemini' && geminiApiKey) || (aiType !== 'gemini' && apiKey)) { if ((aiType === 'gemini' && geminiApiKey) || (aiType !== 'gemini' && apiKey)) {
return null; return null
} }
return ( return (
@@ -21,7 +21,7 @@ const ApiKeyReminder: React.FC = () => {
</button> </button>
</div> </div>
); )
}; }
export default ApiKeyReminder; export default ApiKeyReminder

View File

@@ -127,7 +127,7 @@ const Body = () => {
} }
toast.success(`已添加${segments_.length}个总结任务!`) toast.success(`已添加${segments_.length}个总结任务!`)
} }
}, [addSummarizeTask, curSummaryType, dispatch, envData.aiType, envData.apiKey, envData.geminiApiKey, segments]) }, [addSummarizeTask, curSummaryType, envData.aiType, envData.apiKey, envData.geminiApiKey, segments])
const onFoldAll = useCallback(() => { const onFoldAll = useCallback(() => {
dispatch(setFoldAll(!foldAll)) dispatch(setFoldAll(!foldAll))

View File

@@ -1,8 +1,8 @@
import { useAppSelector } from '@/hooks/redux'; import { useAppSelector } from '@/hooks/redux'
import React from 'react'; import React from 'react'
const DebateChat: React.FC<DebateProps> = ({ messages }) => { const DebateChat: React.FC<DebateProps> = ({ messages }) => {
const fontSize = useAppSelector(state => state.env.envData.fontSize) const fontSize = useAppSelector(state => state.env.envData.fontSize)
return ( return (
<div className="flex flex-col h-full"> <div className="flex flex-col h-full">
@@ -37,7 +37,7 @@ const DebateChat: React.FC<DebateProps> = ({ messages }) => {
))} ))}
</div> </div>
</div> </div>
); )
}; }
export default DebateChat; export default DebateChat

View File

@@ -11,12 +11,12 @@ import {Placement} from '@popperjs/core/lib/enums'
import {useAppDispatch, useAppSelector} from '../hooks/redux' import {useAppDispatch, useAppSelector} from '../hooks/redux'
import {setEnvData, setTempData} from '../redux/envReducer' import {setEnvData, setTempData} from '../redux/envReducer'
import {EventBusContext} from '../Router' import {EventBusContext} from '../Router'
import {DEFAULT_USE_PORT, EVENT_EXPAND} from '../consts/const' import {EVENT_EXPAND} from '../consts/const'
import {formatSrtTime, formatTime, formatVttTime} from '../utils/util' 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/bizUtil' import {getSummarize} from '../utils/bizUtil'
import dayjs from 'dayjs'; import dayjs from 'dayjs'
import { useMessage } from '@/hooks/useMessageService' import { useMessage } from '@/hooks/useMessageService'
interface Props { interface Props {
@@ -68,7 +68,7 @@ const MoreBtn = (props: Props) => {
const segments = useAppSelector(state => state.env.segments) const segments = useAppSelector(state => state.env.segments)
const url = useAppSelector(state => state.env.url) const url = useAppSelector(state => state.env.url)
const title = useAppSelector(state => state.env.title) const title = useAppSelector(state => state.env.title)
const ctime = useAppSelector(state => state.env.ctime) //时间戳单位s const ctime = useAppSelector(state => state.env.ctime) // 时间戳单位s
const author = useAppSelector(state => state.env.author) const author = useAppSelector(state => state.env.author)
const curSummaryType = useAppSelector(state => state.env.tempData.curSummaryType) const curSummaryType = useAppSelector(state => state.env.tempData.curSummaryType)
@@ -81,7 +81,7 @@ const MoreBtn = (props: Props) => {
let fileName = title let fileName = title
let s, suffix let s, suffix
let time = ctime ? dayjs(ctime * 1000).format('YYYY-MM-DD HH:mm:ss') : '' // 2024-05-01 12:00:00 const time = ctime ? dayjs(ctime * 1000).format('YYYY-MM-DD HH:mm:ss') : '' // 2024-05-01 12:00:00
if (!downloadType || downloadType === 'text') { if (!downloadType || downloadType === 'text') {
s = `${title??'无标题'}\n${url??'无链接'}\n${author??'无作者'} ${time}\n\n` s = `${title??'无标题'}\n${url??'无链接'}\n${author??'无作者'} ${time}\n\n`
for (const item of data.body) { for (const item of data.body) {
@@ -162,11 +162,11 @@ const MoreBtn = (props: Props) => {
}).catch(console.error) }).catch(console.error)
} }
setMoreVisible(false) setMoreVisible(false)
}, [curSummaryType, data, downloadType, segments, title, url]) }, [author, ctime, curSummaryType, data, downloadType, segments, title, url])
const downloadAudioCallback = useCallback(() => { const downloadAudioCallback = useCallback(() => {
sendInject(null, 'DOWNLOAD_AUDIO', {}) sendInject(null, 'DOWNLOAD_AUDIO', {})
}, []) }, [sendInject])
const selectCallback = useCallback((e: any) => { const selectCallback = useCallback((e: any) => {
dispatch(setTempData({ dispatch(setTempData({

View File

@@ -1,14 +1,14 @@
import React, { useState } from 'react'; import React, { useState } from 'react'
import { FaStar } from 'react-icons/fa'; import { FaStar } from 'react-icons/fa'
import { IoMdClose } from 'react-icons/io'; import { IoMdClose } from 'react-icons/io'
import { setTempData } from '../redux/envReducer'; import { setTempData } from '../redux/envReducer'
import { useAppDispatch, useAppSelector } from '../hooks/redux'; import { useAppDispatch, useAppSelector } from '../hooks/redux'
import { openUrl } from '@kky002/kky-util'; import { openUrl } from '@kky002/kky-util'
import { isEdgeBrowser } from '../utils/util'; import { isEdgeBrowser } from '../utils/util'
const RateExtension: React.FC = () => { const RateExtension: React.FC = () => {
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
const [isHovered, setIsHovered] = useState(false); const [isHovered, setIsHovered] = useState(false)
const reviewed = useAppSelector(state => state.env.tempData.reviewed) const reviewed = useAppSelector(state => state.env.tempData.reviewed)
const handleRateClick = () => { const handleRateClick = () => {
@@ -21,9 +21,9 @@ const RateExtension: React.FC = () => {
} else { } else {
openUrl('https://chromewebstore.google.com/webstore/detail/bciglihaegkdhoogebcdblfhppoilclp/reviews') openUrl('https://chromewebstore.google.com/webstore/detail/bciglihaegkdhoogebcdblfhppoilclp/reviews')
} }
}; }
if (reviewed === true || reviewed === undefined) return null; if (reviewed === true || reviewed === undefined) return null
return ( return (
<div className="relative bg-gradient-to-r from-primary to-secondary text-primary-content p-4 rounded-lg shadow-lg text-sm transition-all duration-300 ease-in-out hover:shadow-xl"> <div className="relative bg-gradient-to-r from-primary to-secondary text-primary-content p-4 rounded-lg shadow-lg text-sm transition-all duration-300 ease-in-out hover:shadow-xl">
@@ -52,7 +52,7 @@ const RateExtension: React.FC = () => {
</span> </span>
</button> </button>
</div> </div>
); )
}; }
export default RateExtension; export default RateExtension

View File

@@ -188,7 +188,7 @@ Provide an example to illustrate the expected output:
] ]
\`\`\` \`\`\`
`, `,
[PROMPT_TYPE_SUMMARIZE_DEBATE]: `You are a helpful assistant skilled at generating debates based on video subtitles. [PROMPT_TYPE_SUMMARIZE_DEBATE]: `You are a helpful assistant skilled at generating debates based on video subtitles.
## Context ## Context

View File

@@ -1,14 +1,14 @@
import { setAuthor, setCtime, setCurFetched, setCurInfo, setData, setInfos, setTitle, setUrl } from '@/redux/envReducer' import { setAuthor, setCtime, setCurFetched, setCurInfo, setData, setInfos, setTitle, setUrl } from '@/redux/envReducer'
import { useAppDispatch } from './redux' import { useAppDispatch, useAppSelector } from './redux'
import { AllAPPMessages, AllExtensionMessages, AllInjectMessages } from '@/message-typings' import { AllAPPMessages, AllExtensionMessages, AllInjectMessages } from '@/message-typings'
import { DEFAULT_USE_PORT } from '@/consts/const'
import { useMessaging, useMessagingService } from '@kky002/kky-message' import { useMessaging, useMessagingService } from '@kky002/kky-message'
import { useMemoizedFn } from 'ahooks' import { useMemoizedFn } from 'ahooks'
const useMessageService = () => { const useMessageService = () => {
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
const envData = useAppSelector((state) => state.env.envData)
//methods
// methods
const methodsFunc: () => { const methodsFunc: () => {
[K in AllAPPMessages['method']]: (params: Extract<AllAPPMessages, { method: K }>['params'], context: MethodContext) => Promise<any> [K in AllAPPMessages['method']]: (params: Extract<AllAPPMessages, { method: K }>['params'], context: MethodContext) => Promise<any>
} = useMemoizedFn(() => ({ } = useMemoizedFn(() => ({
@@ -28,8 +28,8 @@ const useMessageService = () => {
}, },
})) }))
useMessagingService(DEFAULT_USE_PORT, methodsFunc) useMessagingService(!!envData.sidePanel, methodsFunc)
} }
export default useMessageService export default useMessageService
export const useMessage = useMessaging<AllExtensionMessages, AllInjectMessages> export const useMessage = useMessaging<AllExtensionMessages, AllInjectMessages>

View File

@@ -9,18 +9,18 @@ const useSubtitle = () => {
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 {sendInject} = useMessage(!!envData.sidePanel) const {sendInject} = useMessage(!!envData.sidePanel)
const move = useCallback((time: number, togglePause: boolean) => { const move = useCallback((time: number, togglePause: boolean) => {
sendInject(null, 'MOVE', {time, togglePause}) sendInject(null, 'MOVE', {time, togglePause})
//review action // review action
if (reviewed === undefined && !reviewAction) { if (reviewed === undefined && !reviewAction) {
dispatch(setReviewAction(true)) dispatch(setReviewAction(true))
dispatch(setTempData({ dispatch(setTempData({
reviewActions: (reviewActions ?? 0) + 1 reviewActions: (reviewActions ?? 0) + 1
})) }))
} }
}, [dispatch, reviewAction, reviewActions, reviewed]) }, [dispatch, reviewAction, reviewActions, reviewed, sendInject])
const scrollIntoView = useCallback((ref: React.RefObject<HTMLDivElement>) => { const scrollIntoView = useCallback((ref: React.RefObject<HTMLDivElement>) => {
ref.current?.scrollIntoView({behavior: 'smooth', block: 'center'}) ref.current?.scrollIntoView({behavior: 'smooth', block: 'center'})

View File

@@ -45,7 +45,7 @@ const useSubtitleService = () => {
const reviewActions = useAppSelector(state => state.env.tempData.reviewActions) const reviewActions = useAppSelector(state => state.env.tempData.reviewActions)
const {sendInject} = useMessage(!!envData.sidePanel) const {sendInject} = useMessage(!!envData.sidePanel)
//如果reviewActions达到15次则设置reviewed为false // 如果reviewActions达到15次则设置reviewed为false
useEffect(() => { useEffect(() => {
if (reviewed === undefined && reviewActions && reviewActions >= 15) { if (reviewed === undefined && reviewActions && reviewActions >= 15) {
dispatch(setTempData({ dispatch(setTempData({
@@ -74,7 +74,7 @@ const useSubtitleService = () => {
dispatch(setCurInfo(infos[0])) dispatch(setCurInfo(infos[0]))
dispatch(setCurFetched(false)) dispatch(setCurFetched(false))
} }
}, [curInfo, dispatch, envData.autoExpand, envReady, fold, infos]) }, [curInfo, dispatch, envData.autoExpand, envReady, fold, infos, envData.sidePanel])
// 获取 // 获取
useEffect(() => { useEffect(() => {
if (curInfo && !curFetched) { if (curInfo && !curFetched) {
@@ -86,15 +86,17 @@ const useSubtitleService = () => {
dispatch(setCurFetched(true)) dispatch(setCurFetched(true))
dispatch(setData(data)) dispatch(setData(data))
console.log('subtitle', data) console.debug('subtitle', data)
}) })
} }
}, [curFetched, curInfo]) }, [curFetched, curInfo, dispatch, sendInject])
useAsyncEffect(async () => { useAsyncEffect(async () => {
// 初始获取列表 // 初始获取列表
sendInject(null, 'REFRESH_VIDEO_INFO', {force: true}) if (envReady) {
}, []) sendInject(null, 'REFRESH_VIDEO_INFO', {force: true})
}
}, [envReady, sendInject])
useAsyncEffect(async () => { useAsyncEffect(async () => {
// 更新设置信息 // 更新设置信息
@@ -103,11 +105,11 @@ const useSubtitleService = () => {
if (envData.sidePanel) { if (envData.sidePanel) {
// get screen height // get screen height
dispatch(setTotalHeight(window.innerHeight)) dispatch(setTotalHeight(window.innerHeight))
}else { } else {
dispatch(setTotalHeight(Math.min(Math.max(info.totalHeight, TOTAL_HEIGHT_MIN), TOTAL_HEIGHT_MAX))) dispatch(setTotalHeight(Math.min(Math.max(info.totalHeight, TOTAL_HEIGHT_MIN), TOTAL_HEIGHT_MAX)))
} }
}) })
}, [envData.sidePanel, infos]) }, [envData.sidePanel, infos, sendInject])
// 更新当前位置 // 更新当前位置
useEffect(() => { useEffect(() => {
@@ -212,7 +214,7 @@ const useSubtitleService = () => {
} else { } else {
sendInject(null, 'HIDE_TRANS', {}) sendInject(null, 'HIDE_TRANS', {})
} }
}, [autoTranslate, curIdx, hideOnDisableAutoTranslate, transResults]) }, [autoTranslate, curIdx, hideOnDisableAutoTranslate, sendInject, transResults])
} }
export default useSubtitleService export default useSubtitleService

View File

@@ -139,10 +139,10 @@ const useTranslate = () => {
dispatch(addTaskId(task.id)) dispatch(addTaskId(task.id))
} }
} }
}, [data?.body, envData, language.name, title, dispatch]) }, [data?.body, envData, language.name, title, dispatch, sendExtension])
const addSummarizeTask = useCallback(async (type: SummaryType, segment: Segment) => { const addSummarizeTask = useCallback(async (type: SummaryType, segment: Segment) => {
//review action // review action
if (reviewed === undefined && !reviewAction) { if (reviewed === undefined && !reviewAction) {
dispatch(setReviewAction(true)) dispatch(setReviewAction(true))
dispatch(setTempData({ dispatch(setTempData({
@@ -208,7 +208,7 @@ const useTranslate = () => {
const task = await sendExtension(null, 'ADD_TASK', {taskDef}) const task = await sendExtension(null, 'ADD_TASK', {taskDef})
dispatch(addTaskId(task.id)) dispatch(addTaskId(task.id))
} }
}, [dispatch, envData, summarizeLanguage.name, title]) }, [dispatch, envData, reviewAction, reviewActions, reviewed, sendExtension, summarizeLanguage.name, title])
const addAskTask = useCallback(async (id: string, segment: Segment, question: string) => { const addAskTask = useCallback(async (id: string, segment: Segment, question: string) => {
if (segment.text.length >= SUMMARIZE_THRESHOLD) { if (segment.text.length >= SUMMARIZE_THRESHOLD) {
@@ -265,7 +265,7 @@ const useTranslate = () => {
const task = await sendExtension(null, 'ADD_TASK', {taskDef}) const task = await sendExtension(null, 'ADD_TASK', {taskDef})
dispatch(addTaskId(task.id)) dispatch(addTaskId(task.id))
} }
}, [dispatch, envData, summarizeLanguage.name, title]) }, [dispatch, envData, sendExtension, summarizeLanguage.name, title])
const handleTranslate = useMemoizedFn((task: Task, content: string) => { const handleTranslate = useMemoizedFn((task: Task, content: string) => {
let map: {[key: string]: string} = {} let map: {[key: string]: string} = {}
@@ -355,7 +355,7 @@ const useTranslate = () => {
} else { } else {
dispatch(delTaskId(taskId)) dispatch(delTaskId(taskId))
} }
}, [dispatch, envData.aiType, handleAsk, handleSummarize, handleTranslate]) }, [dispatch, envData.aiType, handleAsk, handleSummarize, handleTranslate, sendExtension])
return {getFetch, getTask, addTask, addSummarizeTask, addAskTask} return {getFetch, getTask, addTask, addSummarizeTask, addAskTask}
} }

View File

@@ -13,7 +13,7 @@ const debug = (...args: any[]) => {
return return
} }
//读取envData // 读取envData
const envDataStr = (await chrome.storage.sync.get(STORAGE_ENV))[STORAGE_ENV] const envDataStr = (await chrome.storage.sync.get(STORAGE_ENV))[STORAGE_ENV]
let sidePanel: boolean | null = null let sidePanel: boolean | null = null
let manualInsert: boolean | null = null let manualInsert: boolean | null = null
@@ -94,7 +94,7 @@ const debug = (...args: any[]) => {
iframe.dataset[vKey] = danmukuBox?.dataset[vKey] iframe.dataset[vKey] = danmukuBox?.dataset[vKey]
} }
//insert before first child // insert before first child
danmukuBox?.insertBefore(iframe, danmukuBox?.firstChild) danmukuBox?.insertBefore(iframe, danmukuBox?.firstChild)
// show badge // show badge
@@ -114,7 +114,7 @@ const debug = (...args: any[]) => {
if (danmukuBox) { if (danmukuBox) {
clearInterval(timerIframe) clearInterval(timerIframe)
//延迟插入iframe插入太快网络较差时容易出现b站网页刷新原因暂时未知可能b站的某种机制 // 延迟插入iframe插入太快网络较差时容易出现b站网页刷新原因暂时未知可能b站的某种机制
setTimeout(createIframe, 1500) setTimeout(createIframe, 1500)
} }
}, 1000) }, 1000)
@@ -140,6 +140,7 @@ const debug = (...args: any[]) => {
// fix: https://github.com/IndieKKY/bilibili-subtitle/issues/5 // fix: https://github.com/IndieKKY/bilibili-subtitle/issues/5
// 处理稍后再看的url( https://www.bilibili.com/list/watchlater?bvid=xxx&oid=xxx ) // 处理稍后再看的url( https://www.bilibili.com/list/watchlater?bvid=xxx&oid=xxx )
const pathSearchs: Record<string, string> = {} const pathSearchs: Record<string, string> = {}
// eslint-disable-next-line no-return-assign
location.search.slice(1).replace(/([^=&]*)=([^=&]*)/g, (matchs, a, b, c) => pathSearchs[a] = b) location.search.slice(1).replace(/([^=&]*)=([^=&]*)/g, (matchs, a, b, c) => pathSearchs[a] = b)
// bvid // bvid
@@ -158,21 +159,21 @@ const debug = (...args: any[]) => {
lastAidOrBvid = aidOrBvid lastAidOrBvid = aidOrBvid
if (aidOrBvid) { if (aidOrBvid) {
//aid,pages // aid,pages
let cid let cid: string | undefined
let subtitles let subtitles
if (aidOrBvid.toLowerCase().startsWith('av')) {//avxxx if (aidOrBvid.toLowerCase().startsWith('av')) { // avxxx
aid = parseInt(aidOrBvid.slice(2)) aid = parseInt(aidOrBvid.slice(2))
pages = await fetch(`https://api.bilibili.com/x/player/pagelist?aid=${aid}`, { credentials: 'include' }).then(res => res.json()).then(res => res.data) pages = await fetch(`https://api.bilibili.com/x/player/pagelist?aid=${aid}`, { credentials: 'include' }).then(async res => await res.json()).then(res => res.data)
cid = pages[0].cid cid = pages[0].cid
ctime = pages[0].ctime ctime = pages[0].ctime
author = pages[0].owner?.name author = pages[0].owner?.name
title = pages[0].part title = pages[0].part
await fetch(`https://api.bilibili.com/x/player/wbi/v2?aid=${aid}&cid=${cid}`, { credentials: 'include' }).then(res => res.json()).then(res => { await fetch(`https://api.bilibili.com/x/player/wbi/v2?aid=${aid}&cid=${cid!}`, { credentials: 'include' }).then(async res => await res.json()).then(res => {
subtitles = res.data.subtitle.subtitles subtitles = res.data.subtitle.subtitles
}) })
} else {//bvxxx } else { // bvxxx
await fetch(`https://api.bilibili.com/x/web-interface/view?bvid=${aidOrBvid}`, { credentials: 'include' }).then(res => res.json()).then(async res => { await fetch(`https://api.bilibili.com/x/web-interface/view?bvid=${aidOrBvid}`, { credentials: 'include' }).then(async res => await res.json()).then(async res => {
title = res.data.title title = res.data.title
aid = res.data.aid aid = res.data.aid
cid = res.data.cid cid = res.data.cid
@@ -180,12 +181,12 @@ const debug = (...args: any[]) => {
author = res.data.owner?.name author = res.data.owner?.name
pages = res.data.pages pages = res.data.pages
}) })
await fetch(`https://api.bilibili.com/x/player/wbi/v2?aid=${aid}&cid=${cid}`, { credentials: 'include' }).then(res => res.json()).then(res => { await fetch(`https://api.bilibili.com/x/player/wbi/v2?aid=${aid!}&cid=${cid!}`, { credentials: 'include' }).then(async res => await res.json()).then(res => {
subtitles = res.data.subtitle.subtitles subtitles = res.data.subtitle.subtitles
}) })
} }
//pagesMap // pagesMap
pagesMap = {} pagesMap = {}
pages.forEach(page => { pages.forEach(page => {
pagesMap[page.page + ''] = page pagesMap[page.page + ''] = page
@@ -193,7 +194,7 @@ const debug = (...args: any[]) => {
debug('refreshVideoInfo: ', aid, cid, pages, subtitles) debug('refreshVideoInfo: ', aid, cid, pages, subtitles)
//send setVideoInfo // send setVideoInfo
runtime.injectMessaging.sendApp(!!sidePanel, 'SET_VIDEO_INFO', { runtime.injectMessaging.sendApp(!!sidePanel, 'SET_VIDEO_INFO', {
url: location.origin + location.pathname, url: location.origin + location.pathname,
title, title,
@@ -219,7 +220,7 @@ const debug = (...args: any[]) => {
const p = urlSearchParams.get('p') || 1 const p = urlSearchParams.get('p') || 1
const page = pagesMap[p] const page = pagesMap[p]
if (!page) return if (!page) return
const cid = page.cid const cid: number | null = page.cid
if (aid !== lastAid || cid !== lastCid) { if (aid !== lastAid || cid !== lastCid) {
debug('refreshSubtitles', aid, cid) debug('refreshSubtitles', aid, cid)
@@ -230,9 +231,9 @@ const debug = (...args: any[]) => {
fetch(`https://api.bilibili.com/x/player/wbi/v2?aid=${aid}&cid=${cid}`, { fetch(`https://api.bilibili.com/x/player/wbi/v2?aid=${aid}&cid=${cid}`, {
credentials: 'include', credentials: 'include',
}) })
.then(res => res.json()) .then(async res => await res.json())
.then(res => { .then(res => {
//remove elements with empty subtitle_url // remove elements with empty subtitle_url
res.data.subtitle.subtitles = res.data.subtitle.subtitles.filter((item: any) => item.subtitle_url) res.data.subtitle.subtitles = res.data.subtitle.subtitles.filter((item: any) => item.subtitle_url)
if (res.data.subtitle.subtitles.length > 0) { if (res.data.subtitle.subtitles.length > 0) {
runtime.injectMessaging.sendApp(!!sidePanel, 'SET_INFOS', { runtime.injectMessaging.sendApp(!!sidePanel, 'SET_INFOS', {
@@ -283,7 +284,7 @@ const debug = (...args: any[]) => {
if (url.startsWith('http://')) { if (url.startsWith('http://')) {
url = url.replace('http://', 'https://') url = url.replace('http://', 'https://')
} }
return await fetch(url).then(res => res.json()) return await fetch(url).then(async res => await res.json())
}, },
GET_VIDEO_STATUS: async (params) => { GET_VIDEO_STATUS: async (params) => {
const video = getVideoElement() const video = getVideoElement()
@@ -336,7 +337,7 @@ const debug = (...args: any[]) => {
runtime.showTrans = false runtime.showTrans = false
runtime.curTrans = undefined runtime.curTrans = undefined
let text = document.getElementById('trans-result-text') const text = document.getElementById('trans-result-text')
if (text) { if (text) {
text.style.display = 'none' text.style.display = 'none'
} }
@@ -357,7 +358,7 @@ const debug = (...args: any[]) => {
const playInfo = JSON.parse(html.match(/window.__playinfo__=(.+?)<\/script/)?.[1] ?? '{}') const playInfo = JSON.parse(html.match(/window.__playinfo__=(.+?)<\/script/)?.[1] ?? '{}')
const audioUrl = playInfo.data.dash.audio[0].baseUrl const audioUrl = playInfo.data.dash.audio[0].baseUrl
fetch(audioUrl).then(res => res.blob()).then(blob => { fetch(audioUrl).then(async res => await res.blob()).then(blob => {
const a = document.createElement('a') const a = document.createElement('a')
a.href = URL.createObjectURL(blob) a.href = URL.createObjectURL(blob)
a.download = `${title}.m4s` a.download = `${title}.m4s`

View File

@@ -1,12 +1,12 @@
import {ExtensionMessage, InjectMessage, AppMessage} from '@kky002/kky-message' import {ExtensionMessage, InjectMessage, AppMessage} from '@kky002/kky-message'
//extension // extension
interface ExtensionCloseSidePanelMessage extends ExtensionMessage { interface ExtensionCloseSidePanelMessage extends ExtensionMessage {
method: 'CLOSE_SIDE_PANEL'; method: 'CLOSE_SIDE_PANEL'
} }
interface ExtensionAddTaskMessage extends ExtensionMessage<{ taskDef: TaskDef }, Task> { interface ExtensionAddTaskMessage extends ExtensionMessage<{ taskDef: TaskDef }, Task> {
method: 'ADD_TASK'; method: 'ADD_TASK'
} }
interface ExtensionGetTaskMessage extends ExtensionMessage<{ taskId: string }, { interface ExtensionGetTaskMessage extends ExtensionMessage<{ taskId: string }, {
@@ -15,76 +15,73 @@ interface ExtensionGetTaskMessage extends ExtensionMessage<{ taskId: string }, {
} | { } | {
code: 'not_found' code: 'not_found'
}> { }> {
method: 'GET_TASK'; method: 'GET_TASK'
} }
interface ExtensionShowFlagMessage extends ExtensionMessage<{ show: boolean }> { interface ExtensionShowFlagMessage extends ExtensionMessage<{ show: boolean }> {
method: 'SHOW_FLAG'; method: 'SHOW_FLAG'
} }
export type AllExtensionMessages = ExtensionCloseSidePanelMessage | ExtensionAddTaskMessage | ExtensionGetTaskMessage | ExtensionShowFlagMessage interface ExtensionGetTabIdMessage extends ExtensionMessage<{ show: boolean }> {
method: 'GET_TAB_ID'
}
export type AllExtensionMessages = ExtensionGetTabIdMessage | ExtensionCloseSidePanelMessage | ExtensionAddTaskMessage | ExtensionGetTaskMessage | ExtensionShowFlagMessage
// inject
//inject
interface InjectToggleDisplayMessage extends InjectMessage<{}> { interface InjectToggleDisplayMessage extends InjectMessage<{}> {
method: 'TOGGLE_DISPLAY'; method: 'TOGGLE_DISPLAY'
} }
interface InjectFoldMessage extends InjectMessage<{ fold: boolean }> { interface InjectFoldMessage extends InjectMessage<{ fold: boolean }> {
method: 'FOLD'; method: 'FOLD'
} }
interface InjectMoveMessage extends InjectMessage<{ time: number, togglePause: boolean }> { interface InjectMoveMessage extends InjectMessage<{ time: number, togglePause: boolean }> {
method: 'MOVE'; method: 'MOVE'
} }
interface InjectGetSubtitleMessage extends InjectMessage<{ info: any }> { interface InjectGetSubtitleMessage extends InjectMessage<{ info: any }> {
method: 'GET_SUBTITLE'; method: 'GET_SUBTITLE'
} }
interface InjectGetVideoStatusMessage extends InjectMessage<{}> { interface InjectGetVideoStatusMessage extends InjectMessage<{}> {
method: 'GET_VIDEO_STATUS'; method: 'GET_VIDEO_STATUS'
} }
interface InjectGetVideoElementInfoMessage extends InjectMessage<{}> { interface InjectGetVideoElementInfoMessage extends InjectMessage<{}> {
method: 'GET_VIDEO_ELEMENT_INFO'; method: 'GET_VIDEO_ELEMENT_INFO'
} }
interface InjectRefreshVideoInfoMessage extends InjectMessage<{ force: boolean }> { interface InjectRefreshVideoInfoMessage extends InjectMessage<{ force: boolean }> {
method: 'REFRESH_VIDEO_INFO'; method: 'REFRESH_VIDEO_INFO'
} }
interface InjectUpdateTransResultMessage extends InjectMessage<{ result: string }> { interface InjectUpdateTransResultMessage extends InjectMessage<{ result: string }> {
method: 'UPDATE_TRANS_RESULT'; method: 'UPDATE_TRANS_RESULT'
} }
interface InjectHideTransMessage extends InjectMessage<{}> { interface InjectHideTransMessage extends InjectMessage<{}> {
method: 'HIDE_TRANS'; method: 'HIDE_TRANS'
} }
interface InjectPlayMessage extends InjectMessage<{ play: boolean }> { interface InjectPlayMessage extends InjectMessage<{ play: boolean }> {
method: 'PLAY'; method: 'PLAY'
} }
interface InjectDownloadAudioMessage extends InjectMessage<{}> { interface InjectDownloadAudioMessage extends InjectMessage<{}> {
method: 'DOWNLOAD_AUDIO'; method: 'DOWNLOAD_AUDIO'
} }
export type AllInjectMessages = InjectToggleDisplayMessage | InjectFoldMessage | InjectMoveMessage | InjectGetSubtitleMessage | InjectGetVideoStatusMessage | InjectGetVideoElementInfoMessage | InjectRefreshVideoInfoMessage | InjectUpdateTransResultMessage | InjectHideTransMessage | InjectPlayMessage | InjectDownloadAudioMessage export type AllInjectMessages = InjectToggleDisplayMessage | InjectFoldMessage | InjectMoveMessage | InjectGetSubtitleMessage | InjectGetVideoStatusMessage | InjectGetVideoElementInfoMessage | InjectRefreshVideoInfoMessage | InjectUpdateTransResultMessage | InjectHideTransMessage | InjectPlayMessage | InjectDownloadAudioMessage
// app
//app
interface AppSetInfosMessage extends AppMessage<{ infos: any }> { interface AppSetInfosMessage extends AppMessage<{ infos: any }> {
method: 'SET_INFOS'; method: 'SET_INFOS'
} }
interface AppSetVideoInfoMessage extends AppMessage<{ url: string, title: string, aid: number | null, ctime: number | null, author?: string, pages: any, infos: any }> { interface AppSetVideoInfoMessage extends AppMessage<{ url: string, title: string, aid: number | null, ctime: number | null, author?: string, pages: any, infos: any }> {
method: 'SET_VIDEO_INFO'; method: 'SET_VIDEO_INFO'
} }
export type AllAPPMessages = AppSetInfosMessage | AppSetVideoInfoMessage export type AllAPPMessages = AppSetInfosMessage | AppSetVideoInfoMessage

View File

@@ -18,7 +18,7 @@ function App() {
const eventBus = useContext(EventBusContext) const eventBus = useContext(EventBusContext)
const totalHeight = useAppSelector(state => state.env.totalHeight) const totalHeight = useAppSelector(state => state.env.totalHeight)
const {sendInject} = useMessage(!!envData.sidePanel) const {sendInject} = useMessage(!!envData.sidePanel)
const foldCallback = useCallback(() => { const foldCallback = useCallback(() => {
dispatch(setFold(!fold)) dispatch(setFold(!fold))
sendInject(null, 'FOLD', {fold: !fold}) sendInject(null, 'FOLD', {fold: !fold})

View File

@@ -29,8 +29,8 @@ import {useEventChecked} from '@kky002/kky-hooks'
import { FaChevronDown, FaChevronUp, FaGripfire } from 'react-icons/fa' import { FaChevronDown, FaChevronUp, FaGripfire } from 'react-icons/fa'
import { useMessage } from '@/hooks/useMessageService' import { useMessage } from '@/hooks/useMessageService'
const OptionCard = ({ title, children, defaultExpanded = true }: { title: React.ReactNode; children: React.ReactNode; defaultExpanded?: boolean }) => { const OptionCard = ({ title, children, defaultExpanded = true }: { title: React.ReactNode, children: React.ReactNode, defaultExpanded?: boolean }) => {
const [isExpanded, setIsExpanded] = useState(defaultExpanded); const [isExpanded, setIsExpanded] = useState(defaultExpanded)
return ( return (
<div className="card bg-base-200 shadow-xl mb-4"> <div className="card bg-base-200 shadow-xl mb-4">
@@ -42,8 +42,8 @@ const OptionCard = ({ title, children, defaultExpanded = true }: { title: React.
{isExpanded && <div className="mt-4">{children}</div>} {isExpanded && <div className="mt-4">{children}</div>}
</div> </div>
</div> </div>
); )
}; }
const FormItem = (props: { const FormItem = (props: {
title: ShowElement title: ShowElement
@@ -430,4 +430,4 @@ const OptionsPage = () => {
) )
} }
export default OptionsPage export default OptionsPage

View File

@@ -50,7 +50,7 @@ interface EnvState {
searchText: string searchText: string
searchResult: Record<string, boolean> searchResult: Record<string, boolean>
//当前视频是否计算过操作 // 当前视频是否计算过操作
reviewAction: boolean reviewAction: boolean
} }

8
src/typings.d.ts vendored
View File

@@ -7,7 +7,7 @@ interface MethodContext {
interface EnvData { interface EnvData {
sidePanel?: boolean sidePanel?: boolean
manualInsert?: boolean //是否手动插入字幕列表 manualInsert?: boolean // 是否手动插入字幕列表
autoExpand?: boolean autoExpand?: boolean
flagDot?: boolean flagDot?: boolean
@@ -154,10 +154,10 @@ type SummaryStatus = 'init' | 'pending' | 'done'
type SummaryType = 'overview' | 'keypoint' | 'brief' | 'question' | 'debate' type SummaryType = 'overview' | 'keypoint' | 'brief' | 'question' | 'debate'
interface DebateMessage { interface DebateMessage {
side: 'pro' | 'con'; side: 'pro' | 'con'
content: string; content: string
} }
interface DebateProps { interface DebateProps {
messages: DebateMessage[]; messages: DebateMessage[]
} }

View File

@@ -115,7 +115,7 @@ export const getSummaryStr = (summary: Summary) => {
s += content.map(item => { s += content.map(item => {
return (item.side === 'pro'?'正方:':'反方:') + item.content + '\n' return (item.side === 'pro'?'正方:':'反方:') + item.content + '\n'
}).join('\n') }).join('\n')
} }
return s return s
} }

View File

@@ -1,7 +1,7 @@
export const openOptionsPage = () => { export const openOptionsPage = () => {
if (chrome.runtime.openOptionsPage) { if (chrome.runtime.openOptionsPage) {
chrome.runtime.openOptionsPage(); chrome.runtime.openOptionsPage()
} else { } else {
window.open(chrome.runtime.getURL('options.html')); window.open(chrome.runtime.getURL('options.html'))
} }
}; }

View File

@@ -1,8 +1,8 @@
import {SyntheticEvent} from 'react' import {SyntheticEvent} from 'react'
export const isEdgeBrowser = () => { export const isEdgeBrowser = () => {
const userAgent = navigator.userAgent.toLowerCase(); const userAgent = navigator.userAgent.toLowerCase()
return userAgent.includes('edg/') && !userAgent.includes('edge/'); return userAgent.includes('edg/') && !userAgent.includes('edge/')
} }
export const formatTime = (time: number) => { export const formatTime = (time: number) => {