mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-15 23:30:00 -04:00
dispatch urlchange event on client creation
This commit is contained in:
parent
6e451eee7f
commit
fd346e8c6d
8 changed files with 268 additions and 265 deletions
|
@ -254,7 +254,16 @@ export class ScramjetClient {
|
|||
|
||||
return sframe;
|
||||
}
|
||||
get isSubframe(): boolean {
|
||||
if (!iswindow) return false;
|
||||
const frame = this.descriptors.get("window.frameElement", this.global);
|
||||
|
||||
if (!frame) return false; // we're top level
|
||||
const sframe = frame[SCRAMJETFRAME];
|
||||
if (!sframe) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
loadcookies(cookiestr: string) {
|
||||
this.cookieStore.load(cookiestr);
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ export default function (client: ScramjetClient, _self: Self) {
|
|||
ctx.call();
|
||||
|
||||
const ev = new UrlChangeEvent(client.url.href);
|
||||
if (client.frame) client.frame.dispatchEvent(ev);
|
||||
if (!client.isSubframe) client.frame?.dispatchEvent(ev);
|
||||
},
|
||||
});
|
||||
|
||||
|
@ -21,7 +21,7 @@ export default function (client: ScramjetClient, _self: Self) {
|
|||
ctx.call();
|
||||
|
||||
const ev = new UrlChangeEvent(client.url.href);
|
||||
if (client.frame) client.frame.dispatchEvent(ev);
|
||||
if (!client.isSubframe) client.frame?.dispatchEvent(ev);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ export class UrlChangeEvent extends Event {
|
|||
}
|
||||
}
|
||||
|
||||
export class ScramjetContextInit extends Event {
|
||||
export class ScramjetContextEvent extends Event {
|
||||
constructor(public window: Self) {
|
||||
super("contextInit");
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
import { loadCodecs } from "../scramjet";
|
||||
import { SCRAMJETCLIENT } from "../symbols";
|
||||
import { ScramjetClient } from "./client";
|
||||
import { ScramjetContextInit } from "./events";
|
||||
import { ScramjetContextEvent, UrlChangeEvent } from "./events";
|
||||
import { ScramjetServiceWorkerRuntime } from "./swruntime";
|
||||
|
||||
export const iswindow = "window" in self && window instanceof Window;
|
||||
|
@ -30,8 +30,10 @@ if (!(SCRAMJETCLIENT in <Partial<typeof self>>self)) {
|
|||
runtime.hook();
|
||||
}
|
||||
|
||||
const ev = new ScramjetContextInit(client.global.window);
|
||||
client.frame?.dispatchEvent(ev);
|
||||
const contextev = new ScramjetContextEvent(client.global.window);
|
||||
client.frame?.dispatchEvent(contextev);
|
||||
const urlchangeev = new UrlChangeEvent(client.url.href);
|
||||
if (!client.isSubframe) client.frame?.dispatchEvent(urlchangeev);
|
||||
}
|
||||
|
||||
Reflect.deleteProperty(self, "WASM");
|
||||
|
|
|
@ -41,7 +41,7 @@ export async function handleFetch(
|
|||
requesturl.searchParams.delete("dest");
|
||||
}
|
||||
|
||||
if (requesturl.pathname == this.config.files.wasm) {
|
||||
if (requesturl.pathname === this.config.files.wasm) {
|
||||
return fetch(this.config.files.wasm).then(async (x) => {
|
||||
const buf = await x.arrayBuffer();
|
||||
const b64 = btoa(
|
||||
|
@ -54,7 +54,8 @@ export async function handleFetch(
|
|||
);
|
||||
|
||||
let payload = "";
|
||||
payload += `if ("document" in self && document.currentScript) { document.currentScript.remove(); }\n`;
|
||||
payload +=
|
||||
"if ('document' in self && document.currentScript) { document.currentScript.remove(); }\n";
|
||||
payload += `self.WASM = '${b64}';`;
|
||||
|
||||
return new Response(payload, {
|
||||
|
@ -164,11 +165,11 @@ export async function handleFetch(
|
|||
|
||||
const ev = new ScramjetRequestEvent(
|
||||
url,
|
||||
headers.headers,
|
||||
request.body,
|
||||
request.method,
|
||||
request.destination,
|
||||
client,
|
||||
headers.headers
|
||||
client
|
||||
);
|
||||
this.dispatchEvent(ev);
|
||||
|
||||
|
@ -396,11 +397,11 @@ export class ScramjetHandleResponseEvent extends Event {
|
|||
export class ScramjetRequestEvent extends Event {
|
||||
constructor(
|
||||
public url: URL,
|
||||
public requestHeaders: Record<string, string>,
|
||||
public body: BodyType,
|
||||
public method: string,
|
||||
public destination: string,
|
||||
public client: Client,
|
||||
public requestHeaders: Record<string, string>
|
||||
public client: Client
|
||||
) {
|
||||
super("request");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue