diff --git a/src/client/shared/event.ts b/src/client/shared/event.ts index 55d68ae..c7450b4 100644 --- a/src/client/shared/event.ts +++ b/src/client/shared/event.ts @@ -1,5 +1,6 @@ import { iswindow } from ".."; import { ScramjetClient } from "../client"; +import { unproxy } from "./unproxy"; const realOnEvent = Symbol.for("scramjet original onevent function"); @@ -45,6 +46,7 @@ export default function (client: ScramjetClient, self: Self) { client.Proxy("EventTarget.prototype.addEventListener", { apply(ctx) { + unproxy(ctx, client); if (typeof ctx.args[1] === "object") ctx.args[1] = wraplistener(ctx.args[1]); }, diff --git a/src/client/shared/requests/websocket.ts b/src/client/shared/requests/websocket.ts index b687109..b22c1f6 100644 --- a/src/client/shared/requests/websocket.ts +++ b/src/client/shared/requests/websocket.ts @@ -2,12 +2,12 @@ import { isworker } from "../.."; import { ScramjetClient } from "../../client"; import { BareClient } from "../../shared"; +const bare = new BareClient(); + export default function (client: ScramjetClient, self: typeof globalThis) { // r58 please fix if (isworker) return; - const bare = new BareClient(); - client.Proxy("WebSocket", { construct(ctx) { ctx.return( diff --git a/src/client/shared/unproxy.ts b/src/client/shared/unproxy.ts index d60ac40..4749256 100644 --- a/src/client/shared/unproxy.ts +++ b/src/client/shared/unproxy.ts @@ -1,5 +1,5 @@ import { iswindow } from ".."; -import { ScramjetClient } from "../client"; +import { ProxyCtx, ScramjetClient } from "../client"; export default function (client: ScramjetClient, self: typeof window) { // an automated approach to cleaning the documentProxy from dom functions @@ -11,11 +11,7 @@ export default function (client: ScramjetClient, self: typeof window) { if (typeof target[prop] === "function") { client.RawProxy(target, prop, { apply(ctx) { - if (ctx.this == client.windowProxy) ctx.this = self; - for (const i in ctx.args) { - if (ctx.args[i] === client.windowProxy) - ctx.args[i] = self.global; - } + unproxy(ctx, client); }, }); } @@ -35,11 +31,7 @@ export default function (client: ScramjetClient, self: typeof window) { if (typeof target[prop] === "function") { client.RawProxy(target, prop, { apply(ctx) { - for (const i in ctx.args) { - if (ctx.args[i] === client.documentProxy) - ctx.args[i] = self.document; - if (ctx.this === client.documentProxy) ctx.this = self.document; - } + unproxy(ctx, client); }, }); } @@ -47,3 +39,14 @@ export default function (client: ScramjetClient, self: typeof window) { } } } + +export function unproxy(ctx: ProxyCtx, client: ScramjetClient) { + const self = client.global; + if (ctx.this === client.windowProxy) ctx.this = self; + if (ctx.this === client.documentProxy) ctx.this = self.document; + + for (const i in ctx.args) { + if (ctx.args[i] === client.documentProxy) ctx.args[i] = self.document; + if (ctx.args[i] === client.windowProxy) ctx.args[i] = self; + } +}