Hello,
I have migrated our webextension from v2 to comply with manifest v3
and submitted a new one.
When testing the signed xpi, I realize that background script is stopped after few seconds.
We use this extension to communicate with smartcard readers connected to client PC to send commands to the inserted card.
Now the extension does not respond to any orders.
How to keep the backgroud script alive in v3 versions ?
Or should I return to v2 manifest ?
Thanks for your help.
There is no rush to migrate to MV3 in Firefox store, so reverting back to MV2 is totally OK.
MV2 in Firefox supports many of the modern MV3 API, so your codebase doesn’t have to change much (or at all).
Alternatively, you can keep your background script running by forcing it to perform some “action” every <30 seconds.
For example using this:
self.setInterval(() => browser.runtime.getPlatformInfo(), 2e4);
However, browser can still kill your background script in some special cases (when PC wakes up from sleep, or on Android I think due to OS limitations…).
But I wonder, why is your background script not woken up when it receives a message from the reader? Are you registering the message handler in the top-level background script code?
Hi juraj,
The message is sent to our extension by a web app that handles end-users smartcards life-cycle.
Here is the bk script code that use native messaging :
function connect() {
var hostName = “com.our.app.webextension”;
if (ilxPort == null) {
console.log(“MyApp WebExt Connecting to Native”);
ilxPort = browserRuntime.connectNative(hostName);
}
if (ilxPort != null) {
ilxPort.onDisconnect.addListener(onDisconnected);
// Add listener for host response
ilxPort.onMessage.addListener(dispatchResponse);
}
}
browserRuntime.onConnect.addListener( function(contentPort) {
// console.log(“browserRuntime.onConnect.addListener :” + JSON.stringify(contentPort))
// console.assert(contentPort.name == “myapp_cms”);
ctxPort = contentPort;
ctxPort.onMessage.addListener(function (request) {
// Connect to host application connect();
console.log("Background Script - Request : " + JSON.stringify(request));
sendNativeMessage(request);
});
});
UPDATE : I revert to manifest v2 with a new signed webextension version and our card manager works as expected
Revert to MV2 is probably the best and easiest option for now.
I also had to do it, and not once, maybe 3 times. But I did build a lot of addons with some unique issues.
Sadly I can’t help with the code, the native messaging is not my area of expertise…
But what I can do is help you format that code! 
You see, you need to place 3 backticks (```) on the line before and after the code, then everything between will be nicely formatted.