Examples
⚡ Loading a virtual browser
Hyperbeam(container: HTMLDivElement | HTMLIFrameElement, embedURL: string, opts?: HyperbeamOptions): Promise<HyperbeamClient>
Creates a new Hyperbeam client. The container can be either a div
or an iframe
element, though it is highly advised to use a div
. The embedURL
is retrieved from the REST API.
The promise returned by the Hyperbeam
function might be rejected. See example below:
💣 Destroying the embed
hb.destroy()
Tears down network connections and browser events. Always call this before removing the container element from the page.
🔉 Setting video volume
hb.volume = 0.5
Sets the volume for the virtual browser locally. Volume changes only apply to the local user. This setting is not persisted on refreshing the page.
🆔 Getting user ID
hb.userId: string
Gets the client’s user ID. A “user” is defined as a single connection to the virtual browser. If a person has multiple tabs connected to the virtual browser, each tab with an active connection will be assigned a different user ID.
⏸️ Pausing video stream
hb.videoPaused = true
Pauses/resumes the video stream for the virtual browser locally. Useful if only the audio component is of interest and you want to minimize CPU usage.
👑 Setting admin token
hb.adminToken = "adminToken"
Sets the client’s admin token. The client must have an admin token set to manage user permissions and control the tabs programmatically.
🔐 Setting permissions
hb.setPermission(userId: string, permissionData: PermissionData): Promise<void>
Sets the permission of a user by their ID. The client must have an admin token set to manage user permissions.
🔄 Manual reconnection
hb.reconnect(): void
In situations where you need to troubleshoot the browser disconnecting, adding a manual reconnect button may help for debugging.
📐 Resizing the browser
hb.resize(width: number, height: number): Promise
Resizes the virtual browser to the specified width and height, in pixels.
The arguments must meet the following conditions, otherwise the function will throw a RangeError
: width * height
cannot be greater than hb.maxArea
.
hb.maxArea
is the maximum area that can be allocated in pixels.
You can retrieve the current width and height by reading hb.width
and hb.height
.
📡 Send events programmatically
hb.sendEvent(event: KeyEvent | MouseEvent | WheelEvent): void
Sends a keyboard, mouse, or mouse wheel event to the Hyperbeam browser.
⌨️ Tighter control over keyboard events
By default, the Hyperbeam client forwards global keydown
and keyup
events into the embed when an input element is not in focus outside the embed. In some cases, you may want to customize this functionality:
- Your app has key bindings which conflict with Hyperbeam
- You have UI flows such as overlays and tabs which hide the Hyperbeam embed from view
You can have the Hyperbeam client disregard these events by setting the delegateKeyboard
option to false
. Input fields inside the embed (e.g. address bar, form fields) will continue to receive input when they are focused.
Moreover, you can combine this with sending keyboard events programmatically to selectively forward keyboard events:
🎛️ Control tabs programmatically
Hyperbeam’s tab API mirrors Chrome extension tab API, found here.
Rather than calling chrome.tabs.create({ active: true })
, you will would call hb.tabs.create({ active: true })
.
We do not support callbacks for accessing return values — tab methods return a Promise
We support the following methods:
- tabs.create
- tabs.detectLanguage
- tabs.discard
- tabs.duplicate
- tabs.get
- tabs.getZoom
- tabs.getZoomSettings
- tabs.goBack
- tabs.goForward
- tabs.group
- tabs.highlight
- tabs.move
- tabs.query
- tabs.reload
- tabs.remove
- tabs.setZoom
- tabs.setZoomSettings
- tabs.ungroup
- tabs.update
👂 Listen to tab events
Hyperbeam’s tab event listener API mirrors Chrome extension tab API, found here.
Rather than calling chrome.tabs.onCreated.addListener((tab) => {})
, you will would call hb.tabs.onCreated.addListener((tab) => {})
.
We support the following events:
- tabs.onActivated
- tabs.onCreated
- tabs.onHighlighted
- tabs.onMoved
- tabs.onRemoved
- tabs.onReplaced
- tabs.onUpdated
- tabs.onZoomChange
🗺️ Optimize server location
To create a session with the optimal server location, you first need to use getRegionInfo()
on the client to retrieve the optimal region information.
From there, you need to send the HyperbeamRegionInfo object’s region
value to your backend, and use it as the argument for the region
parameter when creating a session and generating an embed_url
.
You may also want to cache the region preference in localStorage, since
getRegionInfo()
fires off a web request.
Example