mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-16 07:30:02 -04:00
properly rewrite postmessage
This commit is contained in:
parent
34c3cc5094
commit
c2d147442e
9 changed files with 134 additions and 17 deletions
81
src/client/shared/event.ts
Normal file
81
src/client/shared/event.ts
Normal file
|
@ -0,0 +1,81 @@
|
|||
import { iswindow } from "..";
|
||||
import { ScramjetClient } from "../client";
|
||||
|
||||
const realOnEvent = Symbol.for("scramjet original onevent function");
|
||||
|
||||
export default function (client: ScramjetClient, self: Self) {
|
||||
const handlers = {
|
||||
message: {
|
||||
origin() {
|
||||
return this.data.$scramjet$origin;
|
||||
},
|
||||
data() {
|
||||
return this.data.$scramjet$data;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
function wraplistener(listener: (...args: any) => any) {
|
||||
return new Proxy(listener, {
|
||||
apply(target, thisArg, argArray) {
|
||||
const realEvent: Event = argArray[0];
|
||||
|
||||
// we only need to handle events dispatched from the browser
|
||||
if (realEvent.isTrusted) {
|
||||
const type = realEvent.type;
|
||||
|
||||
if (type in handlers) {
|
||||
let handler = handlers[type];
|
||||
argArray[0] = new Proxy(realEvent, {
|
||||
get(_target, prop, reciever) {
|
||||
if (prop in handler) {
|
||||
return handler[prop].call(_target);
|
||||
}
|
||||
|
||||
return Reflect.get(target, prop, reciever);
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return listener.apply(self, argArray);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
client.Proxy("EventTarget.prototype.addEventListener", {
|
||||
apply(ctx) {
|
||||
ctx.args[1] = wraplistener(ctx.args[1]);
|
||||
},
|
||||
});
|
||||
|
||||
if (!iswindow) return;
|
||||
|
||||
const targets = [self.window, self.HTMLElement.prototype];
|
||||
|
||||
for (const target of targets) {
|
||||
const keys = Reflect.ownKeys(target);
|
||||
|
||||
for (const key of keys) {
|
||||
if (typeof key === "string" && key.startsWith("on")) {
|
||||
const descriptor = Object.getOwnPropertyDescriptor(target, key);
|
||||
if (!descriptor.get || !descriptor.set || !descriptor.configurable)
|
||||
continue;
|
||||
|
||||
// these are the `onmessage`, `onclick`, etc. properties
|
||||
client.RawTrap(target, key, {
|
||||
get(ctx) {
|
||||
if (this[realOnEvent]) return this[realOnEvent];
|
||||
|
||||
return ctx.get();
|
||||
},
|
||||
set(ctx, value: any) {
|
||||
this[realOnEvent] = value;
|
||||
|
||||
ctx.set(wraplistener(value));
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
import { encodeUrl } from "../shared";
|
||||
import { importfn } from "./wrap";
|
||||
import { importfn } from "../";
|
||||
|
||||
export default function (client, self) {
|
||||
self[importfn] = function (base) {
|
||||
|
|
|
@ -1,21 +1,16 @@
|
|||
import { iswindow, isworker, trysetfn, wrapfn } from "..";
|
||||
import { ScramjetClient } from "../client";
|
||||
|
||||
export const iswindow = "window" in self;
|
||||
export const isworker = "WorkerGlobalScope" in self;
|
||||
export const issw = "ServiceWorkerGlobalScope" in self;
|
||||
export const isdedicated = "DedicatedWorkerGlobalScope" in self;
|
||||
export const isshared = "SharedWorkerGlobalScope" in self;
|
||||
|
||||
export const wrapfn = "$scramjet$wrap";
|
||||
export const trysetfn = "$scramjet$tryset";
|
||||
export const importfn = "$scramjet$import";
|
||||
|
||||
export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||
// the main magic of the proxy. all attempts to access any "banned objects" will be redirected here, and instead served a proxy object
|
||||
// this contrasts from how other proxies will leave the root object alone and instead attempt to catch every member access
|
||||
// this presents some issues (see element.ts), but makes us a good bit faster at runtime!
|
||||
Object.defineProperty(self, wrapfn, {
|
||||
value: function (identifier: any) {
|
||||
value: function (identifier: any, args: any) {
|
||||
if (args && typeof args === "object" && args.length === 0)
|
||||
for (const arg of args) {
|
||||
argdbg(arg);
|
||||
}
|
||||
if (iswindow && identifier instanceof self.Window) {
|
||||
return client.windowProxy;
|
||||
} else if (iswindow && identifier instanceof self.parent.self.Window) {
|
||||
|
@ -75,4 +70,15 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
|||
writable: false,
|
||||
configurable: false,
|
||||
});
|
||||
|
||||
function argdbg(arg) {
|
||||
switch (typeof arg) {
|
||||
case "string":
|
||||
if (arg.includes("scramjet")) debugger;
|
||||
break;
|
||||
case "object":
|
||||
for (let ar of arg) argdbg(ar);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue