| 1 | import type { KeyboardInputEvent, MouseInputEvent, MouseWheelInputEvent, Rectangle, WebPreferences } from "electron"; |
| 2 | |
| 3 | // The slice of Electron the browser modules touch. Production binds the real |
| 4 | // WebContentsView (see electronGuestViews.ts); tests inject fakes. |
| 5 | |
| 6 | export interface GuestFrame { |
| 7 | readonly frameTreeNodeId: number; |
| 8 | readonly url: string; |
| 9 | readonly detached: boolean; |
| 10 | readonly parent: GuestFrame | null; |
| 11 | readonly framesInSubtree: GuestFrame[]; |
| 12 | executeJavaScript(code: string, userGesture?: boolean): Promise<unknown>; |
| 13 | } |
| 14 | |
| 15 | export interface GuestDebugger { |
| 16 | isAttached(): boolean; |
| 17 | attach(protocolVersion?: string): void; |
| 18 | detach(): void; |
| 19 | sendCommand(method: string, params?: unknown): Promise<unknown>; |
| 20 | on(event: "message", listener: (event: unknown, method: string, params: unknown) => void): unknown; |
| 21 | removeListener(event: "message", listener: (event: unknown, method: string, params: unknown) => void): unknown; |
| 22 | } |
| 23 | |
| 24 | export interface GuestImage { |
| 25 | toPNG(): Buffer; |
| 26 | getSize(): { width: number; height: number }; |
| 27 | } |
| 28 | |
| 29 | export interface GuestPage { |
| 30 | readonly id: number; |
| 31 | readonly mainFrame: GuestFrame; |
| 32 | readonly debugger: GuestDebugger; |
| 33 | readonly navigationHistory: { canGoBack(): boolean; canGoForward(): boolean; goBack(): void; goForward(): void }; |
| 34 | isDestroyed(): boolean; |
| 35 | loadURL(url: string): Promise<void>; |
| 36 | getURL(): string; |
| 37 | getTitle(): string; |
| 38 | isLoading(): boolean; |
| 39 | reload(): void; |
| 40 | stop(): void; |
| 41 | getZoomFactor(): number; |
| 42 | setZoomFactor(factor: number): void; |
| 43 | isDevToolsOpened(): boolean; |
| 44 | openDevTools(options?: { mode: "detach" }): void; |
| 45 | closeDevTools(): void; |
| 46 | focus(): void; |
| 47 | executeJavaScriptInIsolatedWorld(worldId: number, scripts: { code: string }[], userGesture?: boolean): Promise<unknown>; |
| 48 | sendInputEvent(event: MouseInputEvent | MouseWheelInputEvent | KeyboardInputEvent): void; |
| 49 | insertText(text: string): Promise<void>; |
| 50 | capturePage(rect?: Rectangle): Promise<GuestImage>; |
| 51 | } |
| 52 | |
| 53 | export interface GuestViewEvents { |
| 54 | onStartLoading(): void; |
| 55 | onStopLoading(): void; |
| 56 | onNavigate(url: string, inPage: boolean): void; |
| 57 | onTitle(title: string): void; |
| 58 | onFailLoad(code: number, description: string, url: string): void; |
| 59 | onRenderProcessGone(reason: string): void; |
| 60 | onDestroyed(): void; |
| 61 | // Popups (window.open, target=_blank) become tabs of the same task and |
| 62 | // partition; adopt() receives the view Electron created for the child. |
| 63 | onPopup(url: string, disposition: string): ((view: GuestView) => void) | null; |
| 64 | } |
| 65 | |
| 66 | export interface GuestView { |
| 67 | readonly page: GuestPage; |
| 68 | bind(events: GuestViewEvents): void; |
| 69 | setBounds(bounds: Rectangle): void; |
| 70 | setVisible(visible: boolean): void; |
| 71 | destroy(): void; |
| 72 | } |
| 73 | |
| 74 | export interface GuestViewFactory { |
| 75 | create(partition: string, inherited?: WebPreferences): GuestView; |
| 76 | } |
| 77 | |
| 78 | export const BLOCKED_NAVIGATION_SCHEMES = new Set(["file:", "reasonix:", "javascript:", "data:", "blob:"]); |
| 79 | |
| 80 | export function isBlockedNavigation(url: string): boolean { |
| 81 | try { |
| 82 | return BLOCKED_NAVIGATION_SCHEMES.has(new URL(url).protocol); |
| 83 | } catch { |
| 84 | return true; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | export function isPopupURL(url: string): boolean { |
| 89 | if (url === "about:blank" || url === "") return true; |
| 90 | try { |
| 91 | const protocol = new URL(url).protocol; |
| 92 | return protocol === "http:" || protocol === "https:"; |
| 93 | } catch { |
| 94 | return false; |
| 95 | } |
| 96 | } |
| 97 |