From 3e1e69f13e4f40359a2b4543bbb681d34f35e420 Mon Sep 17 00:00:00 2001 From: IndieKKY Date: Sun, 6 Oct 2024 18:36:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/inject/inject.ts | 10 +++++----- src/messaging/layer2/InjectMessaging.ts | 9 +++------ src/typings.d.ts | 11 +++++++++++ 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/inject/inject.ts b/src/inject/inject.ts index 77578ae..5e4b324 100644 --- a/src/inject/inject.ts +++ b/src/inject/inject.ts @@ -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 { diff --git a/src/messaging/layer2/InjectMessaging.ts b/src/messaging/layer2/InjectMessaging.ts index c83afe4..5230d19 100644 --- a/src/messaging/layer2/InjectMessaging.ts +++ b/src/messaging/layer2/InjectMessaging.ts @@ -76,24 +76,21 @@ class InjectMessaging { }) } - sendExtension = async (method: string, params?: any): Promise => { + sendExtension = async (method: K, params?: Extract['params']): Promise['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 (method: string, params: any): Promise => { - if (method === 'setVideoInfo') { - console.log('sendApp>>>', method, params) - } + sendApp = async (method: K, params?: Extract['params']): Promise['return']> => { return this.sendExtension('ROUTE', { tags: [TAG_TARGET_APP], method, diff --git a/src/typings.d.ts b/src/typings.d.ts index 07f81cb..6dda904 100644 --- a/src/typings.d.ts +++ b/src/typings.d.ts @@ -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 { success: boolean; data?: T;