You've already forked bilibili-subtitle
Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
d7085ba75e | ||
![]() |
b8d8b8fe06 | ||
![]() |
d42844ac3c | ||
![]() |
e66f85ff9f | ||
![]() |
d97af83c9e | ||
![]() |
c70fa81482 | ||
![]() |
4dbce345c2 | ||
![]() |
aa098df6a5 | ||
![]() |
fa99fa8b3d | ||
![]() |
5078baca1b | ||
![]() |
771a802728 | ||
![]() |
9038bf7128 | ||
![]() |
f01e175b4e | ||
![]() |
8098d7cf9b | ||
![]() |
728e4dce84 | ||
![]() |
a54b4a7e5f | ||
![]() |
8a908ecf99 |
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "哔哩哔哩字幕列表",
|
||||
"description": "显示B站视频的字幕列表,可点击跳转与下载字幕,并支持翻译和总结字幕!",
|
||||
"version": "1.7.7",
|
||||
"version": "1.8.1",
|
||||
"manifest_version": 3,
|
||||
"permissions": [
|
||||
"storage"
|
||||
|
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"private": true,
|
||||
"name": "bilibili-subtitle",
|
||||
"version": "1.7.7",
|
||||
"version": "1.8.1",
|
||||
"type": "module",
|
||||
"description": "哔哩哔哩字幕列表",
|
||||
"main": "index.js",
|
||||
|
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 24 KiB |
@@ -3,12 +3,12 @@ import {
|
||||
setAutoScroll,
|
||||
setAutoTranslate,
|
||||
setCheckAutoScroll,
|
||||
setCompact,
|
||||
setFoldAll,
|
||||
setNeedScroll,
|
||||
setPage,
|
||||
setSearchText,
|
||||
setSegmentFold
|
||||
setSegmentFold,
|
||||
setTempData
|
||||
} from '../redux/envReducer'
|
||||
import {useAppDispatch, useAppSelector} from '../hooks/redux'
|
||||
import {
|
||||
@@ -44,8 +44,7 @@ const Body = () => {
|
||||
const segments = useAppSelector(state => state.env.segments)
|
||||
const foldAll = useAppSelector(state => state.env.foldAll)
|
||||
const envData = useAppSelector(state => state.env.envData)
|
||||
const compact = useAppSelector(state => state.env.compact)
|
||||
const apiKey = useAppSelector(state => state.env.envData.apiKey)
|
||||
const compact = useAppSelector(state => state.env.tempData.compact)
|
||||
const floatKeyPointsSegIdx = useAppSelector(state => state.env.floatKeyPointsSegIdx)
|
||||
const translateEnable = useAppSelector(state => state.env.envData.translateEnable)
|
||||
const summarizeEnable = useAppSelector(state => state.env.envData.summarizeEnable)
|
||||
@@ -60,11 +59,15 @@ const Body = () => {
|
||||
const searchText = useAppSelector(state => state.env.searchText)
|
||||
|
||||
const normalCallback = useCallback(() => {
|
||||
dispatch(setCompact(false))
|
||||
dispatch(setTempData({
|
||||
compact: false
|
||||
}))
|
||||
}, [dispatch])
|
||||
|
||||
const compactCallback = useCallback(() => {
|
||||
dispatch(setCompact(true))
|
||||
dispatch(setTempData({
|
||||
compact: true
|
||||
}))
|
||||
}, [dispatch])
|
||||
|
||||
const posCallback = useCallback(() => {
|
||||
@@ -72,6 +75,7 @@ const Body = () => {
|
||||
}, [dispatch])
|
||||
|
||||
const onSummarizeAll = useCallback(() => {
|
||||
const apiKey = envData.aiType === 'gemini'?envData.geminiApiKey:envData.apiKey
|
||||
if (!apiKey) {
|
||||
dispatch(setPage(PAGE_SETTINGS))
|
||||
toast.error('需要先设置ApiKey!')
|
||||
@@ -94,7 +98,7 @@ const Body = () => {
|
||||
}
|
||||
toast.success(`已添加${segments_.length}个总结任务!`)
|
||||
}
|
||||
}, [addSummarizeTask, apiKey, curSummaryType, dispatch, segments])
|
||||
}, [addSummarizeTask, curSummaryType, dispatch, envData.aiType, envData.apiKey, envData.geminiApiKey, segments])
|
||||
|
||||
const onFoldAll = useCallback(() => {
|
||||
dispatch(setFoldAll(!foldAll))
|
||||
@@ -107,13 +111,14 @@ const Body = () => {
|
||||
}, [dispatch, foldAll, segments])
|
||||
|
||||
const toggleAutoTranslateCallback = useCallback(() => {
|
||||
if (envData.apiKey) {
|
||||
const apiKey = envData.aiType === 'gemini'?envData.geminiApiKey:envData.apiKey
|
||||
if (apiKey) {
|
||||
dispatch(setAutoTranslate(!autoTranslate))
|
||||
} else {
|
||||
dispatch(setPage(PAGE_SETTINGS))
|
||||
toast.error('需要先设置ApiKey!')
|
||||
}
|
||||
}, [autoTranslate, dispatch, envData.apiKey])
|
||||
}, [autoTranslate, dispatch, envData.aiType, envData.apiKey, envData.geminiApiKey])
|
||||
|
||||
const onEnableAutoScroll = useCallback(() => {
|
||||
dispatch(setAutoScroll(true))
|
||||
@@ -217,8 +222,8 @@ const Body = () => {
|
||||
{(segments?.length??0) > 0 && <button className='mt-1.5 btn btn-xs btn-info' onClick={onCopy}>点击复制生成的{SUMMARIZE_TYPES[curSummaryType].name}<RiFileCopy2Line/></button>}
|
||||
</div>
|
||||
<div className='flex flex-col items-center text-center py-2 mx-4 border-t border-t-base-300'>
|
||||
<div className='font-semibold text-accent flex items-center gap-1'><img src='/youtube-subtitle.png' alt='youtube subtitle' className='w-8 h-8'/>Youtube Subtitle</div>
|
||||
<div className='text-sm px-2 desc'>这是<span className='text-amber-600 font-semibold text-base'>Youtube</span>版的字幕列表</div>
|
||||
<div className='font-semibold text-accent flex items-center gap-1'><img src='/youtube-caption.png' alt='youtube caption pro' className='w-8 h-8'/>YouTube Caption Pro</div>
|
||||
<div className='text-sm px-2 desc'>这是<span className='text-amber-600 font-semibold text-base'>YouTube</span>版的字幕列表</div>
|
||||
<div className='flex gap-2'>
|
||||
<a title='Chrome商店' href='https://chromewebstore.google.com/detail/fiaeclpicddpifeflpmlgmbjgaedladf' onClick={(e) => {
|
||||
e.preventDefault()
|
||||
|
@@ -22,7 +22,7 @@ const CompactSegmentItem = (props: {
|
||||
<span className={'pl-1 pr-0.5 py-0.5 cursor-pointer rounded-sm hover:bg-base-200'} onClick={moveCallback}>
|
||||
<text className={classNames('font-medium', isIn ? 'text-primary underline' : '')}>{display.main}</text>
|
||||
{display.sub && <text className='desc'>({display.sub})</text>}</span>
|
||||
<span>{!last && ' '}</span>
|
||||
<span className='text-base-content/75'>{!last && ','}</span>
|
||||
</div>
|
||||
}
|
||||
|
||||
|
@@ -11,7 +11,7 @@ import {
|
||||
import Popover from '../components/Popover'
|
||||
import {Placement} from '@popperjs/core/lib/enums'
|
||||
import {useAppDispatch, useAppSelector} from '../hooks/redux'
|
||||
import {setDownloadType, setEnvData, setPage} from '../redux/envReducer'
|
||||
import {setEnvData, setPage, setTempData} from '../redux/envReducer'
|
||||
import {EventBusContext} from '../Router'
|
||||
import {EVENT_EXPAND, PAGE_SETTINGS} from '../const'
|
||||
import {formatSrtTime, formatTime, formatVttTime} from '../util/util'
|
||||
@@ -62,7 +62,7 @@ const MoreBtn = (props: Props) => {
|
||||
const data = useAppSelector(state => state.env.data)
|
||||
const envReady = useAppSelector(state => state.env.envReady)
|
||||
const envData = useAppSelector(state => state.env.envData)
|
||||
const downloadType = useAppSelector(state => state.env.downloadType)
|
||||
const downloadType = useAppSelector(state => state.env.tempData.downloadType)
|
||||
const [moreVisible, setMoreVisible] = useState(false)
|
||||
const eventBus = useContext(EventBusContext)
|
||||
const segments = useAppSelector(state => state.env.segments)
|
||||
@@ -162,7 +162,9 @@ const MoreBtn = (props: Props) => {
|
||||
}, [])
|
||||
|
||||
const selectCallback = useCallback((e: any) => {
|
||||
dispatch(setDownloadType(e.target.value))
|
||||
dispatch(setTempData({
|
||||
downloadType: e.target.value,
|
||||
}))
|
||||
}, [dispatch])
|
||||
|
||||
const preventCallback = useCallback((e: any) => {
|
||||
@@ -265,8 +267,8 @@ const MoreBtn = (props: Props) => {
|
||||
e.stopPropagation()
|
||||
openUrl('https://chromewebstore.google.com/detail/fiaeclpicddpifeflpmlgmbjgaedladf')
|
||||
}}>
|
||||
<img alt='youtube subtitle' src='/youtube-subtitle.png' className='w-[20px] h-[20px] bg-white rounded-sm p-0.5'/>
|
||||
Youtube Subtitle
|
||||
<img alt='youtube subtitle' src='/youtube-caption.png' className='w-[20px] h-[20px] bg-white rounded-sm p-0.5'/>
|
||||
Youtube Caption Pro
|
||||
</a>
|
||||
</li>
|
||||
<li className='hover:bg-accent'>
|
||||
|
@@ -66,19 +66,20 @@ const Summarize = (props: {
|
||||
const {segment, segmentIdx, summary, float} = props
|
||||
|
||||
const dispatch = useAppDispatch()
|
||||
const apiKey = useAppSelector(state => state.env.envData.apiKey)
|
||||
const envData = useAppSelector(state => state.env.envData)
|
||||
const fontSize = useAppSelector(state => state.env.envData.fontSize)
|
||||
const curSummaryType = useAppSelector(state => state.env.tempData.curSummaryType)
|
||||
const {addSummarizeTask} = useTranslate()
|
||||
|
||||
const onGenerate = useCallback(() => {
|
||||
const apiKey = envData.aiType === 'gemini'?envData.geminiApiKey:envData.apiKey
|
||||
if (apiKey) {
|
||||
addSummarizeTask(curSummaryType, segment).catch(console.error)
|
||||
} else {
|
||||
dispatch(setPage(PAGE_SETTINGS))
|
||||
toast.error('需要先设置ApiKey!')
|
||||
}
|
||||
}, [addSummarizeTask, apiKey, curSummaryType, dispatch, segment])
|
||||
}, [addSummarizeTask, curSummaryType, dispatch, envData.aiType, envData.apiKey, envData.geminiApiKey, segment])
|
||||
|
||||
const onCopy = useCallback(() => {
|
||||
if (summary != null) {
|
||||
@@ -138,7 +139,7 @@ const SegmentCard = (props: {
|
||||
const summarizeFloat = useAppSelector(state => state.env.envData.summarizeFloat)
|
||||
const fold = useAppSelector(state => state.env.fold)
|
||||
const page = useAppSelector(state => state.env.page)
|
||||
const compact = useAppSelector(state => state.env.compact)
|
||||
const compact = useAppSelector(state => state.env.tempData.compact)
|
||||
const floatKeyPointsSegIdx = useAppSelector(state => state.env.floatKeyPointsSegIdx)
|
||||
const showCurrent = useMemo(() => curIdx != null && segment.startIdx <= curIdx && curIdx <= segment.endIdx, [curIdx, segment.endIdx, segment.startIdx])
|
||||
const curSummaryType = useAppSelector(state => state.env.tempData.curSummaryType)
|
||||
|
@@ -18,7 +18,7 @@ const SegmentItem = (props: {
|
||||
const ref = useRef<any>()
|
||||
const {move} = useSubtitle()
|
||||
|
||||
const compact = useAppSelector(state => state.env.compact)
|
||||
const compact = useAppSelector(state => state.env.tempData.compact)
|
||||
const searchText = useAppSelector(state => state.env.searchText)
|
||||
const searchResult = useAppSelector(state => state.env.searchResult)
|
||||
const display = useMemo(() => {
|
||||
|
@@ -63,12 +63,14 @@ const Settings = () => {
|
||||
const {value: summarizeFloatValue, onChange: setSummarizeFloatValue} = useEventChecked(envData.summarizeFloat)
|
||||
const [apiKeyValue, { onChange: onChangeApiKeyValue }] = useEventTarget({initialValue: envData.apiKey??''})
|
||||
const [serverUrlValue, setServerUrlValue] = useState(envData.serverUrl)
|
||||
const [geminiApiKeyValue, { onChange: onChangeGeminiApiKeyValue }] = useEventTarget({initialValue: envData.geminiApiKey??''})
|
||||
const [languageValue, { onChange: onChangeLanguageValue }] = useEventTarget({initialValue: envData.language??LANGUAGE_DEFAULT})
|
||||
const [modelValue, { onChange: onChangeModelValue }] = useEventTarget({initialValue: envData.model??MODEL_DEFAULT})
|
||||
const [summarizeLanguageValue, { onChange: onChangeSummarizeLanguageValue }] = useEventTarget({initialValue: envData.summarizeLanguage??SUMMARIZE_LANGUAGE_DEFAULT})
|
||||
const [hideOnDisableAutoTranslateValue, setHideOnDisableAutoTranslateValue] = useState(envData.hideOnDisableAutoTranslate)
|
||||
const [themeValue, setThemeValue] = useState(envData.theme)
|
||||
const [fontSizeValue, setFontSizeValue] = useState(envData.fontSize)
|
||||
const [aiTypeValue, setAiTypeValue] = useState(envData.aiType)
|
||||
const [transDisplayValue, setTransDisplayValue] = useState(envData.transDisplay)
|
||||
const [wordsValue, setWordsValue] = useState<number | undefined>(envData.words??WORDS_DEFAULT)
|
||||
const [fetchAmountValue, setFetchAmountValue] = useState(envData.fetchAmount??TRANSLATE_FETCH_DEFAULT)
|
||||
@@ -91,6 +93,12 @@ const Settings = () => {
|
||||
}
|
||||
return list
|
||||
}, [])
|
||||
const apiKeySetted = useMemo(() => {
|
||||
if (aiTypeValue === 'gemini') {
|
||||
return !!geminiApiKeyValue
|
||||
}
|
||||
return !!apiKeyValue
|
||||
}, [aiTypeValue, apiKeyValue, geminiApiKeyValue])
|
||||
|
||||
const onChangeHideOnDisableAutoTranslate = useCallback((e: any) => {
|
||||
setHideOnDisableAutoTranslateValue(e.target.checked)
|
||||
@@ -99,9 +107,11 @@ const Settings = () => {
|
||||
const onSave = useCallback(() => {
|
||||
dispatch(setEnvData({
|
||||
autoExpand: autoExpandValue,
|
||||
aiType: aiTypeValue,
|
||||
apiKey: apiKeyValue,
|
||||
serverUrl: serverUrlValue,
|
||||
model: modelValue,
|
||||
geminiApiKey: geminiApiKeyValue,
|
||||
translateEnable: translateEnableValue,
|
||||
language: languageValue,
|
||||
hideOnDisableAutoTranslate: hideOnDisableAutoTranslateValue,
|
||||
@@ -119,7 +129,7 @@ const Settings = () => {
|
||||
}))
|
||||
dispatch(setPage(PAGE_MAIN))
|
||||
toast.success('保存成功')
|
||||
}, [dispatch, autoExpandValue, apiKeyValue, serverUrlValue, modelValue, translateEnableValue, languageValue, hideOnDisableAutoTranslateValue, themeValue, transDisplayValue, summarizeEnableValue, summarizeFloatValue, summarizeLanguageValue, wordsValue, fetchAmountValue, fontSizeValue, promptsValue, searchEnabledValue, cnSearchEnabledValue])
|
||||
}, [dispatch, aiTypeValue, geminiApiKeyValue, autoExpandValue, apiKeyValue, serverUrlValue, modelValue, translateEnableValue, languageValue, hideOnDisableAutoTranslateValue, themeValue, transDisplayValue, summarizeEnableValue, summarizeFloatValue, summarizeLanguageValue, wordsValue, fetchAmountValue, fontSizeValue, promptsValue, searchEnabledValue, cnSearchEnabledValue])
|
||||
|
||||
const onCancel = useCallback(() => {
|
||||
dispatch(setPage(PAGE_MAIN))
|
||||
@@ -165,6 +175,14 @@ const Settings = () => {
|
||||
setFontSizeValue('large')
|
||||
}, [])
|
||||
|
||||
const onSelOpenai = useCallback(() => {
|
||||
setAiTypeValue('openai')
|
||||
}, [])
|
||||
|
||||
const onSelGemini = useCallback(() => {
|
||||
setAiTypeValue('gemini')
|
||||
}, [])
|
||||
|
||||
return <div className='text-sm overflow-y-auto' style={{
|
||||
height: fold?undefined:`${totalHeight-HEADER_HEIGHT}px`,
|
||||
}}>
|
||||
@@ -187,8 +205,15 @@ const Settings = () => {
|
||||
<button onClick={onSelFontSize2} className={classNames('btn btn-xs no-animation', fontSizeValue === 'large'?'btn-active':'')}>加大</button>
|
||||
</div>
|
||||
</FormItem>
|
||||
<FormItem title='AI类型' tip='不同AI质量可能有差异'>
|
||||
<div className="btn-group">
|
||||
<button onClick={onSelOpenai} className={classNames('btn btn-xs no-animation', (!aiTypeValue || aiTypeValue === 'openai')?'btn-active':'')}>OpenAI</button>
|
||||
<button onClick={onSelGemini} className={classNames('btn btn-xs no-animation', aiTypeValue === 'gemini'?'btn-active':'')}>Gemini</button>
|
||||
</div>
|
||||
</FormItem>
|
||||
</Section>
|
||||
<Section title='openai配置'>
|
||||
|
||||
{(!aiTypeValue || aiTypeValue === 'openai') && <Section title='openai配置'>
|
||||
<FormItem title='ApiKey' htmlFor='apiKey'>
|
||||
<input id='apiKey' type='text' className='input input-sm input-bordered w-full' placeholder='sk-xxx' value={apiKeyValue} onChange={onChangeApiKeyValue}/>
|
||||
</FormItem>
|
||||
@@ -239,10 +264,52 @@ const Settings = () => {
|
||||
}}/>
|
||||
</FormItem>)}
|
||||
</div>}
|
||||
</Section>
|
||||
</Section>}
|
||||
|
||||
{aiTypeValue === 'gemini' && <Section title='gemini配置'>
|
||||
<FormItem title='ApiKey' htmlFor='geminiApiKey'>
|
||||
<input id='geminiApiKey' type='text' className='input input-sm input-bordered w-full' placeholder='xxx'
|
||||
value={geminiApiKeyValue} onChange={onChangeGeminiApiKeyValue}/>
|
||||
</FormItem>
|
||||
<div className='flex justify-center'>
|
||||
<a className='link text-xs' onClick={toggleMoreFold}>{moreFold ? '点击查看说明' : '点击折叠说明'}</a>
|
||||
</div>
|
||||
{!moreFold && <div>
|
||||
<ul className='pl-3 list-decimal desc text-xs'>
|
||||
<li>官方网址:<a className='link' href='https://makersuite.google.com/app/apikey' target='_blank'
|
||||
rel="noreferrer">Google AI Studio</a> (目前免费)</li>
|
||||
</ul>
|
||||
</div>}
|
||||
<div className='flex justify-center'>
|
||||
<a className='link text-xs'
|
||||
onClick={togglePromptsFold}>{promptsFold ? '点击查看提示词' : '点击折叠提示词'}</a>
|
||||
</div>
|
||||
{!promptsFold && <div>
|
||||
{PROMPT_TYPES.map((item, idx) => <FormItem key={item.type} title={<div>
|
||||
<div>{item.name}</div>
|
||||
<div className='link text-xs' onClick={() => {
|
||||
setPromptsValue({
|
||||
...promptsValue,
|
||||
// @ts-expect-error
|
||||
[item.type]: PROMPT_DEFAULTS[item.type] ?? ''
|
||||
})
|
||||
}}>点击填充默认
|
||||
</div>
|
||||
</div>} htmlFor={`prompt-${item.type}`}>
|
||||
<textarea id={`prompt-${item.type}`} className='mt-2 textarea input-bordered w-full'
|
||||
placeholder='留空使用默认提示词' value={promptsValue[item.type] ?? ''} onChange={(e) => {
|
||||
setPromptsValue({
|
||||
...promptsValue,
|
||||
[item.type]: e.target.value
|
||||
})
|
||||
}}/>
|
||||
</FormItem>)}
|
||||
</div>}
|
||||
</Section>}
|
||||
|
||||
<Section title={<div className='flex items-center'>
|
||||
翻译配置
|
||||
{!apiKeyValue && <div className='tooltip tooltip-right ml-1' data-tip='未设置ApiKey无法使用'>
|
||||
{!apiKeySetted && <div className='tooltip tooltip-right ml-1' data-tip='未设置ApiKey无法使用'>
|
||||
<IoWarning className='text-sm text-warning'/>
|
||||
</div>}
|
||||
</div>}>
|
||||
@@ -277,7 +344,7 @@ const Settings = () => {
|
||||
</Section>
|
||||
<Section title={<div className='flex items-center'>
|
||||
总结配置
|
||||
{!apiKeyValue && <div className='tooltip tooltip-right ml-1' data-tip='未设置ApiKey无法使用'>
|
||||
{!apiKeySetted && <div className='tooltip tooltip-right ml-1' data-tip='未设置ApiKey无法使用'>
|
||||
<IoWarning className='text-sm text-warning'/>
|
||||
</div>}
|
||||
</div>}>
|
||||
|
@@ -18,3 +18,16 @@ export const handleChatCompleteTask = async (task: Task) => {
|
||||
throw new Error(`${task.resp.error.code as string??''} ${task.resp.error.message as string ??''}`)
|
||||
}
|
||||
}
|
||||
|
||||
export const handleGeminiChatCompleteTask = async (task: Task) => {
|
||||
const data = task.def.data
|
||||
const resp = await fetch('https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-goog-api-key': task.def.extra.geminiApiKey,
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
})
|
||||
task.resp = await resp.json()
|
||||
}
|
||||
|
@@ -1,5 +1,5 @@
|
||||
import {TASK_EXPIRE_TIME} from '../const'
|
||||
import {handleChatCompleteTask} from './openaiService'
|
||||
import {handleChatCompleteTask, handleGeminiChatCompleteTask} from './openaiService'
|
||||
|
||||
export const tasksMap = new Map<string, Task>()
|
||||
|
||||
@@ -11,6 +11,9 @@ export const handleTask = async (task: Task) => {
|
||||
case 'chatComplete':
|
||||
await handleChatCompleteTask(task)
|
||||
break
|
||||
case 'geminiChatComplete':
|
||||
await handleGeminiChatCompleteTask(task)
|
||||
break
|
||||
default:
|
||||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
||||
throw new Error(`任务类型不支持: ${task.def.type}`)
|
||||
|
@@ -158,8 +158,8 @@ export const MODELS = [{
|
||||
code: 'gpt-3.5-turbo',
|
||||
name: 'gpt-3.5-turbo',
|
||||
}, {
|
||||
code: 'gpt-3.5-turbo-16k',
|
||||
name: 'gpt-3.5-turbo-16k',
|
||||
code: 'gpt-3.5-turbo-0125',
|
||||
name: 'gpt-3.5-turbo-0125',
|
||||
}, {
|
||||
code: 'gpt-3.5-turbo-1106',
|
||||
name: 'gpt-3.5-turbo-1106',
|
||||
|
@@ -14,7 +14,7 @@ import {
|
||||
setTotalHeight,
|
||||
} from '../redux/envReducer'
|
||||
import {EventBusContext} from '../Router'
|
||||
import {EVENT_EXPAND, TOTAL_HEIGHT_MAX, TOTAL_HEIGHT_MIN, WORDS_DEFAULT, WORDS_MAX, WORDS_MIN} from '../const'
|
||||
import {EVENT_EXPAND, TOTAL_HEIGHT_MAX, TOTAL_HEIGHT_MIN, WORDS_DEFAULT, WORDS_MIN} from '../const'
|
||||
import {useInterval} from 'ahooks'
|
||||
import {getWholeText} from '../util/biz_util'
|
||||
|
||||
@@ -157,7 +157,7 @@ const useSubtitleService = () => {
|
||||
if (items != null) {
|
||||
if (envData.summarizeEnable) { // 分段
|
||||
let size = envData.words??WORDS_DEFAULT
|
||||
size = Math.min(Math.max(size, WORDS_MIN), WORDS_MAX)
|
||||
size = Math.max(size, WORDS_MIN)
|
||||
|
||||
segments = []
|
||||
let transcriptItems: TranscriptItem[] = []
|
||||
|
@@ -82,23 +82,39 @@ const useTranslate = () => {
|
||||
prompt = prompt.replaceAll('{{subtitles}}', lineStr)
|
||||
|
||||
const taskDef: TaskDef = {
|
||||
type: 'chatComplete',
|
||||
type: envData.aiType === 'gemini'?'geminiChatComplete':'chatComplete',
|
||||
serverUrl: envData.serverUrl,
|
||||
data: {
|
||||
model: envData.model??MODEL_DEFAULT,
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: prompt,
|
||||
data: envData.aiType === 'gemini'
|
||||
?{
|
||||
contents: [
|
||||
{
|
||||
parts: [
|
||||
{
|
||||
text: prompt
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
generationConfig: {
|
||||
maxOutputTokens: 2048
|
||||
}
|
||||
}
|
||||
],
|
||||
temperature: 0,
|
||||
n: 1,
|
||||
stream: false,
|
||||
},
|
||||
:{
|
||||
model: envData.model??MODEL_DEFAULT,
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: prompt,
|
||||
}
|
||||
],
|
||||
temperature: 0,
|
||||
n: 1,
|
||||
stream: false,
|
||||
},
|
||||
extra: {
|
||||
type: 'translate',
|
||||
apiKey: envData.apiKey,
|
||||
geminiApiKey: envData.geminiApiKey,
|
||||
startIdx,
|
||||
size: lines.length,
|
||||
}
|
||||
@@ -117,10 +133,10 @@ const useTranslate = () => {
|
||||
dispatch(addTaskId(task.id))
|
||||
}
|
||||
}
|
||||
}, [data?.body, dispatch, envData.apiKey, envData.fetchAmount, envData.serverUrl, envData.prompts, title, language.name])
|
||||
}, [data?.body, envData.fetchAmount, envData.prompts, envData.aiType, envData.serverUrl, envData.model, envData.apiKey, envData.geminiApiKey, language.name, title, dispatch])
|
||||
|
||||
const addSummarizeTask = useCallback(async (type: SummaryType, segment: Segment) => {
|
||||
if (segment.text.length >= SUMMARIZE_THRESHOLD && envData.apiKey) {
|
||||
if (segment.text.length >= SUMMARIZE_THRESHOLD) {
|
||||
let subtitles = ''
|
||||
for (const item of segment.items) {
|
||||
subtitles += formatTime(item.from) + ' ' + item.content + '\n'
|
||||
@@ -135,25 +151,41 @@ const useTranslate = () => {
|
||||
prompt = prompt.replaceAll('{{segment}}', segment.text)
|
||||
|
||||
const taskDef: TaskDef = {
|
||||
type: 'chatComplete',
|
||||
type: envData.aiType === 'gemini'?'geminiChatComplete':'chatComplete',
|
||||
serverUrl: envData.serverUrl,
|
||||
data: {
|
||||
model: envData.model??MODEL_DEFAULT,
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: prompt,
|
||||
data: envData.aiType === 'gemini'
|
||||
?{
|
||||
contents: [
|
||||
{
|
||||
parts: [
|
||||
{
|
||||
text: prompt
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
generationConfig: {
|
||||
maxOutputTokens: 2048
|
||||
}
|
||||
}
|
||||
],
|
||||
temperature: 0,
|
||||
n: 1,
|
||||
stream: false,
|
||||
},
|
||||
:{
|
||||
model: envData.model??MODEL_DEFAULT,
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: prompt,
|
||||
}
|
||||
],
|
||||
temperature: 0,
|
||||
n: 1,
|
||||
stream: false,
|
||||
},
|
||||
extra: {
|
||||
type: 'summarize',
|
||||
summaryType: type,
|
||||
startIdx: segment.startIdx,
|
||||
apiKey: envData.apiKey,
|
||||
geminiApiKey: envData.geminiApiKey,
|
||||
}
|
||||
}
|
||||
console.debug('addSummarizeTask', taskDef)
|
||||
@@ -162,7 +194,7 @@ const useTranslate = () => {
|
||||
const task = await chrome.runtime.sendMessage({type: 'addTask', taskDef})
|
||||
dispatch(addTaskId(task.id))
|
||||
}
|
||||
}, [dispatch, envData.apiKey, envData.prompts, envData.serverUrl, summarizeLanguage.name, title])
|
||||
}, [dispatch, envData.aiType, envData.apiKey, envData.geminiApiKey, envData.model, envData.prompts, envData.serverUrl, summarizeLanguage.name, title])
|
||||
|
||||
const handleTranslate = useMemoizedFn((task: Task, content: string) => {
|
||||
let map: {[key: string]: string} = {}
|
||||
@@ -221,7 +253,7 @@ const useTranslate = () => {
|
||||
console.debug('getTask', taskResp.task)
|
||||
const task: Task = taskResp.task
|
||||
const taskType: string | undefined = task.def.extra?.type
|
||||
const content = task.resp?.choices?.[0]?.message?.content?.trim()
|
||||
const content = envData.aiType === 'gemini'?task.resp?.candidates[0]?.content?.parts[0]?.text?.trim():task.resp?.choices?.[0]?.message?.content?.trim()
|
||||
if (task.status === 'done') {
|
||||
// 异常提示
|
||||
if (task.error) {
|
||||
@@ -239,7 +271,7 @@ const useTranslate = () => {
|
||||
} else {
|
||||
dispatch(delTaskId(taskId))
|
||||
}
|
||||
}, [dispatch, handleSummarize, handleTranslate])
|
||||
}, [dispatch, envData.aiType, handleSummarize, handleTranslate])
|
||||
|
||||
return {getFetch, getTask, addTask, addSummarizeTask}
|
||||
}
|
||||
|
@@ -17,7 +17,6 @@ interface EnvState {
|
||||
autoScroll?: boolean
|
||||
checkAutoScroll?: boolean
|
||||
curOffsetTop?: number
|
||||
compact?: boolean // 是否紧凑视图
|
||||
floatKeyPointsSegIdx?: number // segment的startIdx
|
||||
|
||||
noVideo?: boolean
|
||||
@@ -25,7 +24,6 @@ interface EnvState {
|
||||
curIdx?: number // 从0开始
|
||||
needScroll?: boolean
|
||||
currentTime?: number
|
||||
downloadType?: string
|
||||
infos?: any[]
|
||||
curInfo?: any
|
||||
curFetched?: boolean
|
||||
@@ -81,7 +79,7 @@ export const slice = createSlice({
|
||||
setEnvReady: (state) => {
|
||||
state.envReady = true
|
||||
},
|
||||
setTempData: (state, action: PayloadAction<TempData>) => {
|
||||
setTempData: (state, action: PayloadAction<Partial<TempData>>) => {
|
||||
state.tempData = {
|
||||
...state.tempData,
|
||||
...action.payload,
|
||||
@@ -102,9 +100,6 @@ export const slice = createSlice({
|
||||
setFoldAll: (state, action: PayloadAction<boolean>) => {
|
||||
state.foldAll = action.payload
|
||||
},
|
||||
setCompact: (state, action: PayloadAction<boolean>) => {
|
||||
state.compact = action.payload
|
||||
},
|
||||
setPage: (state, action: PayloadAction<string | undefined>) => {
|
||||
state.page = action.payload
|
||||
},
|
||||
@@ -230,9 +225,6 @@ export const slice = createSlice({
|
||||
setNoVideo: (state, action: PayloadAction<boolean>) => {
|
||||
state.noVideo = action.payload
|
||||
},
|
||||
setDownloadType: (state, action: PayloadAction<string>) => {
|
||||
state.downloadType = action.payload
|
||||
},
|
||||
setNeedScroll: (state, action: PayloadAction<boolean>) => {
|
||||
state.needScroll = action.payload
|
||||
},
|
||||
@@ -275,7 +267,6 @@ export const {
|
||||
setCurOffsetTop,
|
||||
setFloatKeyPointsSegIdx,
|
||||
setFoldAll,
|
||||
setCompact,
|
||||
setSegmentFold,
|
||||
setSummaryContent,
|
||||
setSummaryStatus,
|
||||
@@ -290,7 +281,6 @@ export const {
|
||||
addTaskId,
|
||||
delTaskId,
|
||||
setTaskIds,
|
||||
setDownloadType,
|
||||
setAutoTranslate,
|
||||
setAutoScroll,
|
||||
setNoVideo,
|
||||
|
10
src/typings.d.ts
vendored
10
src/typings.d.ts
vendored
@@ -1,9 +1,15 @@
|
||||
interface EnvData {
|
||||
autoExpand?: boolean
|
||||
flagDot?: boolean
|
||||
|
||||
aiType?: 'openai' | 'gemini'
|
||||
// openai
|
||||
apiKey?: string
|
||||
serverUrl?: string
|
||||
model?: string
|
||||
// gemini
|
||||
geminiApiKey?: string
|
||||
|
||||
translateEnable?: boolean
|
||||
language?: string
|
||||
hideOnDisableAutoTranslate?: boolean
|
||||
@@ -27,10 +33,12 @@ interface EnvData {
|
||||
|
||||
interface TempData {
|
||||
curSummaryType: SummaryType
|
||||
downloadType?: string
|
||||
compact?: boolean // 是否紧凑视图
|
||||
}
|
||||
|
||||
interface TaskDef {
|
||||
type: 'chatComplete'
|
||||
type: 'chatComplete' | 'geminiChatComplete'
|
||||
serverUrl?: string
|
||||
data: any
|
||||
extra?: any
|
||||
|
@@ -253,7 +253,15 @@ export const extractJsonObject = (content: string) => {
|
||||
const start = content.indexOf('```')
|
||||
const end = content.lastIndexOf('```')
|
||||
if (start >= 0 && end >= 0) {
|
||||
content = content.slice(start + 3, end)
|
||||
if (start === end) { // 异常情况
|
||||
if (content.startsWith('```')) {
|
||||
content = content.slice(3)
|
||||
} else {
|
||||
content = content.slice(0, -3)
|
||||
}
|
||||
} else {
|
||||
content = content.slice(start + 3, end)
|
||||
}
|
||||
}
|
||||
// get content between { and }
|
||||
const start2 = content.indexOf('{')
|
||||
@@ -269,7 +277,15 @@ export const extractJsonArray = (content: string) => {
|
||||
const start = content.indexOf('```')
|
||||
const end = content.lastIndexOf('```')
|
||||
if (start >= 0 && end >= 0) {
|
||||
content = content.slice(start + 3, end)
|
||||
if (start === end) { // 异常情况
|
||||
if (content.startsWith('```')) {
|
||||
content = content.slice(3)
|
||||
} else {
|
||||
content = content.slice(0, -3)
|
||||
}
|
||||
} else {
|
||||
content = content.slice(start + 3, end)
|
||||
}
|
||||
}
|
||||
// get content between [ and ]
|
||||
const start3 = content.indexOf('[')
|
||||
|
@@ -18,11 +18,13 @@ module.exports = {
|
||||
light: {
|
||||
...require("daisyui/src/colors/themes")["[data-theme=light]"],
|
||||
"--rounded-btn": "0.15rem",
|
||||
"primary": "rgb(0, 174, 236)",
|
||||
},
|
||||
}, {
|
||||
dark: {
|
||||
...require("daisyui/src/colors/themes")["[data-theme=dark]"],
|
||||
"--rounded-btn": "0.15rem",
|
||||
"primary": "rgb(0, 174, 236)",
|
||||
}
|
||||
}],
|
||||
base: true,
|
||||
|
66
vite.config.ts.timestamp-1705728659971.mjs
Normal file
66
vite.config.ts.timestamp-1705728659971.mjs
Normal file
@@ -0,0 +1,66 @@
|
||||
// vite.config.ts
|
||||
import { defineConfig } from "vite";
|
||||
import react from "@vitejs/plugin-react";
|
||||
import { visualizer } from "rollup-plugin-visualizer";
|
||||
import { crx } from "@crxjs/vite-plugin";
|
||||
|
||||
// manifest.json
|
||||
var manifest_default = {
|
||||
name: "\u54D4\u54E9\u54D4\u54E9\u5B57\u5E55\u5217\u8868",
|
||||
description: "\u663E\u793AB\u7AD9\u89C6\u9891\u7684\u5B57\u5E55\u5217\u8868,\u53EF\u70B9\u51FB\u8DF3\u8F6C\u4E0E\u4E0B\u8F7D\u5B57\u5E55,\u5E76\u652F\u6301\u7FFB\u8BD1\u548C\u603B\u7ED3\u5B57\u5E55!",
|
||||
version: "1.7.11",
|
||||
manifest_version: 3,
|
||||
permissions: [
|
||||
"storage"
|
||||
],
|
||||
background: {
|
||||
service_worker: "src/chrome/background.ts"
|
||||
},
|
||||
content_scripts: [
|
||||
{
|
||||
matches: ["https://www.bilibili.com/video/*", "https://www.bilibili.com/list/*"],
|
||||
js: ["src/chrome/content-script.cjs"]
|
||||
}
|
||||
],
|
||||
icons: {
|
||||
"16": "favicon-16x16.png",
|
||||
"32": "favicon-32x32.png",
|
||||
"48": "favicon-48x48.png",
|
||||
"128": "favicon-128x128.png"
|
||||
},
|
||||
action: {
|
||||
default_popup: "index.html",
|
||||
default_icon: {
|
||||
"16": "favicon-16x16.png",
|
||||
"32": "favicon-32x32.png",
|
||||
"48": "favicon-48x48.png",
|
||||
"128": "favicon-128x128.png"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// vite.config.ts
|
||||
var vite_config_default = ({ mode }) => {
|
||||
const plugins = [
|
||||
react(),
|
||||
visualizer()
|
||||
];
|
||||
if (mode === "production_chrome") {
|
||||
plugins.push(crx({
|
||||
manifest: manifest_default
|
||||
}));
|
||||
}
|
||||
return defineConfig({
|
||||
base: "/",
|
||||
plugins,
|
||||
css: {
|
||||
modules: {
|
||||
localsConvention: "camelCase"
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
export {
|
||||
vite_config_default as default
|
||||
};
|
||||
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsidml0ZS5jb25maWcudHMiXSwKICAic291cmNlc0NvbnRlbnQiOiBbImNvbnN0IF9fdml0ZV9pbmplY3RlZF9vcmlnaW5hbF9kaXJuYW1lID0gXCIvVXNlcnMvZmVuZ3l1ZXhpYW5nL2RhdGEvcHJvamVjdC9iaWxpYmlsaS1zdWJ0aXRsZVwiO2NvbnN0IF9fdml0ZV9pbmplY3RlZF9vcmlnaW5hbF9maWxlbmFtZSA9IFwiL1VzZXJzL2Zlbmd5dWV4aWFuZy9kYXRhL3Byb2plY3QvYmlsaWJpbGktc3VidGl0bGUvdml0ZS5jb25maWcudHNcIjtjb25zdCBfX3ZpdGVfaW5qZWN0ZWRfb3JpZ2luYWxfaW1wb3J0X21ldGFfdXJsID0gXCJmaWxlOi8vL1VzZXJzL2Zlbmd5dWV4aWFuZy9kYXRhL3Byb2plY3QvYmlsaWJpbGktc3VidGl0bGUvdml0ZS5jb25maWcudHNcIjtpbXBvcnQge2RlZmluZUNvbmZpZywgUGx1Z2luT3B0aW9ufSBmcm9tICd2aXRlJ1xuaW1wb3J0IHJlYWN0IGZyb20gJ0B2aXRlanMvcGx1Z2luLXJlYWN0J1xuaW1wb3J0IHt2aXN1YWxpemVyfSBmcm9tIFwicm9sbHVwLXBsdWdpbi12aXN1YWxpemVyXCI7XG5pbXBvcnQge2NyeH0gZnJvbSAnQGNyeGpzL3ZpdGUtcGx1Z2luJ1xuLy8gQHRzLWlnbm9yZVxuaW1wb3J0IG1hbmlmZXN0IGZyb20gJy4vbWFuaWZlc3QuanNvbidcblxuLy8gaHR0cHM6Ly92aXRlanMuZGV2L2NvbmZpZy9cbmV4cG9ydCBkZWZhdWx0ICh7bW9kZX0pID0+IHtcbiAgY29uc3QgcGx1Z2lucyA9IFtcbiAgICByZWFjdCgpLFxuICAgIHZpc3VhbGl6ZXIoKSBhcyBQbHVnaW5PcHRpb24sXG4gIF1cbiAgLy8gQHRzLWlnbm9yZVxuICBpZiAobW9kZSA9PT0gJ3Byb2R1Y3Rpb25fY2hyb21lJykge1xuICAgIHBsdWdpbnMucHVzaChjcngoe1xuICAgICAgbWFuaWZlc3QsXG4gICAgfSkpXG4gIH1cbiAgcmV0dXJuIGRlZmluZUNvbmZpZyh7XG4gICAgYmFzZTogJy8nLFxuICAgIHBsdWdpbnMsXG4gICAgY3NzOiB7XG4gICAgICBtb2R1bGVzOiB7XG4gICAgICAgIGxvY2Fsc0NvbnZlbnRpb246IFwiY2FtZWxDYXNlXCJcbiAgICAgIH1cbiAgICB9XG4gIH0pXG59XG4iXSwKICAibWFwcGluZ3MiOiAiO0FBQXdVLFNBQVEsb0JBQWlDO0FBQ2pYLE9BQU8sV0FBVztBQUNsQixTQUFRLGtCQUFpQjtBQUN6QixTQUFRLFdBQVU7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0FBS2xCLElBQU8sc0JBQVEsQ0FBQyxFQUFDLEtBQUksTUFBTTtBQUN6QixRQUFNLFVBQVU7QUFBQSxJQUNkLE1BQU07QUFBQSxJQUNOLFdBQVc7QUFBQSxFQUNiO0FBRUEsTUFBSSxTQUFTLHFCQUFxQjtBQUNoQyxZQUFRLEtBQUssSUFBSTtBQUFBLE1BQ2Y7QUFBQSxJQUNGLENBQUMsQ0FBQztBQUFBLEVBQ0o7QUFDQSxTQUFPLGFBQWE7QUFBQSxJQUNsQixNQUFNO0FBQUEsSUFDTjtBQUFBLElBQ0EsS0FBSztBQUFBLE1BQ0gsU0FBUztBQUFBLFFBQ1Asa0JBQWtCO0FBQUEsTUFDcEI7QUFBQSxJQUNGO0FBQUEsRUFDRixDQUFDO0FBQ0g7IiwKICAibmFtZXMiOiBbXQp9Cg==
|
Reference in New Issue
Block a user