File size: 2,622 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { ScramjetClient } from "../client";

export default function (client: ScramjetClient, self: typeof window) {
	const handler: ProxyHandler<Storage> = {
		get(target, prop) {
			switch (prop) {
				case "getItem":
					return (key: string) => {
						return target.getItem(client.url.host + "@" + key);
					};

				case "setItem":
					return (key: string, value: string) => {
						return target.setItem(client.url.host + "@" + key, value);
					};

				case "removeItem":
					return (key: string) => {
						return target.removeItem(client.url.host + "@" + key);
					};

				case "clear":
					return () => {
						for (const key in Object.keys(target)) {
							if (key.startsWith(client.url.host)) {
								target.removeItem(key);
							}
						}
					};

				case "key":
					return (index: number) => {
						const keys = Object.keys(target).filter((key) =>
							key.startsWith(client.url.host)
						);

						return target.getItem(keys[index]);
					};

				case "length":
					return Object.keys(target).filter((key) =>
						key.startsWith(client.url.host)
					).length;

				default:
					if (prop in Object.prototype || typeof prop === "symbol") {
						return Reflect.get(target, prop);
					}
					console.log("GET", prop, target == realLocalStorage);

					return target.getItem(client.url.host + "@" + (prop as string));
			}
		},

		set(target, prop, value) {
			if (target == realLocalStorage)
				console.log("SET", prop, value, target === realLocalStorage);
			target.setItem(client.url.host + "@" + (prop as string), value);

			return true;
		},

		ownKeys(target) {
			return Reflect.ownKeys(target)
				.filter((f) => typeof f === "string" && f.startsWith(client.url.host))
				.map((f) =>
					typeof f === "string" ? f.substring(client.url.host.length + 1) : f
				);
		},

		getOwnPropertyDescriptor(target, property) {
			return {
				value: target.getItem(client.url.host + "@" + (property as string)),
				enumerable: true,
				configurable: true,
				writable: true,
			};
		},

		defineProperty(target, property, attributes) {
			target.setItem(
				client.url.host + "@" + (property as string),
				attributes.value
			);

			return true;
		},
	};

	const realLocalStorage = self.localStorage;

	const localStorageProxy = new Proxy(self.localStorage, handler);
	const sessionStorageProxy = new Proxy(self.sessionStorage, handler);

	delete self.localStorage;
	delete self.sessionStorage;

	self.localStorage = localStorageProxy;
	self.sessionStorage = sessionStorageProxy;
}