cookie communication

This commit is contained in:
velzie 2024-08-24 17:54:28 -04:00
parent bac00de12b
commit c1aecdf3a8
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
3 changed files with 32 additions and 7 deletions

View file

@ -1,12 +1,28 @@
import { ScramjetClient } from "../client";
export default function (client: ScramjetClient, self: typeof window) {
client.serviceworker.addEventListener("message", ({ data }) => {
if (!("scramjet$type" in data)) return;
if (data.scramjet$type === "cookie") {
this.cookieStore.setCookies([data.cookie], new URL(data.url));
return;
}
});
client.Trap("Document.prototype.cookie", {
get() {
return client.cookieStore.getCookies(client.url, true);
},
set(ctx, value: string) {
client.cookieStore.setCookies([value], client.url);
// TODO hardcode because scoping whatever
client.serviceworker.controller!.postMessage({
scramjet$type: "cookie",
cookie: value,
url: client.url.href,
});
},
});