tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

file_block_script_wrong_mime_sw.js (1577B)


      1 /**
      2 * Service Worker that runs in 2 modes: 1) direct pass-through via
      3 * fetch(event.request) and 2) indirect pass-through via
      4 * fetch(event.request.url).
      5 *
      6 * Because this is updating a pre-existing mochitest that didn't use a SW and
      7 * used a single test document, we use a SW idiom where the SW claims the
      8 * existing window client.  And because we operate in two modes and we
      9 * parameterize via URL, we also ensure that we skipWaiting.
     10 */
     11 
     12 /* eslint-env serviceworker */
     13 
     14 // We are parameterized by "mode".
     15 const params = new URLSearchParams(location.search);
     16 const fetchMode = params.get("fetchMode");
     17 
     18 // When activating on initial install, claim the existing window client.
     19 // For synchronziation, also message the controlled document to report our mode.
     20 self.addEventListener("activate", event => {
     21  event.waitUntil(
     22    (async () => {
     23      await clients.claim();
     24      const allClients = await clients.matchAll();
     25      for (const client of allClients) {
     26        client.postMessage({
     27          fetchMode,
     28        });
     29      }
     30    })()
     31  );
     32 });
     33 
     34 // When updating the SW to change our mode of operation, skipWaiting so we
     35 // advance directly to activating without waiting for the test window client
     36 // to stop being controlled by our previous configuration.
     37 self.addEventListener("install", () => {
     38  self.skipWaiting();
     39 });
     40 
     41 self.addEventListener("fetch", event => {
     42  switch (fetchMode) {
     43    case "direct":
     44      event.respondWith(fetch(event.request));
     45      break;
     46 
     47    case "indirect":
     48      event.respondWith(fetch(event.request.url));
     49      break;
     50  }
     51 });