background.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. export default defineBackground(() => {
  2. let currentTabId
  3. // Executed when background is loaded
  4. chrome.sidePanel
  5. .setPanelBehavior({ openPanelOnActionClick: true })
  6. .catch((error) => console.error(error))
  7. chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  8. if (message.type === 'PAGE_INFO') {
  9. // 转发到侧边栏
  10. chrome.runtime.sendMessage({
  11. type: 'TO_SIDE_PANEL_PAGE_INFO', data: message.data
  12. })
  13. }
  14. if (message.type === 'FROM_CONTENT_TO_SEND_PAGE_FORM') {
  15. console.log(message)
  16. chrome.runtime.sendMessage({
  17. type: 'TO_SIDE_PANEL_FORM_INFO', data: message.data
  18. })
  19. return true
  20. }
  21. if (message.type === 'FROM_SIDE_PANEL_TO_ACTION') {
  22. console.log(565888)
  23. chrome.tabs.query({ active: true }, (tabs) => {
  24. console.log(tabs)
  25. if (tabs.length === 0) return // 确保有活动标签页
  26. const tabId = tabs[0].id // 获取当前活动的 tabId
  27. chrome.tabs.sendMessage(tabId, { type: 'GET_TAG_ACTION', data: message.data }, (response) => {
  28. if (chrome.runtime.lastError) {
  29. console.error('消息发送失败:', chrome.runtime.lastError.message)
  30. } else {
  31. console.log('收到 content script 响应:', response)
  32. console.log(response, 998)
  33. sendResponse(response)
  34. }
  35. })
  36. })
  37. return true
  38. }
  39. if (message.type === 'FROM_SIDE_PANEL_TO_GET_PAGE_FORM') {
  40. chrome.tabs.query({ active: true }, (tabs) => {
  41. if (tabs.length === 0) return // 确保有活动标签页
  42. const tabId = tabs[0].id // 获取当前活动的 tabId
  43. chrome.tabs.sendMessage(tabId, { type: 'GET_PAGE_FORM', data: 'Hello from background!' }, (response) => {
  44. if (chrome.runtime.lastError) {
  45. console.error('消息发送失败:', chrome.runtime.lastError.message)
  46. } else {
  47. console.log('收到 content script 响应:', response)
  48. sendResponse(response)
  49. }
  50. return true
  51. })
  52. })
  53. return true
  54. }
  55. if (message.type === 'FROM_SIDE_PANEL_TO_INPUT_FORM') {
  56. chrome.tabs.query({ active: true }, (tabs) => {
  57. if (tabs.length === 0) return // 确保有活动标签页
  58. const tabId = tabs[0].id // 获取当前活动的 tabId
  59. chrome.tabs.sendMessage(tabId, { type: 'INPUT_FORM', data: message.data }, (response) => {
  60. if (chrome.runtime.lastError) {
  61. console.error('消息发送失败:', chrome.runtime.lastError.message)
  62. } else {
  63. console.log('收到 content script 响应:', response)
  64. sendResponse(response.data)
  65. }
  66. })
  67. })
  68. return true
  69. }
  70. if (message.type === 'FROM_SIDE_PANEL_TO_GET_PAGE_INFO') {
  71. chrome.tabs.query({ active: true }, (tabs) => {
  72. if (tabs.length === 0) return // 确保有活动标签页
  73. const tabId = tabs[0].id // 获取当前活动的 tabId
  74. chrome.tabs.sendMessage(tabId, { type: 'GET_PAGE_INFO', data: 'Hello from background!' }, (response) => {
  75. if (chrome.runtime.lastError) {
  76. console.error('消息发送失败:', chrome.runtime.lastError.message)
  77. } else {
  78. console.log(response, 777)
  79. console.log('收到 content script 响应:', response)
  80. sendResponse(response.data)
  81. }
  82. })
  83. })
  84. return true
  85. }
  86. // 截图
  87. if (message.type === 'SCREENSHOT') {
  88. chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
  89. if (tabs.length === 0) return // 确保有活动标签页
  90. const tabId = tabs[0].id // 获取当前活动的 tabId
  91. chrome.tabs.sendMessage(tabId, message, (response) => {
  92. if (chrome.runtime.lastError) {
  93. sendResponse(chrome.runtime.lastError.message)
  94. } else {
  95. sendResponse(response)
  96. }
  97. })
  98. })
  99. return true
  100. }
  101. })
  102. chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
  103. if (changeInfo.status === 'complete' && tab.url) {
  104. chrome.runtime.sendMessage({
  105. type: 'TO_SIDE_PANEL_PAGE_CHANGE', data: tab
  106. })
  107. currentTabId = tab.id
  108. }
  109. })
  110. chrome.tabs.onActivated.addListener((activeInfo) => {
  111. chrome.tabs.get(activeInfo.tabId, (tab) => {
  112. if (chrome.runtime.lastError) {
  113. console.error(chrome.runtime.lastError)
  114. return
  115. }
  116. chrome.runtime.sendMessage({
  117. type: 'TO_SIDE_PANEL_PAGE_CHANGE', data: tab
  118. })
  119. console.log('用户切换到新 Tab,URL:', tab)
  120. currentTabId = tab.id
  121. })
  122. })
  123. })
  124. // Allows users to open the side panel by clicking on the action toolbar icon