export default defineBackground(() => { let currentTabId let arr = [] let token = '' let userId = '' const url = import.meta.env.VITE_APP_BASE_API + '/behavior/user/adds' // 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 === 'CLICK_EVENT') { if (token) { arr.push({ ...message.data, userId:token }) if (arr.length > 10) { fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token }, body: JSON.stringify(arr.map(_ => ({ ..._, userId }))) }).then(res => 1) arr = [] } } } 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') { chrome.runtime.sendMessage({ type: 'TO_SIDE_PANEL_FORM_INFO', data: message.data }) } if (message.type === 'FROM_SIDE_PANEL_TO_ACTION') { chrome.tabs.query({ active: true }, (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 } return true }) let timer // chrome.tabs.onRemoved.addListener(function (...a) { // clearTimeout(timer) // timer = setTimeout(() => { // chrome.tabs.query({}, function (tabs) { // fetch(url, { // method: 'POST', // headers: { // 'Content-Type': 'application/json', // }, // body: JSON.stringify(arr) // }).then(res => 1) // arr = [] // }); // }, 0) // }); chrome.storage.onChanged.addListener((changes, areaName) => { console.log('[Storage Changed] Area:', areaName); // 遍历所有被修改的键 for (const [key, { oldValue, newValue }] of Object.entries(changes)) { console.log(key); if (key === 'token') { token = newValue if (!newValue) { chrome.runtime.sendMessage({ type: 'USER_LOGOUT', }, function (response) { }); break } } if (key === 'userInfo') { if (!newValue) { chrome.runtime.sendMessage({ type: 'USER_LOGOUT', }, function (response) { }); break } userId = JSON.parse(newValue).id } } }); 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