You've already forked bilibili-subtitle
优化
This commit is contained in:
@@ -78,7 +78,7 @@ const methods: {
|
|||||||
const extensionMessaging = new ExtensionMessaging()
|
const extensionMessaging = new ExtensionMessaging()
|
||||||
extensionMessaging.init(methods)
|
extensionMessaging.init(methods)
|
||||||
|
|
||||||
chrome.runtime.onMessage.addListener((event: MessageData, sender: chrome.runtime.MessageSender, sendResponse: (result: any) => void) => {
|
chrome.runtime.onMessage.addListener((event: any, sender: chrome.runtime.MessageSender, sendResponse: (result: any) => void) => {
|
||||||
// debug((sender.tab != null) ? `tab ${sender.tab.url ?? ''} => ` : 'extension => ', event)
|
// debug((sender.tab != null) ? `tab ${sender.tab.url ?? ''} => ` : 'extension => ', event)
|
||||||
|
|
||||||
// legacy
|
// legacy
|
||||||
|
91
src/message-typings.d.ts
vendored
Normal file
91
src/message-typings.d.ts
vendored
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
//extension
|
||||||
|
interface ExtensionCloseSidePanelMessage extends ExtensionMessage<{}> {
|
||||||
|
method: 'CLOSE_SIDE_PANEL';
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ExtensionAddTaskMessage extends ExtensionMessage<{ taskDef: TaskDef }> {
|
||||||
|
method: 'ADD_TASK';
|
||||||
|
return: Task
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ExtensionGetTaskMessage extends ExtensionMessage<{ taskId: string }> {
|
||||||
|
method: 'GET_TASK';
|
||||||
|
return: {
|
||||||
|
code: 'ok'
|
||||||
|
task: Task
|
||||||
|
} | {
|
||||||
|
code: 'not_found'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ExtensionShowFlagMessage extends ExtensionMessage<{ show: boolean }> {
|
||||||
|
method: 'SHOW_FLAG';
|
||||||
|
}
|
||||||
|
|
||||||
|
type AllExtensionMessages = ExtensionCloseSidePanelMessage | ExtensionAddTaskMessage | ExtensionGetTaskMessage | ExtensionShowFlagMessage
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//inject
|
||||||
|
interface InjectToggleDisplayMessage extends InjectMessage<{}> {
|
||||||
|
method: 'TOGGLE_DISPLAY';
|
||||||
|
}
|
||||||
|
|
||||||
|
interface InjectFoldMessage extends InjectMessage<{ fold: boolean }> {
|
||||||
|
method: 'FOLD';
|
||||||
|
}
|
||||||
|
|
||||||
|
interface InjectMoveMessage extends InjectMessage<{ time: number, togglePause: boolean }> {
|
||||||
|
method: 'MOVE';
|
||||||
|
}
|
||||||
|
|
||||||
|
interface InjectGetSubtitleMessage extends InjectMessage<{ info: any }> {
|
||||||
|
method: 'GET_SUBTITLE';
|
||||||
|
}
|
||||||
|
|
||||||
|
interface InjectGetVideoStatusMessage extends InjectMessage<{}> {
|
||||||
|
method: 'GET_VIDEO_STATUS';
|
||||||
|
}
|
||||||
|
|
||||||
|
interface InjectGetVideoElementInfoMessage extends InjectMessage<{}> {
|
||||||
|
method: 'GET_VIDEO_ELEMENT_INFO';
|
||||||
|
}
|
||||||
|
|
||||||
|
interface InjectRefreshVideoInfoMessage extends InjectMessage<{ force: boolean }> {
|
||||||
|
method: 'REFRESH_VIDEO_INFO';
|
||||||
|
}
|
||||||
|
|
||||||
|
interface InjectUpdateTransResultMessage extends InjectMessage<{ result: string }> {
|
||||||
|
method: 'UPDATE_TRANS_RESULT';
|
||||||
|
}
|
||||||
|
|
||||||
|
interface InjectHideTransMessage extends InjectMessage<{}> {
|
||||||
|
method: 'HIDE_TRANS';
|
||||||
|
}
|
||||||
|
|
||||||
|
interface InjectPlayMessage extends InjectMessage<{ play: boolean }> {
|
||||||
|
method: 'PLAY';
|
||||||
|
}
|
||||||
|
|
||||||
|
interface InjectDownloadAudioMessage extends InjectMessage<{}> {
|
||||||
|
method: 'DOWNLOAD_AUDIO';
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
111
src/typings.d.ts
vendored
111
src/typings.d.ts
vendored
@@ -1,114 +1,5 @@
|
|||||||
//extension
|
|
||||||
interface ExtensionCloseSidePanelMessage extends ExtensionMessage<{}> {
|
|
||||||
method: 'CLOSE_SIDE_PANEL';
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ExtensionAddTaskMessage extends ExtensionMessage<{ taskDef: TaskDef }> {
|
|
||||||
method: 'ADD_TASK';
|
|
||||||
return: Task
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ExtensionGetTaskMessage extends ExtensionMessage<{ taskId: string }> {
|
|
||||||
method: 'GET_TASK';
|
|
||||||
return: {
|
|
||||||
code: 'ok'
|
|
||||||
task: Task
|
|
||||||
} | {
|
|
||||||
code: 'not_found'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ExtensionShowFlagMessage extends ExtensionMessage<{ show: boolean }> {
|
|
||||||
method: 'SHOW_FLAG';
|
|
||||||
}
|
|
||||||
|
|
||||||
type AllExtensionMessages = ExtensionCloseSidePanelMessage | ExtensionAddTaskMessage | ExtensionGetTaskMessage | ExtensionShowFlagMessage
|
|
||||||
|
|
||||||
//inject
|
|
||||||
interface InjectToggleDisplayMessage extends InjectMessage<{}> {
|
|
||||||
method: 'TOGGLE_DISPLAY';
|
|
||||||
}
|
|
||||||
|
|
||||||
interface InjectFoldMessage extends InjectMessage<{ fold: boolean }> {
|
|
||||||
method: 'FOLD';
|
|
||||||
}
|
|
||||||
|
|
||||||
interface InjectMoveMessage extends InjectMessage<{ time: number, togglePause: boolean }> {
|
|
||||||
method: 'MOVE';
|
|
||||||
}
|
|
||||||
|
|
||||||
interface InjectGetSubtitleMessage extends InjectMessage<{ info: any }> {
|
|
||||||
method: 'GET_SUBTITLE';
|
|
||||||
}
|
|
||||||
|
|
||||||
interface InjectGetVideoStatusMessage extends InjectMessage<{}> {
|
|
||||||
method: 'GET_VIDEO_STATUS';
|
|
||||||
}
|
|
||||||
|
|
||||||
interface InjectGetVideoElementInfoMessage extends InjectMessage<{}> {
|
|
||||||
method: 'GET_VIDEO_ELEMENT_INFO';
|
|
||||||
}
|
|
||||||
|
|
||||||
interface InjectRefreshVideoInfoMessage extends InjectMessage<{ force: boolean }> {
|
|
||||||
method: 'REFRESH_VIDEO_INFO';
|
|
||||||
}
|
|
||||||
|
|
||||||
interface InjectUpdateTransResultMessage extends InjectMessage<{ result: string }> {
|
|
||||||
method: 'UPDATE_TRANS_RESULT';
|
|
||||||
}
|
|
||||||
|
|
||||||
interface InjectHideTransMessage extends InjectMessage<{}> {
|
|
||||||
method: 'HIDE_TRANS';
|
|
||||||
}
|
|
||||||
|
|
||||||
interface InjectPlayMessage extends InjectMessage<{ play: boolean }> {
|
|
||||||
method: 'PLAY';
|
|
||||||
}
|
|
||||||
|
|
||||||
interface InjectDownloadAudioMessage extends InjectMessage<{}> {
|
|
||||||
method: 'DOWNLOAD_AUDIO';
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
error?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
type MessageFrom = 'extension' | 'inject' | 'app'
|
|
||||||
|
|
||||||
interface MessageData {
|
|
||||||
from: MessageFrom
|
|
||||||
target: string
|
|
||||||
method: string
|
|
||||||
params?: any
|
|
||||||
[key: string]: any
|
|
||||||
}
|
|
||||||
|
|
||||||
interface MessageResult {
|
|
||||||
success: boolean
|
|
||||||
code: number
|
|
||||||
message?: string
|
|
||||||
data?: any
|
|
||||||
}
|
|
||||||
|
|
||||||
interface MethodContext {
|
interface MethodContext {
|
||||||
from: MessageFrom
|
from: 'extension' | 'inject' | 'app'
|
||||||
event: any
|
event: any
|
||||||
tabId?: number
|
tabId?: number
|
||||||
// sender?: chrome.runtime.MessageSender | null
|
// sender?: chrome.runtime.MessageSender | null
|
||||||
|
Reference in New Issue
Block a user