background.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. type: "popup", // 设置为弹出窗口
  12. }, (newTab) => {
  13. rinfo[newTab.tabs[0].id] = info
  14. });
  15. }
  16. });
  17. // 监听已更新的标签,以找出何时打开了 page.html
  18. chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
  19. if (changeInfo.status === 'complete' && tab.url.includes('page.html')) {
  20. chrome.tabs.sendMessage(tabId, { msgToPopup: rinfo[tabId] + new Date().toString() });
  21. }
  22. });
  23. // // background.js
  24. // chrome.runtime.onInstalled.addListener(function () {
  25. // console.log("插件已被安装");
  26. // });
  27. // let jsRequests = [];
  28. // let jsRequestsByTab = {}; // 用于存储每个标签页的请求
  29. // chrome.webRequest.onBeforeRequest.addListener(
  30. // (details) => {
  31. // const tabId = details.tabId; // 获取当前请求的标签页 ID
  32. // const url = details.url;
  33. // let uri = new URL(url);
  34. // if (
  35. // details.initiator &&
  36. // uri.pathname.endsWith(".js") &&
  37. // (uri.protocol.startsWith("http") || uri.protocol.startsWith("https"))
  38. // ) {
  39. // // 确保有个数组来记录当前标签页的请求
  40. // if (!jsRequestsByTab[tabId]) {
  41. // jsRequestsByTab[tabId] = [];
  42. // }
  43. // jsRequestsByTab[tabId].push(url);
  44. // }
  45. // },
  46. // { urls: ["<all_urls>"] }
  47. // );
  48. // chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  49. // if (request.action === "getJsRequests") {
  50. // const tabId = sender.tab.id; // 获取请求来源的标签页 ID
  51. // sendResponse(jsRequestsByTab[tabId] || []);
  52. // }
  53. // });
  54. // // 监听标签页更新(包括刷新)事件以清空请求记录
  55. // chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
  56. // if (changeInfo.status === "loading") {
  57. // // 标签页加载完成时清空请求记录
  58. // delete jsRequestsByTab[tabId];
  59. // }
  60. // });
  61. // // 监听标签页关闭事件以清空请求记录
  62. // chrome.tabs.onRemoved.addListener((tabId) => {
  63. // delete jsRequestsByTab[tabId]; // 清空对应标签页的请求记录
  64. // });