-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(cli): restore cursor overlay through page.evaluate #2869
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| export const CURSOR_OVERLAY_SCRIPT = `(() => { | ||
| if (globalThis !== globalThis.top) return; | ||
|
|
||
| const cursorId = "__browse_cursor_overlay__"; | ||
| const ensureCursor = () => { | ||
| const existing = document.getElementById(cursorId); | ||
| if (existing instanceof HTMLDivElement) return existing; | ||
|
|
||
| const root = document.documentElement || document.body; | ||
| if (!root) return null; | ||
|
|
||
| const cursor = document.createElement("div"); | ||
| cursor.id = cursorId; | ||
| cursor.setAttribute("aria-hidden", "true"); | ||
| Object.assign(cursor.style, { | ||
| contain: "layout style paint", | ||
| height: "24px", | ||
| left: "0px", | ||
| mixBlendMode: "normal", | ||
| pointerEvents: "none", | ||
| position: "fixed", | ||
| top: "0px", | ||
| userSelect: "none", | ||
| width: "16px", | ||
| willChange: "left,top", | ||
| zIndex: "2147483647", | ||
| }); | ||
| cursor.innerHTML = | ||
| '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="24" viewBox="0 0 16 24"><path d="M1 0 L1 22 L6 14 L15 14 Z" fill="black" stroke="white" stroke-width="0.7"/></svg>'; | ||
| root.appendChild(cursor); | ||
| return cursor; | ||
| }; | ||
|
|
||
| const moveCursor = (x, y) => { | ||
| const cursor = ensureCursor(); | ||
| if (!cursor) return; | ||
| cursor.style.left = Math.max(0, x) + "px"; | ||
| cursor.style.top = Math.max(0, y) + "px"; | ||
| }; | ||
|
|
||
| const installCursor = () => { | ||
| if (ensureCursor()) return; | ||
| if (globalThis.__browseCursorOverlayDomReadyListenerInstalled__) return; | ||
|
|
||
| document.addEventListener( | ||
| "DOMContentLoaded", | ||
| () => { | ||
| globalThis.__browseCursorOverlayDomReadyListenerInstalled__ = false; | ||
| ensureCursor(); | ||
| }, | ||
| { once: true }, | ||
| ); | ||
| globalThis.__browseCursorOverlayDomReadyListenerInstalled__ = true; | ||
| }; | ||
|
|
||
| globalThis.__browseMoveCursorOverlay__ = moveCursor; | ||
| installCursor(); | ||
| if (!globalThis.__browseCursorOverlayListenerInstalled__) { | ||
| document.addEventListener( | ||
| "mousemove", | ||
|
shrey150 marked this conversation as resolved.
|
||
| (event) => { | ||
| moveCursor(event.clientX, event.clientY); | ||
| }, | ||
| { capture: true }, | ||
| ); | ||
| globalThis.__browseCursorOverlayListenerInstalled__ = true; | ||
| } | ||
| })()`; | ||
|
|
||
| export function updateCursorOverlayPosition(position: { | ||
| x: number; | ||
| y: number; | ||
| }): void { | ||
| const moveCursor = ( | ||
| globalThis as typeof globalThis & { | ||
| __browseMoveCursorOverlay__?: (x: number, y: number) => void; | ||
| } | ||
| ).__browseMoveCursorOverlay__; | ||
| moveCursor?.(position.x, position.y); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import { createContext, runInContext } from "node:vm"; | ||
|
|
||
| import { describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { CURSOR_OVERLAY_SCRIPT } from "../src/lib/driver/cursor-overlay.js"; | ||
|
|
||
| describe("cursor overlay", () => { | ||
| it("installs after DOMContentLoaded when the document root is not ready", () => { | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| const harness = createCursorHarness({ ready: false }); | ||
|
|
||
| harness.install(); | ||
|
|
||
| expect(harness.elements.size).toBe(0); | ||
| expect(harness.listeners.has("DOMContentLoaded")).toBe(true); | ||
|
|
||
| harness.makeDocumentReady(); | ||
| harness.listeners.get("DOMContentLoaded")!(); | ||
|
|
||
| expect(harness.cursor()).toBeInstanceOf(FakeDiv); | ||
| }); | ||
|
|
||
| it("installs a click-through cursor once in an already-ready document", () => { | ||
| const harness = createCursorHarness(); | ||
|
|
||
| harness.install(); | ||
|
|
||
| expect(harness.cursor()?.style).toMatchObject({ | ||
| left: "0px", | ||
| pointerEvents: "none", | ||
| position: "fixed", | ||
| top: "0px", | ||
| zIndex: "2147483647", | ||
| }); | ||
| expect(harness.listeners.has("DOMContentLoaded")).toBe(false); | ||
| expect(harness.listeners.has("mousemove")).toBe(true); | ||
|
|
||
| harness.install(); | ||
|
|
||
| expect(harness.document.createElement).toHaveBeenCalledOnce(); | ||
| expect(harness.document.addEventListener).toHaveBeenCalledOnce(); | ||
| expect(harness.elements.size).toBe(1); | ||
| }); | ||
|
|
||
| it("moves and clamps the cursor from top-document mouse events", () => { | ||
| const harness = createCursorHarness(); | ||
| harness.install(); | ||
|
|
||
| const mousemove = harness.listeners.get("mousemove")!; | ||
| mousemove({ clientX: -25, clientY: 80 }); | ||
| expect(harness.cursor()?.style).toMatchObject({ | ||
| left: "0px", | ||
| top: "80px", | ||
| }); | ||
|
|
||
| mousemove({ clientX: 140, clientY: -10 }); | ||
| expect(harness.cursor()?.style).toMatchObject({ | ||
| left: "140px", | ||
| top: "0px", | ||
| }); | ||
| }); | ||
|
|
||
| it("does not install inside a child frame", () => { | ||
| const harness = createCursorHarness({ topFrame: false }); | ||
|
|
||
| harness.install(); | ||
|
|
||
| expect(harness.elements.size).toBe(0); | ||
| expect(harness.document.createElement).not.toHaveBeenCalled(); | ||
| expect(harness.document.addEventListener).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| class FakeDiv { | ||
| id = ""; | ||
| innerHTML = ""; | ||
| style: Record<string, string> = {}; | ||
|
|
||
| setAttribute(): void {} | ||
| } | ||
|
|
||
| type CursorEvent = { clientX: number; clientY: number }; | ||
| type CursorListener = (event?: CursorEvent) => void; | ||
|
|
||
| function createCursorHarness( | ||
| options: { ready?: boolean; topFrame?: boolean } = {}, | ||
| ) { | ||
| const elements = new Map<string, FakeDiv>(); | ||
| const listeners = new Map<string, CursorListener>(); | ||
| const root = { | ||
| appendChild(element: FakeDiv) { | ||
| elements.set(element.id, element); | ||
| }, | ||
| }; | ||
| let documentElement: typeof root | null = | ||
| options.ready === false ? null : root; | ||
| const document = { | ||
| addEventListener: vi.fn((name: string, listener: CursorListener) => { | ||
| listeners.set(name, listener); | ||
| }), | ||
| body: null, | ||
| createElement: vi.fn(() => new FakeDiv()), | ||
| get documentElement() { | ||
| return documentElement; | ||
| }, | ||
| getElementById: vi.fn((id: string) => elements.get(id) ?? null), | ||
| }; | ||
| const context = createContext({ document, HTMLDivElement: FakeDiv }); | ||
| runInContext( | ||
| `globalThis.top = ${options.topFrame === false ? "{}" : "globalThis"}`, | ||
| context, | ||
| ); | ||
|
|
||
| return { | ||
| cursor: () => elements.get("__browse_cursor_overlay__"), | ||
| document, | ||
| elements, | ||
| install: () => runInContext(CURSOR_OVERLAY_SCRIPT, context), | ||
| listeners, | ||
| makeDocumentReady: () => { | ||
| documentElement = root; | ||
| }, | ||
| }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.