background.js 4.4 KB

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