消息传递重构&支持侧边面板

This commit is contained in:
IndieKKY
2024-10-05 19:55:43 +08:00
parent c1c21c2fee
commit b283695b02
18 changed files with 454 additions and 214 deletions

View File

@@ -1,6 +1,6 @@
import {v4} from 'uuid'
import {handleTask, initTaskService, tasksMap} from './taskService'
import {MESSAGE_TARGET_INJECT, MESSAGE_TO_EXTENSION_ADD_TASK, MESSAGE_TO_EXTENSION_GET_TASK, MESSAGE_TO_EXTENSION_SHOW_FLAG, MESSAGE_TO_INJECT_TOGGLE_DISPLAY} from '@/const'
import {MESSAGE_TARGET_INJECT, MESSAGE_TO_EXTENSION_ADD_TASK, MESSAGE_TO_EXTENSION_GET_TASK, MESSAGE_TO_EXTENSION_SHOW_FLAG, MESSAGE_TO_INJECT_TOGGLE_DISPLAY, STORAGE_ENV} from '@/const'
import ExtensionMessage from '@/messaging/ExtensionMessage'
const setBadgeOk = async (tabId: number, ok: boolean) => {
@@ -58,7 +58,7 @@ const methods: {
}
},
[MESSAGE_TO_EXTENSION_SHOW_FLAG]: async (params, context) => {
await setBadgeOk(context.sender?.tab?.id!, params.show)
await setBadgeOk(context.tabId!, params.show)
},
}
// 初始化backgroundMessage
@@ -85,7 +85,21 @@ chrome.runtime.onMessage.addListener((event: MessageData, sender: chrome.runtime
//点击扩展图标
chrome.action.onClicked.addListener(async (tab) => {
extensionMessage.broadcastMessageExact([tab.id!], MESSAGE_TARGET_INJECT, MESSAGE_TO_INJECT_TOGGLE_DISPLAY).catch(console.error)
chrome.storage.sync.get(STORAGE_ENV, envDatas => {
const envDataStr = envDatas[STORAGE_ENV]
const envData = envDataStr ? JSON.parse(envDataStr) : {}
if (envData.sidePanel) {
chrome.sidePanel.setOptions({
tabId: tab.id!,
path: 'sidepanel.html?tabId=' + tab.id,
})
chrome.sidePanel.open({
tabId: tab.id!,
})
} else {
extensionMessage.broadcastMessageExact([tab.id!], MESSAGE_TARGET_INJECT, MESSAGE_TO_INJECT_TOGGLE_DISPLAY).catch(console.error)
}
})
})
initTaskService()