{
  "id": 10068598,
  "title": "How to get access to DOM from Service Worker?",
  "url": "https://urgent.news/2026/09/26/how-to-get-access-to-dom-from-service-worker",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-09-26T21:24:00.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/jcubic/how-to-get-access-to-dom-from-service-worker-43e3"
  },
  "original_language": "en",
  "account": "To access the DOM from a Service Worker, you can use the Mitty library. First, install Mitty using npm by running `npm install @jcubic/mitty`. In the main thread of your application, import the `Host` object from Mitty and create a `modules` object containing the `document` and `window` objects. Establish a connection between the main thread and the web worker using a `BroadcastChannel`. In the web worker (or service worker), use Mitty's `connect` function to establish a connection channel. Then, require the `document` object using the `require` function from the channel. You can use this `document` object to access and manipulate the DOM. For example, you can select the `body` element using `document.querySelector('body')` and then modify its properties, such as the `color` or `backgroundColor`. The Mitty library allows you to access and control the DOM from Service Workers, providing a transport-agnostic solution for executing method chains from any isolated context, including Workers, Tabs, or Servers.",
  "summary": "When working on Fake Linux Terminal and then Hacking Cafe , I've created an abstraction of a Linux process in the form of a web worker. Those executable scripts lived in Filesystem and needed an access to DOM (in my case jQuery, becasuse the used jQuery Terminal ). In this article I will show how to give access to the DOM from a web worker (service workers will work exactly the same) using Mitty…",
  "key_points": [],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}