scramjet / src /client /document.ts
soiz1's picture
Upload 150 files
bee6636 verified
raw
history blame contribute delete
803 Bytes
import { rewriteUrl } from "../shared/rewriters/url";
import { ScramjetClient } from "./client";
import { getOwnPropertyDescriptorHandler } from "./helpers";
export function createDocumentProxy(
client: ScramjetClient,
self: typeof globalThis
) {
return new Proxy(self.document, {
get(target, prop) {
if (prop === "location") {
return client.locationProxy;
}
if (prop === "defaultView") {
return client.globalProxy;
}
const value = Reflect.get(target, prop);
return value;
},
set(target, prop, newValue) {
if (prop === "location") {
location.href = rewriteUrl(newValue, client.meta);
return;
}
return Reflect.set(target, prop, newValue);
},
getOwnPropertyDescriptor: getOwnPropertyDescriptorHandler,
});
}