Skip to content

Commit

Permalink
fix: add messageId to callbacks (#131)
Browse files Browse the repository at this point in the history
Co-authored-by: David Case <[email protected]>
  • Loading branch information
shruggr and David Case authored Nov 13, 2023
1 parent 776eb11 commit 74ca55b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
10 changes: 6 additions & 4 deletions public/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ document.addEventListener('PandaRequest', (e) => {

params.domain = window.location.hostname;

chrome.runtime.sendMessage({ action: type, params }, responseCallback);
chrome.runtime.sendMessage({ action: type, params }, buildResponseCallback(e.detail.messageId));
});

const responseCallback = (response) => {
const responseEvent = new CustomEvent('PandaResponse', { detail: response });
document.dispatchEvent(responseEvent);
const buildResponseCallback = (messageId) => {
return (response) => {
const responseEvent = new CustomEvent(messageId, { detail: response });
document.dispatchEvent(responseEvent);
};
};
7 changes: 3 additions & 4 deletions public/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ const createPandaMethod = (type) => {
return async (params) => {
return new Promise((resolve, reject) => {
// Send request
const messageId = `${type}-${Date.now()}-${Math.random()}`;
const requestEvent = new CustomEvent('PandaRequest', {
detail: { type, params },
detail: { messageId, type, params },
});
document.dispatchEvent(requestEvent);

Expand All @@ -15,12 +16,10 @@ const createPandaMethod = (type) => {
} else {
reject(e.detail.error);
}

document.removeEventListener('PandaResponse', onResponse);
}
}

document.addEventListener('PandaResponse', onResponse, { once: true });
document.addEventListener(messageId, onResponse, { once: true });
});
};
};
Expand Down

0 comments on commit 74ca55b

Please sign in to comment.