intercept window.open

This commit is contained in:
velzie 2024-07-28 10:47:43 -04:00
parent f4a4a0e7e4
commit c93952b885
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F

36
src/client/dom/open.ts Normal file
View file

@ -0,0 +1,36 @@
import { encodeUrl } from "../../shared/rewriters/url";
import { ScramjetClient } from "../client";
export default function (client: ScramjetClient) {
client.Proxy("window.open", {
apply(ctx) {
if (ctx.args[0]) ctx.args[0] = encodeUrl(ctx.args[0]);
const realwin = ctx.fn.apply(this, ctx.args);
if (ScramjetClient.SCRAMJET in realwin.self) {
return ctx.return(realwin.self[ScramjetClient.SCRAMJET].windowProxy);
} else {
const newclient = new ScramjetClient(realwin.self);
// hook the opened window
newclient.hook();
return ctx.return(newclient.windowProxy);
}
},
});
// opener will refer to the real window if it was opened by window.open
client.Trap("opener", {
get(ctx) {
const realwin = ctx.get() as Window;
if (ScramjetClient.SCRAMJET in realwin.self) {
return realwin.self[ScramjetClient.SCRAMJET].windowProxy;
} else {
// the opener has to have been already hooked, so if we reach here then it's a real window
return undefined;
}
},
});
}