How hulufei/electron-as-browser works
Project type and purpose
• This is an open-source Node module for Electron apps called “electron-as-browser.”
• It provides a BrowserLikeWindow class that you instantiate in the Electron main process to spin up a browser-style window using BrowserView, plus a pluggable control panel in the renderer process.
File structure
• Top level
• index.js / index.d.ts – exports the BrowserLikeWindow constructor and sets up the public API
• control.js / control.d.ts – IPC helper functions for sending browser-control commands
• useConnect.js – React hook that wraps IPC channels for control panels
• README.md, docs/ – documentation and static assets for the site
• example/ – a minimal Electron app showing how to wire up main and renderer
• babel.config.js, package.json – build and dependency definitions
• Patterns
• Main process code (index.js) instantiates BrowserWindow and BrowserViews
• Renderer code in example/renderer is a standalone UI that talks to the main process via IPC
• IPC channels for bi-directional communication of tab events and control commands
README for additional insights
• Features emphasize BrowserView over webview, pluggable control panel, exposed webContents and BrowserWindow, automatic resizing
• Usage section shows two distinct processes:
• Main: create BrowserLikeWindow, listen to events (‘new-tab’, ‘closed’)
• Renderer: implement control panel UI, subscribe to tabs and active-tab events via IPC or useConnect hook, send commands via functions from control.js
a. Main components
• Electron Main Process
– BrowserLikeWindow module
– Underlying Electron API: BrowserWindow + BrowserView + webContents
• Renderer Control Panel
– HTML/JS UI loaded as a BrowserView inside the same BrowserWindow
– Uses either raw ipcRenderer or useConnect hook
• Tab Content Views
– One BrowserView per tab, each with its own webContents loading external pages
• IPC Layer
– Channels for ‘control-ready’, ‘tabs-update’, ‘active-update’
– Channels for commands: sendNewTab, sendEnterURL, sendGoBack, etc.
• External Services
– Remote websites loaded in BrowserViews via HTTP/HTTPS
b. Relationships and interactions
1. On app start, main process loads controlPanel URL into a BrowserView
2. Renderer sends ‘control-ready’ to signal it’s listening
3. Main process emits ‘tabs-update’ and ‘active-update’ on state changes
4. Renderer UI dispatches commands (sendNewTab, sendReload, etc.) via ipcRenderer to main
5. Main process updates internal state, creates/destroys BrowserViews, adjusts BrowserWindow layout
6. New or switched tabs load external URLs in separate BrowserViews
c. Architectural patterns and design principles
• Event-driven architecture (IPC as the event bus)
• Separation of concerns: main process handles heavy lifting and window management; renderer handles user interface
• Pluggable UI: control panel can be any HTML/JS framework (React via useConnect or plain JS via control.js)
• Modular exports: index.js, control.js, useConnect.js as composable parts
d. Key technologies, frameworks, libraries
• Electron APIs: BrowserWindow, BrowserView, webContents, ipcMain, ipcRenderer
• Node.js module system (import/require)
• Optional React (via useConnect hook)
• Babel for transpilation
Detail
• Lifecycle of a tab: command → view creation → tabs-update event → view destroyed on close
• Resizing logic (main process auto-resize of views when window changes)