This commit is contained in:
velzie 2024-08-31 11:52:00 -04:00
parent 08ccb4f56e
commit 7adf76d37f
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
2 changed files with 48 additions and 37 deletions

View file

@ -2,15 +2,17 @@
import { ScramjetClient } from "./client"; import { ScramjetClient } from "./client";
import { nativeGetOwnPropertyDescriptor } from "./natives"; import { nativeGetOwnPropertyDescriptor } from "./natives";
import { encodeUrl, decodeUrl } from "../shared"; import { encodeUrl, decodeUrl } from "../shared";
import { iswindow } from ".";
export function createLocationProxy( export function createLocationProxy(
client: ScramjetClient, client: ScramjetClient,
self: typeof globalThis self: typeof globalThis
) { ) {
const Location = iswindow ? self.Location : self.WorkerLocation;
// location cannot be Proxy()d // location cannot be Proxy()d
const fakeLocation = {}; const fakeLocation = {};
Object.setPrototypeOf(fakeLocation, self.Location.prototype); Object.setPrototypeOf(fakeLocation, Location.prototype);
fakeLocation.constructor = self.Location; fakeLocation.constructor = Location;
const urlprops = [ const urlprops = [
"protocol", "protocol",
@ -25,15 +27,19 @@ export function createLocationProxy(
]; ];
for (const prop of urlprops) { for (const prop of urlprops) {
const native = nativeGetOwnPropertyDescriptor(self.location, prop); const native = nativeGetOwnPropertyDescriptor(self.location, prop);
if (!native) continue;
const desc = { const desc = {
configurable: true, configurable: true,
enumerable: true, enumerable: true,
get: new Proxy(native.get, { };
if (native.get) {
desc.get = new Proxy(native.get, {
apply() { apply() {
return client.url[prop]; return client.url[prop];
}, },
}), });
}; }
if (native.set) { if (native.set) {
desc.set = new Proxy(native.set, { desc.set = new Proxy(native.set, {
apply(target, thisArg, args) { apply(target, thisArg, args) {
@ -52,22 +58,27 @@ export function createLocationProxy(
return client.url.href; return client.url.href;
}, },
}); });
if (self.location.valueOf)
fakeLocation.valueOf = new Proxy(self.location.valueOf, { fakeLocation.valueOf = new Proxy(self.location.valueOf, {
apply() { apply() {
return client.url.href; return client.url.href;
}, },
}); });
if (self.location.assign)
fakeLocation.assign = new Proxy(self.location.assign, { fakeLocation.assign = new Proxy(self.location.assign, {
apply(target, thisArg, args) { apply(target, thisArg, args) {
args[0] = encodeUrl(args[0]); args[0] = encodeUrl(args[0]);
Reflect.apply(target, thisArg, args); Reflect.apply(target, thisArg, args);
}, },
}); });
if (self.location.reload)
fakeLocation.reload = new Proxy(self.location.reload, { fakeLocation.reload = new Proxy(self.location.reload, {
apply(target, thisArg, args) { apply(target, thisArg, args) {
Reflect.apply(target, thisArg, args); Reflect.apply(target, thisArg, args);
}, },
}); });
if (self.location.replace)
fakeLocation.replace = new Proxy(self.location.replace, { fakeLocation.replace = new Proxy(self.location.replace, {
apply(target, thisArg, args) { apply(target, thisArg, args) {
args[0] = encodeUrl(args[0]); args[0] = encodeUrl(args[0]);

View file

@ -103,14 +103,14 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
return t; return t;
}; };
client.Proxy("Promise.prototype.catch", { // client.Proxy("Promise.prototype.catch", {
apply(ctx) { // apply(ctx) {
ctx.args[0] = new Proxy(ctx.args[0], { // ctx.args[0] = new Proxy(ctx.args[0], {
apply(target, thisArg, argArray) { // apply(target, thisArg, argArray) {
// console.warn("CAUGHT PROMISE REJECTION", argArray); // // console.warn("CAUGHT PROMISE REJECTION", argArray);
Reflect.apply(target, thisArg, argArray); // Reflect.apply(target, thisArg, argArray);
}, // },
}); // });
}, // },
}); // });
} }