background.js 785 B

123456789101112131415161718192021222324
  1. // background.js
  2. chrome.runtime.onInstalled.addListener(function () {
  3. console.log("插件已被安装");
  4. });
  5. let rinfo = {}
  6. chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  7. if (request.action === "show_popup") {
  8. const info = request.data;
  9. let n = chrome.windows.create({
  10. url: chrome.runtime.getURL("page.html"), // 指向你的弹出页面
  11. }, (newTab) => {
  12. rinfo[newTab.tabs[0].id] = info
  13. });
  14. }
  15. });
  16. // 监听已更新的标签,以找出何时打开了 page.html
  17. chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
  18. if (changeInfo.status === 'complete' && tab.url.includes('page.html')) {
  19. chrome.tabs.sendMessage(tabId, { msgToPopup: rinfo[tabId] + new Date().toString() });
  20. }
  21. });