tor-browser

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

partitioned-cookies-sw.js (1589B)


      1 self.addEventListener('message', ev => ev.waitUntil(onMessage(ev)));
      2 
      3 async function onMessage(event) {
      4  if (!event.data)
      5    return;
      6  switch (event.data.type) {
      7    case 'test_message':
      8      return onTestMessage(event);
      9    case 'echo_cookies_http':
     10      return onEchoCookiesHttp(event);
     11    case 'echo_cookies_js':
     12      return onEchoCookiesJs(event);
     13    case 'echo_cookies_import':
     14      return onEchoCookiesImport(event);
     15    default:
     16      return;
     17  }
     18 }
     19 
     20 // test_message just verifies that the message passing is working.
     21 async function onTestMessage(event) {
     22  event.source.postMessage({ok: true});
     23 }
     24 
     25 async function onEchoCookiesHttp(event) {
     26  try {
     27    const resp = await fetch(
     28        `${self.origin}/cookies/resources/list.py`, {credentials: 'include'});
     29    const cookies = await resp.json();
     30    event.source.postMessage({ok: true, cookies: Object.keys(cookies)});
     31  } catch (err) {
     32    event.source.postMessage({ok: false});
     33  }
     34 }
     35 
     36 // echo_cookies returns the names of all of the cookies available to the worker.
     37 async function onEchoCookiesJs(event) {
     38  try {
     39    const cookie_objects = await self.cookieStore.getAll();
     40    const cookies = cookie_objects.map(c => c.name);
     41    event.source.postMessage({ok: true, cookies});
     42  } catch (err) {
     43    event.source.postMessage({ok: false});
     44  }
     45 }
     46 
     47 // Sets `self._cookies` variable, array of the names of cookies available to
     48 // the request.
     49 importScripts(`${self.origin}/cookies/resources/list-cookies-for-script.py`);
     50 
     51 function onEchoCookiesImport(event) {
     52  event.source.postMessage({ok: true, cookies: self._cookies});
     53 }