mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 06:50:01 -04:00
intercept window.open
This commit is contained in:
parent
f4a4a0e7e4
commit
c93952b885
1 changed files with 36 additions and 0 deletions
36
src/client/dom/open.ts
Normal file
36
src/client/dom/open.ts
Normal 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;
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue