Skip to content

Commit

Permalink
feat: proxy panel events to a content port
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 committed Aug 24, 2021
1 parent fcdb9d7 commit b727043
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/swr-devtools-extensions/src/background.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// background.js
let panelPort: chrome.runtime.Port | null = null;
let contentPort: chrome.runtime.Port | null = null;

const queuedMessages: any[] = [];
const flushQueuedMessages = () => {
Expand All @@ -18,7 +19,11 @@ const enqueueMessage = (message: any) => {
chrome.runtime.onConnect.addListener((port) => {
// A port between a content page
if (port.name === "content") {
port.onMessage.addListener((message) => {
contentPort = port;
contentPort.onDisconnect.addListener(() => {
contentPort = null;
});
contentPort.onMessage.addListener((message) => {
console.log("sent message from content to panel", message);
if (panelPort === null) {
enqueueMessage(message);
Expand All @@ -34,5 +39,11 @@ chrome.runtime.onConnect.addListener((port) => {
panelPort.onDisconnect.addListener(() => {
panelPort = null;
});
panelPort.onMessage.addListener((message) => {
console.log("sent message from panel to content", message);
if (contentPort !== null) {
contentPort.postMessage(message);
}
});
}
});

0 comments on commit b727043

Please sign in to comment.