event target

This commit is contained in:
velzie 2024-08-31 11:29:09 -04:00
parent 5f7a9d4bd4
commit 08ccb4f56e
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
6 changed files with 48 additions and 5 deletions

View file

@ -8,6 +8,7 @@ import { createLocationProxy } from "./location";
import { nativeGetOwnPropertyDescriptor } from "./natives";
import { CookieStore, config, decodeUrl, encodeUrl } from "../shared";
import { createWrapFn } from "./shared/wrap";
import { NavigateEvent } from "./events";
declare global {
interface Window {
@ -26,6 +27,7 @@ export type ProxyCtx = {
args: any[];
newTarget: AnyFunction;
return: (r: any) => void;
call: () => any;
};
export type Proxy = {
construct?(ctx: ProxyCtx): any;
@ -140,9 +142,15 @@ export class ScramjetClient {
}
set url(url: URL | string) {
if (typeof url === "string") url = new URL(url);
if (url instanceof URL) url = url.toString();
self.location.href = encodeUrl(url.href);
const ev = new NavigateEvent(url);
if (this.frame) {
this.frame.dispatchEvent(ev);
}
if (ev.defaultPrevented) return;
self.location.href = encodeUrl(ev.url);
}
// below are the utilities for proxying and trapping dom APIs
@ -192,6 +200,7 @@ export class ScramjetClient {
return: (r: any) => {
returnValue = r;
},
call: () => alert("todo"),
};
handler.construct(ctx);
@ -218,6 +227,10 @@ export class ScramjetClient {
earlyreturn = true;
returnValue = r;
},
call: () => {
earlyreturn = true;
returnValue = Reflect.apply(ctx.fn, ctx.this, ctx.args);
},
};
const pst = Error.prepareStackTrace;

View file

@ -1,16 +1,25 @@
import { ScramjetClient } from "../client";
import { encodeUrl } from "../../shared";
import { UrlChangeEvent } from "../events";
export default function (client: ScramjetClient, self: typeof globalThis) {
client.Proxy("history.pushState", {
apply(ctx) {
ctx.args[2] = encodeUrl(ctx.args[2]);
ctx.call();
const ev = new UrlChangeEvent(client.url.href);
if (client.frame) client.frame.dispatchEvent(ev);
},
});
client.Proxy("history.replaceState", {
apply(ctx) {
ctx.args[2] = encodeUrl(ctx.args[2]);
ctx.call();
const ev = new UrlChangeEvent(client.url.href);
if (client.frame) client.frame.dispatchEvent(ev);
},
});
}

11
src/client/events.ts Normal file
View file

@ -0,0 +1,11 @@
export class NavigateEvent extends Event {
constructor(public url: string) {
super("navigate");
}
}
export class UrlChangeEvent extends Event {
constructor(public url: string) {
super("urlchange");
}
}

View file

@ -26,7 +26,7 @@ export class ScramjetController {
client: "/scramjet.client.js",
codecs: "/scramjet.codecs.js",
flags: {
serviceworkers: false,
serviceworkers: true,
},
};

4
src/types.d.ts vendored
View file

@ -1,4 +1,4 @@
import { ScramjetController } from "./bootsrapper/index";
import { ScramjetController } from "./controller/index";
import { encodeUrl, decodeUrl } from "./shared/rewriters/url";
import { rewriteCss } from "./shared/rewriters/css";
import {
@ -21,6 +21,7 @@ import { ScramjetFrame } from "./controller/frame";
type ScramjetFlags = {
serviceworkers: boolean;
naiiverewriter: boolean;
};
interface ScramjetConfig {
@ -39,6 +40,7 @@ interface ScramjetConfig {
client: string;
codecs: string;
flags: ScramjetFlags;
siteflags?: Record<string, ScramjetFlags>;
}
declare global {

View file

@ -153,6 +153,10 @@ function App() {
const frame = scramjet.createFrame();
frame.addEventListener("urlchange", (e) => {
this.url = e.url;
});
return html`
<div>
<h1>Percury Unblocker</h1>
@ -185,7 +189,11 @@ function App() {
</div>
<div class=${[flex, "nav"]} style="width: 60%">
<button on:click=${() => frame.back()}>&lt;-</button>
<input class="bar" style="flex: 1" bind:value=${use(store.url)} on:input=${(e) => (store.url = e.target.value)} on:keyup=${(e) => e.keyCode == 13 && frame.go(e.target.value)}></input>
<input class="bar" style="flex: 1" bind:value=${use(this.url)} on:input=${(
e
) => {
this.url = e.target.value;
}} on:keyup=${(e) => e.keyCode == 13 && frame.go(e.target.value) && (store.url = this.url)}></input>
<button on:click=${() => frame.forward()}>-&gt;</button>
</div>
${frame.frame}