Using Web Workers in extensions

Is there something special about using Web Workers in extensions?
The official MDN page only links this obsolete “Using workers in extensions” article.
I know there is a limited test of API available for Workers, but what about WebExtension API, is some of it available?

I’ve never used workers before so if there are some gotchas I should know about, please let me know :slight_smile:.

Right now my plan is to simply start a worker from background script, send it work, wait for the response and kill it afterwards.

I have a CPU intensive task that can run for 10+ seconds and when it’s running in background script, it actually blocks my extension UI (I think I’ve read this somewhere that different contexts of the same extension are actually sharing same event loop).

I’m not aware of anything special. Your worker has the exact same reduced API scope it has in a normal web context, so no webextension APIs and only postMessage based communication.

This is based on my experience for https://github.com/freaktechnik/coupon-booklet/blob/master/worker.js

1 Like

Thanks Martin!
One more question, can you see console.log statements in the background script console window? They are there in Chrome, but in Firefox I can see them only in the Browser Console.

Based on the MDN docs it should work (or maybe it’s a bug1604679).

I do not recall how I’ve debugged the worker, it’s been quite a while and the devtools have changed quite a bit. If I inspect the extension right now, I do see its worker in the JS debugger though.

1 Like