You've already forked bilibili-subtitle
优化
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { TOTAL_HEIGHT_DEF, HEADER_HEIGHT, TOTAL_HEIGHT_MIN, TOTAL_HEIGHT_MAX, IFRAME_ID, MESSAGE_TO_INJECT_DOWNLOAD_AUDIO, MESSAGE_TO_APP_SET_INFOS, MESSAGE_TO_INJECT_TOGGLE_DISPLAY, STORAGE_ENV, MESSAGE_TO_EXTENSION_SHOW_FLAG } from '@/consts/const'
|
import { TOTAL_HEIGHT_DEF, HEADER_HEIGHT, TOTAL_HEIGHT_MIN, TOTAL_HEIGHT_MAX, IFRAME_ID, MESSAGE_TO_INJECT_DOWNLOAD_AUDIO, MESSAGE_TO_APP_SET_INFOS, MESSAGE_TO_INJECT_TOGGLE_DISPLAY, STORAGE_ENV, MESSAGE_TO_EXTENSION_SHOW_FLAG } from '@/consts/const'
|
||||||
import { MESSAGE_TO_INJECT_FOLD, MESSAGE_TO_INJECT_MOVE, MESSAGE_TO_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_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 InjectMessaging from '@/messaging/layer2/InjectMessaging'
|
import InjectMessaging from '@/messaging/layer2/InjectMessaging'
|
||||||
|
|
||||||
const debug = (...args: any[]) => {
|
const debug = (...args: any[]) => {
|
||||||
@@ -98,7 +98,7 @@ const debug = (...args: any[]) => {
|
|||||||
danmukuBox?.insertBefore(iframe, danmukuBox?.firstChild)
|
danmukuBox?.insertBefore(iframe, danmukuBox?.firstChild)
|
||||||
|
|
||||||
// show badge
|
// show badge
|
||||||
runtime.injectMessaging.sendExtension(MESSAGE_TO_EXTENSION_SHOW_FLAG, {
|
runtime.injectMessaging.sendExtension('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.injectMessaging.sendApp(MESSAGE_TO_APP_SET_VIDEO_INFO, {
|
runtime.injectMessaging.sendApp('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.injectMessaging.sendApp(MESSAGE_TO_APP_SET_INFOS, {
|
runtime.injectMessaging.sendApp('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.injectMessaging.sendExtension(MESSAGE_TO_EXTENSION_SHOW_FLAG, {
|
runtime.injectMessaging.sendExtension('SHOW_FLAG', {
|
||||||
show: iframe.style.display !== 'none'
|
show: iframe.style.display !== 'none'
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
@@ -76,24 +76,21 @@ class InjectMessaging {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
sendExtension = async <T = any>(method: string, params?: any): Promise<T> => {
|
sendExtension = async <M extends AllExtensionMessages | MessagingExtensionMessages, K extends M['method']>(method: K, params?: Extract<M, { method: K }>['params']): Promise<Extract<M, { method: K }>['return']> => {
|
||||||
return await this.l1protocol!.sendMessage({
|
return await this.l1protocol!.sendMessage({
|
||||||
from: 'inject',
|
from: 'inject',
|
||||||
method,
|
method,
|
||||||
params: params ?? {},
|
params: params ?? {},
|
||||||
}).then((res) => {
|
}).then((res) => {
|
||||||
if (res.code === 200) {
|
if (res.code === 200) {
|
||||||
return res.data as T
|
return res.data
|
||||||
} else {
|
} else {
|
||||||
throw new Error(res.msg)
|
throw new Error(res.msg)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
sendApp = async <T>(method: string, params: any): Promise<T> => {
|
sendApp = async <M extends AllAPPMessages, K extends M['method']>(method: K, params?: Extract<M, { method: K }>['params']): Promise<Extract<M, { method: K }>['return']> => {
|
||||||
if (method === 'setVideoInfo') {
|
|
||||||
console.log('sendApp>>>', method, params)
|
|
||||||
}
|
|
||||||
return this.sendExtension('ROUTE', {
|
return this.sendExtension('ROUTE', {
|
||||||
tags: [TAG_TARGET_APP],
|
tags: [TAG_TARGET_APP],
|
||||||
method,
|
method,
|
||||||
|
11
src/typings.d.ts
vendored
11
src/typings.d.ts
vendored
@@ -71,6 +71,17 @@ interface InjectDownloadAudioMessage extends InjectMessage<{}> {
|
|||||||
|
|
||||||
type AllInjectMessages = InjectToggleDisplayMessage | InjectFoldMessage | InjectMoveMessage | InjectGetSubtitleMessage | InjectGetVideoStatusMessage | InjectGetVideoElementInfoMessage | InjectRefreshVideoInfoMessage | InjectUpdateTransResultMessage | InjectHideTransMessage | InjectPlayMessage | InjectDownloadAudioMessage
|
type AllInjectMessages = InjectToggleDisplayMessage | InjectFoldMessage | InjectMoveMessage | InjectGetSubtitleMessage | InjectGetVideoStatusMessage | InjectGetVideoElementInfoMessage | InjectRefreshVideoInfoMessage | InjectUpdateTransResultMessage | InjectHideTransMessage | InjectPlayMessage | InjectDownloadAudioMessage
|
||||||
|
|
||||||
|
//app
|
||||||
|
interface AppSetInfosMessage extends AppMessage<{ infos: any }> {
|
||||||
|
method: 'SET_INFOS';
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AppSetVideoInfoMessage extends AppMessage<{ url: string, title: string, aid: number | null, pages: any, infos: any }> {
|
||||||
|
method: 'SET_VIDEO_INFO';
|
||||||
|
}
|
||||||
|
|
||||||
|
type AllAPPMessages = AppSetInfosMessage | AppSetVideoInfoMessage
|
||||||
|
|
||||||
interface MessageResponse<T = any> {
|
interface MessageResponse<T = any> {
|
||||||
success: boolean;
|
success: boolean;
|
||||||
data?: T;
|
data?: T;
|
||||||
|
Reference in New Issue
Block a user