|
@@ -1,4 +1,3 @@
|
|
|
-import { onMessage, sendMessage } from 'webext-bridge/background'
|
|
|
import { navigator } from '@/utils/navigator.js'
|
|
|
import { plan, messageList } from './plan'
|
|
|
|
|
@@ -16,163 +15,163 @@ export default defineBackground(() => {
|
|
|
.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 === 'SET_SELECT') {
|
|
|
- chrome.tabs.query({}, (tabs) => {
|
|
|
- tabs.forEach(tab => {
|
|
|
- if (tab.id) {
|
|
|
- chrome.tabs.sendMessage(tab.id, {
|
|
|
- type: 'SET_SELECT_TO_CENTENT',
|
|
|
- data: 1
|
|
|
- }, (response) => {
|
|
|
- if (chrome.runtime.lastError) {
|
|
|
- console.error(`Tab ${tab.id} 错误: ${chrome.runtime.lastError.message}`)
|
|
|
- } else {
|
|
|
- console.log(`Tab ${tab.id} 响应:`, response)
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
- })
|
|
|
- })
|
|
|
- chrome.runtime.sendMessage({
|
|
|
- type: 'SET_SELECT_TO_CENTENT',
|
|
|
- data: 1
|
|
|
- })
|
|
|
- }
|
|
|
- if (message.type === 'GET_SETTING_INFO') {
|
|
|
- console.log(message)
|
|
|
+ // 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 === 'SET_SELECT') {
|
|
|
+ // chrome.tabs.query({}, (tabs) => {
|
|
|
+ // tabs.forEach(tab => {
|
|
|
+ // if (tab.id) {
|
|
|
+ // chrome.tabs.sendMessage(tab.id, {
|
|
|
+ // type: 'SET_SELECT_TO_CENTENT',
|
|
|
+ // data: 1
|
|
|
+ // }, (response) => {
|
|
|
+ // if (chrome.runtime.lastError) {
|
|
|
+ // console.error(`Tab ${tab.id} 错误: ${chrome.runtime.lastError.message}`)
|
|
|
+ // } else {
|
|
|
+ // console.log(`Tab ${tab.id} 响应:`, response)
|
|
|
+ // }
|
|
|
+ // })
|
|
|
+ // }
|
|
|
+ // })
|
|
|
+ // })
|
|
|
+ // chrome.runtime.sendMessage({
|
|
|
+ // type: 'SET_SELECT_TO_CENTENT',
|
|
|
+ // data: 1
|
|
|
+ // })
|
|
|
+ // }
|
|
|
+ // if (message.type === 'GET_SETTING_INFO') {
|
|
|
+ // console.log(message)
|
|
|
|
|
|
- chrome.storage.local.get('token', (result) => {
|
|
|
- sendResponse(result.token || '')
|
|
|
- })
|
|
|
- }
|
|
|
+ // chrome.storage.local.get('token', (result) => {
|
|
|
+ // sendResponse(result.token || '')
|
|
|
+ // })
|
|
|
+ // }
|
|
|
|
|
|
- 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[1].id // 获取当前活动的 tabId
|
|
|
- chrome.tabs.sendMessage(
|
|
|
- tabId,
|
|
|
- { type: 'INPUT_FORM' },
|
|
|
- (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('收到 content script 响应:', response)
|
|
|
- sendResponse(response.data)
|
|
|
- }
|
|
|
- })
|
|
|
- })
|
|
|
- return true
|
|
|
- }
|
|
|
- // 截图
|
|
|
- if (message.type === 'SCREENSHOT') {
|
|
|
- chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
|
|
- if (tabs.length === 0) return // 确保有活动标签页
|
|
|
- const tabId = tabs[0].id // 获取当前活动的 tabId
|
|
|
- chrome.tabs.sendMessage(tabId, message, (response) => {
|
|
|
- if (chrome.runtime.lastError) {
|
|
|
- sendResponse(chrome.runtime.lastError.message)
|
|
|
- } else {
|
|
|
- sendResponse(response)
|
|
|
- }
|
|
|
- })
|
|
|
- })
|
|
|
- return true
|
|
|
- }
|
|
|
- return true
|
|
|
- })
|
|
|
+ // 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[1].id // 获取当前活动的 tabId
|
|
|
+ // chrome.tabs.sendMessage(
|
|
|
+ // tabId,
|
|
|
+ // { type: 'INPUT_FORM' },
|
|
|
+ // (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('收到 content script 响应:', response)
|
|
|
+ // sendResponse(response.data)
|
|
|
+ // }
|
|
|
+ // })
|
|
|
+ // })
|
|
|
+ // return true
|
|
|
+ // }
|
|
|
+ // // 截图
|
|
|
+ // if (message.type === 'SCREENSHOT') {
|
|
|
+ // chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
|
|
+ // if (tabs.length === 0) return // 确保有活动标签页
|
|
|
+ // const tabId = tabs[0].id // 获取当前活动的 tabId
|
|
|
+ // chrome.tabs.sendMessage(tabId, message, (response) => {
|
|
|
+ // if (chrome.runtime.lastError) {
|
|
|
+ // sendResponse(chrome.runtime.lastError.message)
|
|
|
+ // } else {
|
|
|
+ // sendResponse(response)
|
|
|
+ // }
|
|
|
+ // })
|
|
|
+ // })
|
|
|
+ // return true
|
|
|
+ // }
|
|
|
+ // return true
|
|
|
+ // })
|
|
|
let timer
|
|
|
// chrome.tabs.onRemoved.addListener(function (...a) {
|
|
|
// clearTimeout(timer)
|