This commit is contained in:
IndieKKY
2024-10-06 18:36:23 +08:00
parent 9384c1419a
commit 3e1e69f13e
3 changed files with 19 additions and 11 deletions

View File

@@ -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 { 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'
const debug = (...args: any[]) => {
@@ -98,7 +98,7 @@ const debug = (...args: any[]) => {
danmukuBox?.insertBefore(iframe, danmukuBox?.firstChild)
// show badge
runtime.injectMessaging.sendExtension(MESSAGE_TO_EXTENSION_SHOW_FLAG, {
runtime.injectMessaging.sendExtension('SHOW_FLAG', {
show: true
})
@@ -188,7 +188,7 @@ const debug = (...args: any[]) => {
debug('refreshVideoInfo: ', aid, cid, pages, subtitles)
//send setVideoInfo
runtime.injectMessaging.sendApp(MESSAGE_TO_APP_SET_VIDEO_INFO, {
runtime.injectMessaging.sendApp('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.injectMessaging.sendApp(MESSAGE_TO_APP_SET_INFOS, {
runtime.injectMessaging.sendApp('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.injectMessaging.sendExtension(MESSAGE_TO_EXTENSION_SHOW_FLAG, {
runtime.injectMessaging.sendExtension('SHOW_FLAG', {
show: iframe.style.display !== 'none'
})
} else {

View File

@@ -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({
from: 'inject',
method,
params: params ?? {},
}).then((res) => {
if (res.code === 200) {
return res.data as T
return res.data
} else {
throw new Error(res.msg)
}
})
}
sendApp = async <T>(method: string, params: any): Promise<T> => {
if (method === 'setVideoInfo') {
console.log('sendApp>>>', method, params)
}
sendApp = async <M extends AllAPPMessages, K extends M['method']>(method: K, params?: Extract<M, { method: K }>['params']): Promise<Extract<M, { method: K }>['return']> => {
return this.sendExtension('ROUTE', {
tags: [TAG_TARGET_APP],
method,

11
src/typings.d.ts vendored
View File

@@ -71,6 +71,17 @@ interface InjectDownloadAudioMessage extends InjectMessage<{}> {
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> {
success: boolean;
data?: T;