123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- 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
|