From 7adf76d37f16f1ed0d0d8989fe21206de55e9ac8 Mon Sep 17 00:00:00 2001 From: velzie Date: Sat, 31 Aug 2024 11:52:00 -0400 Subject: [PATCH] fix ggf --- src/client/location.ts | 65 +++++++++++++++++++++++---------------- src/client/shared/wrap.ts | 20 ++++++------ 2 files changed, 48 insertions(+), 37 deletions(-) diff --git a/src/client/location.ts b/src/client/location.ts index 5457cd0..c8dd31d 100644 --- a/src/client/location.ts +++ b/src/client/location.ts @@ -2,15 +2,17 @@ import { ScramjetClient } from "./client"; import { nativeGetOwnPropertyDescriptor } from "./natives"; import { encodeUrl, decodeUrl } from "../shared"; +import { iswindow } from "."; export function createLocationProxy( client: ScramjetClient, self: typeof globalThis ) { + const Location = iswindow ? self.Location : self.WorkerLocation; // location cannot be Proxy()d const fakeLocation = {}; - Object.setPrototypeOf(fakeLocation, self.Location.prototype); - fakeLocation.constructor = self.Location; + Object.setPrototypeOf(fakeLocation, Location.prototype); + fakeLocation.constructor = Location; const urlprops = [ "protocol", @@ -25,15 +27,19 @@ export function createLocationProxy( ]; for (const prop of urlprops) { const native = nativeGetOwnPropertyDescriptor(self.location, prop); + if (!native) continue; + const desc = { configurable: true, enumerable: true, - get: new Proxy(native.get, { + }; + if (native.get) { + desc.get = new Proxy(native.get, { apply() { return client.url[prop]; }, - }), - }; + }); + } if (native.set) { desc.set = new Proxy(native.set, { apply(target, thisArg, args) { @@ -52,28 +58,33 @@ export function createLocationProxy( return client.url.href; }, }); - 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); - }, - }); + + if (self.location.valueOf) + fakeLocation.valueOf = new Proxy(self.location.valueOf, { + apply() { + return client.url.href; + }, + }); + if (self.location.assign) + fakeLocation.assign = new Proxy(self.location.assign, { + apply(target, thisArg, args) { + args[0] = encodeUrl(args[0]); + Reflect.apply(target, thisArg, args); + }, + }); + if (self.location.reload) + fakeLocation.reload = new Proxy(self.location.reload, { + apply(target, thisArg, args) { + Reflect.apply(target, thisArg, args); + }, + }); + if (self.location.replace) + fakeLocation.replace = new Proxy(self.location.replace, { + apply(target, thisArg, args) { + args[0] = encodeUrl(args[0]); + Reflect.apply(target, thisArg, args); + }, + }); return fakeLocation; } diff --git a/src/client/shared/wrap.ts b/src/client/shared/wrap.ts index 897ad30..8028228 100644 --- a/src/client/shared/wrap.ts +++ b/src/client/shared/wrap.ts @@ -103,14 +103,14 @@ export default function (client: ScramjetClient, self: typeof globalThis) { return t; }; - client.Proxy("Promise.prototype.catch", { - apply(ctx) { - ctx.args[0] = new Proxy(ctx.args[0], { - apply(target, thisArg, argArray) { - // console.warn("CAUGHT PROMISE REJECTION", argArray); - Reflect.apply(target, thisArg, argArray); - }, - }); - }, - }); + // client.Proxy("Promise.prototype.catch", { + // apply(ctx) { + // ctx.args[0] = new Proxy(ctx.args[0], { + // apply(target, thisArg, argArray) { + // // console.warn("CAUGHT PROMISE REJECTION", argArray); + // Reflect.apply(target, thisArg, argArray); + // }, + // }); + // }, + // }); }