https://github.com/fwextensions/stdout-ext I finally broke down and threw together this extension after banging my head trying to debug the transition from a persistent background page to a manifest v3 service worker. I'd gotten it working while having the service worker devtools open the whole time. Then I tested it with devtools closed, and... there were bugs.
But how to debug the service worker if having devtools open changes its behavior? It's like trying to determine whether Schrodinger's cat is dead or alive without opening the box. The best workaround I found was to log everything with
console.error(), so the messages would show up in the extension details page without devtools being open. But it's not ideal, as that page doesn't show timestamps, doesn't show repeated log messages, and objects are logged as
[object Object].
So I finally tried sending log messages via
runtime.sendMessage() to another extension, which could then render the messages without having the source extension's devtools open. As long as the recipient extension sends a response back, the source service worker will still go inactive after 30 seconds. So you can see debugging output while keeping normal service worker behavior.
Initially I tried using the
console-feed component to render the messages in the stdout extension, but ran into issues with regenerator-runtime and its use of
Function(), which doesn't work in an extension. I belatedly realized the best console is the
actual console, so the whole stdout "extension" is just a service worker with a single
onExternalMessage() handler that redirects the incoming messages to the console.
Anyway, hope this helps other folks trying to debug the quantum weirdness of service workers. To use it, you just need to load the unpacked stdout extension into Chrome and get its ID. Then import a simple package and call
stdout() with that ID. After that, any calls to
console.log() and whatnot will be forwarded to the stdout extension, where you can view the messages in its service worker's console.