From 08ccb4f56e7893963041323cb38daf694f45653b Mon Sep 17 00:00:00 2001 From: velzie Date: Sat, 31 Aug 2024 11:29:09 -0400 Subject: [PATCH] event target --- src/client/client.ts | 17 +++++++++++++++-- src/client/dom/history.ts | 9 +++++++++ src/client/events.ts | 11 +++++++++++ src/controller/index.ts | 2 +- src/types.d.ts | 4 +++- static/ui.js | 10 +++++++++- 6 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 src/client/events.ts diff --git a/src/client/client.ts b/src/client/client.ts index 510532d..2811e3b 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -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; diff --git a/src/client/dom/history.ts b/src/client/dom/history.ts index 69559dd..caaf43c 100644 --- a/src/client/dom/history.ts +++ b/src/client/dom/history.ts @@ -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); }, }); } diff --git a/src/client/events.ts b/src/client/events.ts new file mode 100644 index 0000000..7a7da34 --- /dev/null +++ b/src/client/events.ts @@ -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"); + } +} diff --git a/src/controller/index.ts b/src/controller/index.ts index be0308c..d5bf67f 100644 --- a/src/controller/index.ts +++ b/src/controller/index.ts @@ -26,7 +26,7 @@ export class ScramjetController { client: "/scramjet.client.js", codecs: "/scramjet.codecs.js", flags: { - serviceworkers: false, + serviceworkers: true, }, }; diff --git a/src/types.d.ts b/src/types.d.ts index 1f3fac4..e13520f 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -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; } declare global { diff --git a/static/ui.js b/static/ui.js index 56a1519..8245123 100644 --- a/static/ui.js +++ b/static/ui.js @@ -153,6 +153,10 @@ function App() { const frame = scramjet.createFrame(); + frame.addEventListener("urlchange", (e) => { + this.url = e.url; + }); + return html`

Percury Unblocker

@@ -185,7 +189,11 @@ function App() {
- (store.url = e.target.value)} on:keyup=${(e) => e.keyCode == 13 && frame.go(e.target.value)}> + { + this.url = e.target.value; + }} on:keyup=${(e) => e.keyCode == 13 && frame.go(e.target.value) && (store.url = this.url)}>
${frame.frame}