diff --git a/privacy-protections/storage-blocking/main.js b/privacy-protections/storage-blocking/main.js index 17ba89e..c033c0a 100644 --- a/privacy-protections/storage-blocking/main.js +++ b/privacy-protections/storage-blocking/main.js @@ -1,4 +1,4 @@ -/* globals commonTests,THIRD_PARTY_ORIGIN,THIRD_PARTY_TRACKER_ORIGIN,THIRD_PARTY_AD_ORIGIN */ +/* globals commonTests,THIRD_PARTY_ORIGIN,THIRD_PARTY_TRACKER_ORIGIN,THIRD_PARTY_AD_ORIGIN,cookieStore */ const storeButton = document.querySelector('#store'); const retriveButton = document.querySelector('#retrive'); @@ -146,6 +146,24 @@ const tests = [ throw new Error(`Invalid response (${r.status})`); }); } + }, + { + id: 'service worker cookieStore', + store: async (data) => { + await navigator.serviceWorker.register(`./service-worker.js?data=${data}`, { scope: './' }); + const resp = await fetch(`./service-worker-set-cookie?name=swcookiestoredata&data=${data}`); + return await resp.text(); + }, + retrive: async () => { + return (await cookieStore.get('swcookiestoredata')).value; + }, + extra: () => { + if (window.cookieStore) { + return cookieStore.get('swcookiestoredata').then(cookie => { + return 'expires in ' + ((cookie.expires - Date.now()) / (1000 * 60 * 60 * 24)).toFixed(2) + ' days'; + }); + } + } } ]; diff --git a/privacy-protections/storage-blocking/service-worker.js b/privacy-protections/storage-blocking/service-worker.js index b80e1f7..5adc206 100644 --- a/privacy-protections/storage-blocking/service-worker.js +++ b/privacy-protections/storage-blocking/service-worker.js @@ -1,4 +1,5 @@ /* eslint-env serviceworker */ +/* global cookieStore */ self.addEventListener('install', (evt) => { self.skipWaiting(); @@ -12,4 +13,15 @@ self.addEventListener('fetch', (event) => { if (event.request.url.includes('/service-worker-data')) { event.respondWith(new Response(location.search.replace('?data=', ''))); } + if (event.request.url.includes('/service-worker-set-cookie')) { + const url = new URL(event.request.url); + if (globalThis.cookieStore) { + cookieStore.set({ + name: url.searchParams.get('name'), + value: url.searchParams.get('data'), + expires: new Date('Wed, 21 Aug 2030 20:00:00 UTC').getTime() + }); + } + event.respondWith(new Response('OK')); + } });