From b98d57a5bfaee8309c1c541c482b2e6d18d47e65 Mon Sep 17 00:00:00 2001 From: Percs <83934299+Percslol@users.noreply.github.com> Date: Wed, 23 Oct 2024 09:59:35 -0500 Subject: [PATCH] feat: add back self.top to wrapfn --- src/client/shared/wrap.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/client/shared/wrap.ts b/src/client/shared/wrap.ts index 705d9ec..70d184a 100644 --- a/src/client/shared/wrap.ts +++ b/src/client/shared/wrap.ts @@ -22,9 +22,23 @@ export function createWrapFn(client: ScramjetClient, self: typeof globalThis) { } } else if (identifier === self.document) { return client.documentProxy; - } + } else if (identifier === self.top) { + // instead of returning top, we need to return the uppermost parent that's inside a scramjet context + let current = self.self; - // TODO .top + for (;;) { + const test = current.parent.self; + if (test === current) break; // there is no parent, actual or emulated. + + // ... then `test` represents a window outside of the proxy context, and therefore `current` is the topmost window in the proxy context + if (!(SCRAMJETCLIENT in test)) break; + + // test is also insde a proxy, so we should continue up the chain + current = test; + } + + return current[SCRAMJETCLIENT].globalProxy.window; + } } return identifier;