Here's a function for detecting whether or not a script is running in the browser's main thread or in a web worker context:
function isInWorkerContext() {
return (
typeof WorkerGlobalScope !== "undefined" &&
self instanceof WorkerGlobalScope
)
}
Source: [link]