From 2cb27547edc1f54c1527612e5c9afda4f97f4bfb Mon Sep 17 00:00:00 2001 From: lino-levan <11367844+lino-levan@users.noreply.github.com> Date: Thu, 31 Aug 2023 14:55:04 +1200 Subject: [PATCH] feat: url property to page --- src/browser.ts | 2 +- src/page.ts | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/browser.ts b/src/browser.ts index 888bccf..9c624fe 100644 --- a/src/browser.ts +++ b/src/browser.ts @@ -87,7 +87,7 @@ export class Browser { const websocket = new WebSocket(wsUrl); await websocketReady(websocket); - const page = new Page(targetId, websocket, this); + const page = new Page(targetId, url, websocket, this); this.pages.push(page); const celestial = page.unsafelyGetCelestialBindings(); diff --git a/src/page.ts b/src/page.ts index 633e469..432af83 100644 --- a/src/page.ts +++ b/src/page.ts @@ -54,17 +54,29 @@ export class Page { #id: string; #celestial: Celestial; #browser: Browser; + #url: string | undefined; readonly timeout = 10000; readonly mouse: Mouse; readonly keyboard: Keyboard; readonly touchscreen: Touchscreen; - constructor(id: string, ws: WebSocket, browser: Browser) { + constructor( + id: string, + url: string | undefined, + ws: WebSocket, + browser: Browser, + ) { this.#id = id; + this.#url = url; this.#celestial = new Celestial(ws); this.#browser = browser; + this.#celestial.addEventListener("Page.frameNavigated", (e) => { + const { frame } = e.detail; + this.#url = frame.urlFragment ?? frame.url; + }); + this.mouse = new Mouse(this.#celestial); this.keyboard = new Keyboard(this.#celestial); this.touchscreen = new Touchscreen(this.#celestial); @@ -330,6 +342,13 @@ export class Page { return convertToUint8Array(data); } + /** + * The current URL of the page + */ + get url() { + return this.#url; + } + /** * Runs a function in the context of the page until it returns a truthy value. */