My extension (which uses manifest v3) goes idle, which I understand is expected, but things go awry and I’m trying to understand exactly what, by adding lots of logs.
But, alas, when the Developer Tools are open in order to see these logs, the extension never goes idle, with this message: Background event page was not terminated on idle because a DevTools toolbox is attached to the extension.
Which means I can never reproduce the issue (or I can reproduce it but I don’t get the logs).
When you debug a non-persistent background script, the background script won’t go idle while the toolbox is open. However, if you need to terminate the background page, you can do so in about:debugging.
That sounded promising… however it doesn’t say how, and I don’t see any “Terminate” button or anything like that in about:debugging.
Well, it is a developer specific thing, maybe they didn’t wanted to expose it to all users…
In any case, if your extension is not doing anything, the background script should get terminated in exactly 30 seconds.
From my experience, most issues related to non-persistent background script was caused by a race condition - the function that woke up the background script was executed “too fast” - and the dependencies that should be loaded first were not ready.
What I’ve done in the past to assist with debugging an non-persistant background context is to create a separate “console” page in my extension, then have the background context push messages to tthe console page using the Broadcast Channel API. In the console page, you can either log received messages to the console or append them to the page’s DOM so you don’t have to open DevTools on the page to see the messages.
This approach works because neither having an extension page open in a separate tab or sending/recieving messages on a broadcast channel will affect the lifetime of the background context.
Be aware that this approach will only allow you to capture the broadcast log messages while the console page is open. Any messages sent before the page is open will be lost because there’s no listener available to hear them.