Bug when using another `chrome.runtime.onMessage` listener
ajubin opened this issue · comments
When calling registerMessageHandler
within the background script, it adds a new listener with chrome.runtime.onMessage.addListener
.
But as there is already a listener (call it B) to handle other types of operation in my code, this causes a bug. The other listener (B) is sending it's response first to contentScript.
In my case, it returns nothing, so I've got the error "Cannot read properties of null (reading 'error')
Moreover, the library uses chrome.runtime
api which is callback oriented. In my project, I'm using webextension-polyfill
which offers a broader support for other browsers which is not the case of the library and seems to be incompatible
I can't patch-pacakge it neither as the code is minified.
My temporary solution is to fork and modify the code to add the messageHandler
in my own listener (B)
I'd be happy to help find a solution that can work for everybody though
Hello @ajubin,
could you provide a minimal reproduction repository which reproduces your issue please ?
If I can reproduce it I think I could extract the logic which handles the message to a function which I could export so you could use it inside your own chrome.runtime.onMessage.addListener
handler.
Something like this:
import { handleAxiosRequestMessage } from 'axios-chrome-messaging-adapter';
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (handleAxiosRequestMessage(message, sender, sendResponse)) {
return true
}
// the rest of your logic here...
});
About the webextension-polyfill
API support, this is not a package I have ever used. But I could consider having a look at it in the future.
It's not something planned right now.