File size: 5,300 Bytes
bee6636 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
import { iswindow } from "..";
import { unrewriteUrl } from "../../shared/rewriters/url";
import { SCRAMJETCLIENT } from "../../symbols";
import { ScramjetClient } from "../client";
import { getOwnPropertyDescriptorHandler } from "../helpers";
import { nativeGetOwnPropertyDescriptor } from "../natives";
import { unproxy } from "./unproxy";
const realOnEvent = Symbol.for("scramjet original onevent function");
export default function (client: ScramjetClient, self: Self) {
const handlers = {
message: {
_init() {
if (typeof this.data === "object" && "$scramjet$type" in this.data) {
// this is a ctl message
return false;
}
return true;
},
ports() {
// don't know why i have to do this?
return this.ports;
},
source() {
if (this.source === null) return null;
const scram: ScramjetClient = this.source[SCRAMJETCLIENT];
if (scram) return scram.globalProxy;
return this.source;
},
origin() {
if (typeof this.data === "object" && "$scramjet$origin" in this.data)
return this.data.$scramjet$origin;
return client.url.origin;
},
data() {
if (typeof this.data === "object" && "$scramjet$data" in this.data)
return this.data.$scramjet$data;
return this.data;
},
},
hashchange: {
oldURL() {
return unrewriteUrl(this.oldURL);
},
newURL() {
return unrewriteUrl(this.newURL);
},
},
storage: {
_init() {
return this.key.startsWith(client.url.host + "@");
},
key() {
return this.key.substring(this.key.indexOf("@") + 1);
},
url() {
return unrewriteUrl(this.url);
},
},
};
function wraplistener(listener: (...args: any) => any) {
return new Proxy(listener, {
apply(target, that, args) {
const realEvent: Event = args[0];
// we only need to handle events dispatched from the browser
if (realEvent.isTrusted) {
const type = realEvent.type;
if (type in handlers) {
const handler = handlers[type];
if (handler._init) {
if (handler._init.call(realEvent) === false) return;
}
args[0] = new Proxy(realEvent, {
get(target, prop, reciever) {
const value = Reflect.get(target, prop);
if (prop in handler) {
return handler[prop].call(target);
}
if (typeof value === "function") {
return new Proxy(value, {
apply(target, that, args) {
if (that === reciever) {
return Reflect.apply(target, realEvent, args);
}
return Reflect.apply(target, that, args);
},
});
}
return value;
},
getOwnPropertyDescriptor: getOwnPropertyDescriptorHandler,
});
}
}
if (!self.event) {
Object.defineProperty(self, "event", {
get() {
return args[0];
},
configurable: true,
});
}
const rv = Reflect.apply(target, that, args);
return rv;
},
getOwnPropertyDescriptor: getOwnPropertyDescriptorHandler,
});
}
client.Proxy("EventTarget.prototype.addEventListener", {
apply(ctx) {
unproxy(ctx, client);
if (typeof ctx.args[1] !== "function") return;
const origlistener = ctx.args[1];
const proxylistener = wraplistener(origlistener);
ctx.args[1] = proxylistener;
let arr = client.eventcallbacks.get(ctx.this);
arr ||= [] as any;
arr.push({
event: ctx.args[0] as string,
originalCallback: origlistener,
proxiedCallback: proxylistener,
});
client.eventcallbacks.set(ctx.this, arr);
},
});
client.Proxy("EventTarget.prototype.removeEventListener", {
apply(ctx) {
unproxy(ctx, client);
if (typeof ctx.args[1] !== "function") return;
const arr = client.eventcallbacks.get(ctx.this);
if (!arr) return;
const i = arr.findIndex(
(e) => e.event === ctx.args[0] && e.originalCallback === ctx.args[1]
);
if (i === -1) return;
const r = arr.splice(i, 1);
client.eventcallbacks.set(ctx.this, arr);
ctx.args[1] = r[0].proxiedCallback;
},
});
client.Proxy("EventTarget.prototype.dispatchEvent", {
apply(ctx) {
unproxy(ctx, client);
},
});
const targets = [self.self, self.MessagePort.prototype] as Array<any>;
if (iswindow) targets.push(self.HTMLElement.prototype);
if (self.Worker) targets.push(self.Worker.prototype);
for (const target of targets) {
const keys = Reflect.ownKeys(target);
for (const key of keys) {
if (
typeof key === "string" &&
key.startsWith("on") &&
handlers[key.slice(2)]
) {
const descriptor = nativeGetOwnPropertyDescriptor(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;
if (typeof value !== "function") return ctx.set(value);
ctx.set(wraplistener(value));
},
});
}
}
}
}
|