export default defineBackground(() => { let currentTabId // Executed when background is loaded chrome.sidePanel .setPanelBehavior({ openPanelOnActionClick: true }) .catch((error) => console.error(error)) chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { if (message.type === 'PAGE_INFO') { // 转发到侧边栏 chrome.runtime.sendMessage({ type: 'TO_SIDE_PANEL_PAGE_INFO', data: message.data }) } if (message.type === 'FROM_CONTENT_TO_SEND_PAGE_FORM') { console.log(message) chrome.runtime.sendMessage({ type: 'TO_SIDE_PANEL_FORM_INFO', data: message.data }) return true } if (message.type === 'FROM_SIDE_PANEL_TO_ACTION') { console.log(565888) chrome.tabs.query({ active: true }, (tabs) => { console.log(tabs) if (tabs.length === 0) return // 确保有活动标签页 const tabId = tabs[0].id // 获取当前活动的 tabId chrome.tabs.sendMessage( tabId, { type: 'GET_TAG_ACTION', data: message.data }, (response) => { if (chrome.runtime.lastError) { console.error('消息发送失败:', chrome.runtime.lastError.message) } else { console.log('收到 content script 响应:', response) console.log(response, 998) sendResponse(response) } } ) }) return true } if (message.type === 'FROM_SIDE_PANEL_TO_GET_PAGE_FORM') { chrome.tabs.query({ active: true }, (tabs) => { if (tabs.length === 0) return // 确保有活动标签页 const tabId = tabs[0].id // 获取当前活动的 tabId chrome.tabs.sendMessage( tabId, { type: 'GET_PAGE_FORM', data: 'Hello from background!' }, (response) => { if (chrome.runtime.lastError) { console.error('消息发送失败:', chrome.runtime.lastError.message) } else { console.log('收到 content script 响应:', response) sendResponse(response) } return true } ) }) return true } if (message.type === 'FROM_SIDE_PANEL_TO_INPUT_FORM') { chrome.tabs.query({ active: true }, (tabs) => { if (tabs.length === 0) return // 确保有活动标签页 const tabId = tabs[0].id // 获取当前活动的 tabId chrome.tabs.sendMessage( tabId, { type: 'INPUT_FORM', data: message.data }, (response) => { if (chrome.runtime.lastError) { console.error('消息发送失败:', chrome.runtime.lastError.message) } else { console.log('收到 content script 响应:', response) sendResponse(response.data) } } ) }) return true } if (message.type === 'FROM_SIDE_PANEL_TO_GET_PAGE_INFO') { chrome.tabs.query({ active: true }, (tabs) => { if (tabs.length === 0) return // 确保有活动标签页 const tabId = tabs[0].id // 获取当前活动的 tabId chrome.tabs.sendMessage( tabId, { type: 'GET_PAGE_INFO', data: 'Hello from background!' }, (response) => { if (chrome.runtime.lastError) { console.error('消息发送失败:', chrome.runtime.lastError.message) } else { console.log(response, 777) console.log('收到 content script 响应:', response) sendResponse(response.data) } } ) }) return true } }) chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { if (changeInfo.status === 'complete' && tab.url) { chrome.runtime.sendMessage({ type: 'TO_SIDE_PANEL_PAGE_CHANGE', data: tab }) currentTabId = tab.id } }) chrome.tabs.onActivated.addListener((activeInfo) => { chrome.tabs.get(activeInfo.tabId, (tab) => { if (chrome.runtime.lastError) { console.error(chrome.runtime.lastError) return } chrome.runtime.sendMessage({ type: 'TO_SIDE_PANEL_PAGE_CHANGE', data: tab }) console.log('用户切换到新 Tab,URL:', tab) currentTabId = tab.id }) }) }) // Allows users to open the side panel by clicking on the action toolbar icon