mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 23:10:02 -04:00
rewrite location proxy (regressions!)
This commit is contained in:
parent
2bc31d7e56
commit
7f8956979a
5 changed files with 132 additions and 95 deletions
|
@ -1,44 +1,78 @@
|
|||
// @ts-nocheck
|
||||
import { ScramjetClient } from "./client";
|
||||
import { nativeGetOwnPropertyDescriptor } from "./natives";
|
||||
import { encodeUrl, decodeUrl } from "./shared";
|
||||
|
||||
export function createLocationProxy(
|
||||
client: ScramjetClient,
|
||||
self: typeof globalThis
|
||||
) {
|
||||
function createLocation() {
|
||||
const loc = new URL(client.url.href);
|
||||
// location cannot be Proxy()d
|
||||
const fakeLocation = {};
|
||||
Object.setPrototypeOf(fakeLocation, self.Location.prototype);
|
||||
fakeLocation.constructor = self.Location;
|
||||
|
||||
loc.assign = (url: string) => self.location.assign(encodeUrl(url));
|
||||
loc.reload = () => self.location.reload();
|
||||
loc.replace = (url: string) => self.location.replace(encodeUrl(url));
|
||||
loc.toString = () => loc.href;
|
||||
|
||||
return loc;
|
||||
const urlprops = [
|
||||
"hash",
|
||||
"host",
|
||||
"hostname",
|
||||
"href",
|
||||
"origin",
|
||||
"pathname",
|
||||
"port",
|
||||
"search",
|
||||
];
|
||||
for (const prop of urlprops) {
|
||||
const native = nativeGetOwnPropertyDescriptor(self.location, prop);
|
||||
const desc = {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
get: new Proxy(native.get, {
|
||||
apply() {
|
||||
return client.url[prop];
|
||||
},
|
||||
}),
|
||||
};
|
||||
if (native.set) {
|
||||
desc.set = new Proxy(native.set, {
|
||||
apply(target, thisArg, args) {
|
||||
let url = new URL(client.url.href);
|
||||
url[prop] = args[0];
|
||||
self.location.href = encodeUrl(url.href);
|
||||
},
|
||||
});
|
||||
}
|
||||
Object.defineProperty(fakeLocation, prop, desc);
|
||||
}
|
||||
|
||||
return new Proxy(
|
||||
{
|
||||
host: "",
|
||||
// functions
|
||||
fakeLocation.toString = new Proxy(self.location.toString, {
|
||||
apply() {
|
||||
return client.url.href;
|
||||
},
|
||||
{
|
||||
get(target, prop) {
|
||||
const loc = createLocation();
|
||||
});
|
||||
fakeLocation.valueOf = new Proxy(self.location.valueOf, {
|
||||
apply() {
|
||||
return client.url.href;
|
||||
},
|
||||
});
|
||||
fakeLocation.assign = new Proxy(self.location.assign, {
|
||||
apply(target, thisArg, args) {
|
||||
args[0] = encodeUrl(args[0]);
|
||||
Reflect.apply(target, thisArg, args);
|
||||
},
|
||||
});
|
||||
fakeLocation.reload = new Proxy(self.location.reload, {
|
||||
apply(target, thisArg, args) {
|
||||
Reflect.apply(target, thisArg, args);
|
||||
},
|
||||
});
|
||||
fakeLocation.replace = new Proxy(self.location.replace, {
|
||||
apply(target, thisArg, args) {
|
||||
args[0] = encodeUrl(args[0]);
|
||||
Reflect.apply(target, thisArg, args);
|
||||
},
|
||||
});
|
||||
|
||||
return loc[prop];
|
||||
},
|
||||
|
||||
set(obj, prop, value) {
|
||||
const loc = createLocation();
|
||||
|
||||
if (prop === "href") {
|
||||
self.location.href = encodeUrl(value);
|
||||
} else {
|
||||
loc[prop] = value;
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
}
|
||||
);
|
||||
return fakeLocation;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue