File size: 803 Bytes
bee6636
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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,
	});
}