This commit is contained in:
Pagwin 2025-09-02 16:37:50 -04:00
parent a0b2d06a2c
commit d1b5aae9df

View file

@ -11,7 +11,7 @@ self.addEventListener("fetch", event =>{
return await handle_redirect(event.request); return await handle_redirect(event.request);
} }
else { else {
return await handle_html(event.request,resp); return await handle_html(event.request,resp, body);
} }
})()); })());
}) })
@ -71,17 +71,27 @@ function toggleLightDark(transaction){
}); });
} }
async function handle_html(req, resp){ async function handle_html(req, resp, body){
// TODO: read from indexDB to figure out if we fiddle with the body classes or not const db_req = self.indexedDB.open("light-dark-store");
const obj_store = transaction.objectStore("light-dark-store"); const up_promise = new Promise((res)=>{
const grab = obj_store.get(1); db_req.onupgradeneeded = (event) => {
const toggled = await (new Promise((res)=>{ const db = event.target.result;
grab.onsuccess = (event)=>{ IDB_cond_create(db, "light-dark-store", {}).then(res);
const val = event.result;
const toggled = !!val;
res(toggled);
} }
})); });
const toggled = await new Promise((res)=>{
db_req.onsuccess = (event) => {
const db = event.target.result;
const transaction = db.transaction("light-dark-store");
transaction.oncomplete = res;
transaction.objectStore("light-dark-store", "readwrite");
toggleLightDark(transaction);
const grab = obj_store.get(1);
grab.onsuccess event => res(!!event.result);
};
});
if(!toggled) return resp; if(!toggled) return resp;
return new Response(body.replace("<body>", '<body class="toggled">'), { return new Response(body.replace("<body>", '<body class="toggled">'), {