Skip to content

Commit

Permalink
Refinement
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Feb 4, 2024
1 parent 6234c2f commit 6e8bab7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/contentScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const handleMessage = (message: PiralDebugApiMessage) => {
}
}
};

/**
* Disconnects the Piral Inspector.
*/
Expand All @@ -37,7 +38,7 @@ window.addEventListener('message', (event) => {
/**
* Receives messages from the service worker.js
*/
runtime.onMessage.addListener((content, sender) => {
runtime.onMessage.addListener((content) => {
const message: PiralInspectorMessage = {
content,
source: 'piral-inspector',
Expand Down
24 changes: 12 additions & 12 deletions src/serviceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { setIconAndPopup } from './icons';
const tabPorts: Record<number, Runtime.Port> = {};

/**
* Core message routing
* answers messages to contentscripts and dev tools
* From contentScript.js to background.js (and maybe to devtools.js)
* s -------------------> o -------------------> t
*/
runtime.onMessage.addListener(async function (message, sender) {
runtime.onMessage.addListener((message, sender) => {
const tabId = sender.tab?.id;

if (!tabId) return;
Expand All @@ -30,18 +30,19 @@ runtime.onMessage.addListener(async function (message, sender) {
});

/**
* handles the connection to the devtools and initializes event handlers
* sends messages from devtools to the content scripts
* From devtools.js to background.js (and maybe to contentScript.js)
* s -------------------> o -------------------> t
*/
runtime.onConnect.addListener(function (devToolsConnection) {
if (devToolsConnection.name !== 'piral-inspector-host') {
runtime.onConnect.addListener((port) => {
if (port.name !== 'piral-inspector-host') {
return;
}

let tabId: number;

const handler = async (ev: any) => {
const message = ev.message;

if (!message) {
return;
}
Expand All @@ -50,8 +51,7 @@ runtime.onConnect.addListener(function (devToolsConnection) {
// handle initialize message to connect contentScript.js with devtools.js
if (tabId === undefined) {
tabId = ev.tabId;

tabPorts[tabId] = devToolsConnection;
tabPorts[tabId] = port;
}
}

Expand All @@ -61,10 +61,10 @@ runtime.onConnect.addListener(function (devToolsConnection) {
}
};

devToolsConnection.onMessage.addListener(handler);
port.onMessage.addListener(handler);

devToolsConnection.onDisconnect.addListener(() => {
devToolsConnection.onMessage.removeListener(handler);
port.onDisconnect.addListener(() => {
port.onMessage.removeListener(handler);
delete tabPorts[tabId];
});
});

0 comments on commit 6e8bab7

Please sign in to comment.