mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 22:40:01 -04:00
refactor: everything
This commit is contained in:
parent
78e666d314
commit
506d99f9b6
37 changed files with 925 additions and 885 deletions
|
@ -1,102 +1,97 @@
|
|||
import { client } from ".";
|
||||
import { encodeUrl } from "../shared/rewriters/url";
|
||||
import { locationProxy } from "./location";
|
||||
import { ScramjetClient } from "./client";
|
||||
|
||||
export const windowProxy = new Proxy(self, {
|
||||
get(target, prop) {
|
||||
const propIsString = typeof prop === "string";
|
||||
if (propIsString && prop === "location") {
|
||||
return locationProxy;
|
||||
} else if (
|
||||
propIsString &&
|
||||
["window", "top", "self", "globalThis"].includes(prop)
|
||||
) {
|
||||
return windowProxy;
|
||||
} else if (propIsString && prop == "parent") {
|
||||
return self.parent;
|
||||
} else if (propIsString && prop === "$scramjet") {
|
||||
return;
|
||||
}
|
||||
export function createWindowProxy(
|
||||
client: ScramjetClient,
|
||||
self: typeof globalThis
|
||||
): typeof globalThis {
|
||||
return new Proxy(self, {
|
||||
get(target, prop) {
|
||||
const propIsString = typeof prop === "string";
|
||||
if (propIsString && prop === "location") {
|
||||
return client.locationProxy;
|
||||
} else if (
|
||||
propIsString &&
|
||||
["window", "top", "self", "globalThis"].includes(prop)
|
||||
) {
|
||||
return client.windowProxy;
|
||||
} else if (propIsString && prop == "parent") {
|
||||
return self.parent;
|
||||
} else if (propIsString && prop === "$scramjet") {
|
||||
return;
|
||||
}
|
||||
|
||||
const value = Reflect.get(target, prop);
|
||||
const value = Reflect.get(target, prop);
|
||||
|
||||
// this is bad! i don't know what the right thing to do is
|
||||
if (typeof value === "function") {
|
||||
return new Proxy(value, {
|
||||
apply(_target, thisArg, argArray) {
|
||||
return value.apply(self, argArray);
|
||||
},
|
||||
});
|
||||
}
|
||||
// this is bad! i don't know what the right thing to do is
|
||||
if (typeof value === "function") {
|
||||
return new Proxy(value, {
|
||||
apply(_target, thisArg, argArray) {
|
||||
return value.apply(self, argArray);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return value;
|
||||
},
|
||||
|
||||
set(target, prop, newValue) {
|
||||
// ensures that no apis are overwritten
|
||||
if (
|
||||
typeof prop === "string" &&
|
||||
["window", "top", "parent", "self", "globalThis", "location"].includes(
|
||||
prop
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Reflect.set(target, prop, newValue);
|
||||
},
|
||||
defineProperty(target, property, attributes) {
|
||||
if (!attributes.get && !attributes.set) {
|
||||
attributes.writable = true;
|
||||
}
|
||||
attributes.configurable = true;
|
||||
|
||||
return Reflect.defineProperty(target, property, attributes);
|
||||
},
|
||||
});
|
||||
|
||||
export const documentProxy = new Proxy(self.document || {}, {
|
||||
get(target, prop) {
|
||||
const propIsString = typeof prop === "string";
|
||||
|
||||
if (propIsString && prop === "location") {
|
||||
return locationProxy;
|
||||
}
|
||||
|
||||
const value = Reflect.get(target, prop);
|
||||
|
||||
if (typeof value === "function") {
|
||||
return new Proxy(value, {
|
||||
apply(_target, thisArg, argArray) {
|
||||
return value.apply(self.document, argArray);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return value;
|
||||
},
|
||||
set(target, prop, newValue) {
|
||||
if (typeof prop === "string" && prop === "location") {
|
||||
//@ts-ignore
|
||||
location = new URL(encodeUrl(newValue));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
return Reflect.set(target, prop, newValue);
|
||||
},
|
||||
});
|
||||
|
||||
client.Proxy(
|
||||
[
|
||||
"Function.prototype.call",
|
||||
"Function.prototype.bind",
|
||||
"Function.prototype.apply",
|
||||
],
|
||||
{
|
||||
apply(ctx) {
|
||||
if (ctx.args[0] === windowProxy) ctx.args[0] = window;
|
||||
if (ctx.args[0] === documentProxy) ctx.args[0] = document;
|
||||
return value;
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
set(target, prop, newValue) {
|
||||
// ensures that no apis are overwritten
|
||||
if (
|
||||
typeof prop === "string" &&
|
||||
["window", "top", "parent", "self", "globalThis", "location"].includes(
|
||||
prop
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Reflect.set(target, prop, newValue);
|
||||
},
|
||||
defineProperty(target, property, attributes) {
|
||||
if (!attributes.get && !attributes.set) {
|
||||
attributes.writable = true;
|
||||
}
|
||||
attributes.configurable = true;
|
||||
|
||||
return Reflect.defineProperty(target, property, attributes);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function createDocumentProxy(
|
||||
client: ScramjetClient,
|
||||
self: typeof globalThis
|
||||
) {
|
||||
return new Proxy(self.document || {}, {
|
||||
get(target, prop) {
|
||||
const propIsString = typeof prop === "string";
|
||||
|
||||
if (propIsString && prop === "location") {
|
||||
return client.locationProxy;
|
||||
}
|
||||
|
||||
const value = Reflect.get(target, prop);
|
||||
|
||||
if (typeof value === "function") {
|
||||
return new Proxy(value, {
|
||||
apply(_target, thisArg, argArray) {
|
||||
return value.apply(self.document, argArray);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return value;
|
||||
},
|
||||
set(target, prop, newValue) {
|
||||
if (typeof prop === "string" && prop === "location") {
|
||||
//@ts-ignore
|
||||
location = new URL(encodeUrl(newValue));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
return Reflect.set(target, prop, newValue);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue