Browser
A browser instance is responsible for loading, rendering, and controlling the web content.
import { Browser } from '@mobrowser/api';
The browser instance is created automatically when the BrowserWindow instance
is created. You can access the browser instance using the browser property
of the BrowserWindow instance.
Example
import { app, Browser } from '@mobrowser/api';
const win = app.createWindow()
const browser: Browser = win.browser
browser.loadUrl('https://example.com')
Properties
devTools
readonly devTools: DevTools;
The DevTools window that can be used to inspect and debug the browser.
Example
import { app, Browser } from '@mobrowser/api';
const win = app.createWindow()
const browser: Browser = win.browser
browser.devTools.open()
Methods
loadUrl()
loadUrl(url: string): void;
Loads the resource identified by the given URL.
Tells the browser to start asynchronous loading and returns immediately. There is no guarantee that the resource will be loaded completely, by the time this method returns. If there is a pending navigation, it will be stopped/canceled.
| Parameter | Type | Description |
|---|---|---|
url | string | The URL of the resource to load. It can be URL of a web page (e.g., "https://example.com") or an absolute path to a local file like "file:///path/to/file.html". |
Example
import { app, Browser } from '@mobrowser/api';
const win = app.createWindow()
const browser: Browser = win.browser
browser.loadUrl('https://example.com')
downloadUrl()
downloadUrl(url: string): void;
Initiates a download of the resource identified by the given URL without navigating the browser to it.
If the given URL is valid and points to a downloadable resource, then the download process will be started.
The method does nothing if the given URL is invalid or points to a resource that cannot be downloaded.
| Parameter | Type | Description |
|---|---|---|
url | string | The URL of the resource to download. |
Example
import { app, Browser } from '@mobrowser/api';
const win = app.createWindow()
const browser: Browser = win.browser
browser.downloadUrl('https://example.com/file.zip')
stop()
stop(): void;
Cancels any pending navigation or download operation and stops any dynamic page elements, such as background sounds and animations.
Example
import { app, Browser } from '@mobrowser/api';
const win = app.createWindow()
const browser: Browser = win.browser
browser.loadUrl('https://example.com')
browser.stop()
goBack()
goBack(): void;
Loads the previous location in the back-forward list. It does nothing if there is no previous location in the list.
goForward()
goForward(): void;
Loads the next location in the back-forward list. It does nothing if there is no next location in the list.
canGoBack()
canGoBack(): boolean;
Checks if the previous location can be loaded.
Example
if (win.browser.canGoBack()) {
win.browser.goBack()
}
canGoForward()
canGoForward(): boolean;
Checks if the next location can be loaded.
Example
if (win.browser.canGoForward()) {
win.browser.goForward()
}
reload()
reload(): void;
Reloads the currently loaded web page.
Example
import { app, Browser } from '@mobrowser/api';
const win = app.createWindow()
const browser: Browser = win.browser
browser.reload()
reloadIgnoringCache()
reloadIgnoringCache(): void;
Reloads the currently loaded web page ignoring caches.
Example
import { app, Browser } from '@mobrowser/api';
const win = app.createWindow()
const browser: Browser = win.browser
browser.reloadIgnoringCache()
reloadAndCheckForRepost()
reloadAndCheckForRepost(): void;
Reloads the currently loaded web page, but if the current web page has POST data, the user is prompted to see if they really want to reload the page.
Example
import { app, Browser } from '@mobrowser/api';
const win = app.createWindow()
const browser: Browser = win.browser
browser.reloadAndCheckForRepost()
reloadIgnoringCacheAndCheckForRepost()
reloadIgnoringCacheAndCheckForRepost(): void;
Reloads the currently loaded web page ignoring caches, but if the current web page has POST data, the user is prompted to see if they really want to reload the page.
Example
import { app, Browser } from '@mobrowser/api';
const win = app.createWindow()
const browser: Browser = win.browser
browser.reloadIgnoringCacheAndCheckForRepost()
close()
close(): void;
Try to close the browser by closing the currently loaded page as
if it calls the window.close() JavaScript function. If the page is closed
successfully, the browser instance will be closed.
Example
import { app, Browser } from '@mobrowser/api';
const win = app.createWindow()
const browser: Browser = win.browser
browser.close()
focus()
focus(): void;
Passes focus to the content of the browser.
Example
// main process
import { app, Browser } from '@mobrowser/api';
const win = app.createWindow()
const browser: Browser = win.browser
browser.focus()
Events
’titleChanged'
on(event: 'titleChanged', listener: (title: string) => void): void;
off(event: 'titleChanged', listener: (title: string) => void): void;
Emitted when the title of the web page is changed. It can be changed by JavaScript
through modifying the document.title property or when the user navigates to
a different page (e.g., by clicking on a link).
Example
import { app, Browser } from '@mobrowser/api';
const win = app.createWindow()
const browser: Browser = win.browser
browser.on('titleChanged', (title: string) => {
console.log(title)
})
‘consoleMessage’
on(event: 'consoleMessage', listener: (message: ConsoleMessage) => void): void;
off(event: 'consoleMessage', listener: (message: ConsoleMessage) => void): void;
Emitted when the currently loaded page logs a JavaScript console message.
Example
import { app, ConsoleMessage } from '@mobrowser/api';
const win = app.createWindow()
win.browser.on('consoleMessage', (message: ConsoleMessage) => {
console.log(message)
})
‘responsive’
on(event: 'responsive', listener: () => void): void;
off(event: 'responsive', listener: () => void): void;
Emitted when the page becomes responsive again after being unresponsive.
Example
import { app, Browser } from '@mobrowser/api';
const win = app.createWindow()
const browser: Browser = win.browser
browser.on('responsive', () => {
console.log('Page is responsive again')
})
‘unresponsive’
on(event: 'unresponsive', listener: () => void): void;
off(event: 'unresponsive', listener: () => void): void;
Emitted when the page becomes unresponsive.
It is fired when JavaScript code on the loaded web page is executing for a long period of time (~45 seconds) blocking the web page while user is trying to work with the web page by clicking or typing on it. If the web page does not respond, this event is triggered.
Example
import { app, Browser } from '@mobrowser/api';
const win = app.createWindow()
const browser: Browser = win.browser
browser.on('unresponsive', () => {
console.warn('Page is not responding')
})
’enterFullScreen'
on(event: 'enterFullScreen', listener: () => void): void;
off(event: 'enterFullScreen', listener: () => void): void;
Emitted when the window enters full screen mode triggered by HTML API.
Example
import { app, Browser } from '@mobrowser/api';
const win = app.createWindow()
const browser: Browser = win.browser
browser.on('enterFullScreen', () => {
console.log('Window entered full screen mode')
})
’exitFullScreen'
on(event: 'exitFullScreen', listener: () => void): void;
off(event: 'exitFullScreen', listener: () => void): void;
Emitted when the window exits full screen mode triggered by HTML API.
Example
import { app, Browser } from '@mobrowser/api';
const win = app.createWindow()
const browser: Browser = win.browser
browser.on('exitFullScreen', () => {
console.log('Window exited full screen mode')
})
‘focused’
on(event: 'focused', listener: () => void): void;
off(event: 'focused', listener: () => void): void;
Emitted when the browser’s web content is focused.
Example
import { app, Browser } from '@mobrowser/api';
const win = app.createWindow()
const browser: Browser = win.browser
browser.on('focused', () => {
console.log('Browser is focused')
})
### 'blurred'
```ts
on(event: 'blurred', listener: () => void): void;
off(event: 'blurred', listener: () => void): void;
Emitted when the browser’s web content loses focus.
Example
import { app, Browser } from '@mobrowser/api';
const win = app.createWindow()
const browser: Browser = win.browser
browser.on('blurred', () => {
console.log('Browser is blurred')
})
### 'closed'
```ts
on(event: 'closed', listener: () => void): void;
off(event: 'closed', listener: () => void): void;
Emitted when the browser is closed. After you receive this event, the browser object will be destroyed, so you should remove any references to it and avoid using it.
Example
import { app, Browser } from '@mobrowser/api';
const win = app.createWindow()
const browser: Browser = win.browser
browser.on('closed', () => {
console.log('Browser is closed')
})
browser.close()
Handlers
alert
handle(action: 'alert', handler: (params: AlertParams) => Promise<AlertAction>): void;
removeHandler(action: 'alert'): void;
Invoked when the browser is about to show an alert.
handler: The handler function to be invoked when the action is triggered.
Example
import { app, AlertParams } from '@mobrowser/api';
const win = app.createWindow()
win.browser.handle('alert', async (params: AlertParams) => {
await app.showMessageDialog({
parentWindow: win,
title: params.title,
message: params.message,
buttons: [{ label: params.labelOk, type: 'primary' }]
})
return 'ok'
})
confirm
handle(action: 'confirm', handler: (params: ConfirmParams) => Promise<ConfirmAction>): void;
removeHandler(action: 'confirm'): void;
Invoked when the browser is about to show a confirm dialog.
handler: The handler function to be invoked when the action is triggered.
Example
import { app, ConfirmParams } from '@mobrowser/api';
const win = app.createWindow()
win.browser.handle('confirm', async (params: ConfirmParams) => {
const result = await app.showMessageDialog({
parentWindow: win,
title: params.title,
message: params.message,
buttons: [{ label: params.labelOk, type: 'primary' }]
})
return result.button.type === 'primary' ? 'ok' : 'cancel'
})
prompt
handle(action: 'prompt', handler: (params: PromptParams) => Promise<PromptAction>): void;
removeHandler(action: 'prompt'): void;
Invoked when the browser is about to show a prompt dialog.
handler: The handler function to be invoked when the action is triggered.
Example
import { app, PromptParams } from '@mobrowser/api';
const win = app.createWindow()
win.browser.handle('prompt', async (params: PromptParams) => {
const result = await app.showMessageDialog({
parentWindow: win,
title: params.title,
message: params.message,
buttons: [
{ label: params.labelOk, type: 'primary' },
{ label: params.labelCancel, type: 'secondary' }
],
textFields: [{
placeholder: 'Your name',
type: 'text',
value: params.defaultValue
}]
})
if (result.button.type === 'primary') {
return { action: 'ok', value: result.textFieldValues?.[0] ?? '' }
}
return 'cancel'
})
openPopup
handle(action: 'openPopup', handler: (params: OpenPopupParams) => OpenPopupAction): void;
removeHandler(action: 'openPopup'): void;
Invoked when the browser is about to open a popup window.
handler: The handler function to be invoked when the action is triggered.
Example
import { app, OpenPopupParams } from '@mobrowser/api';
const win = app.createWindow()
win.browser.handle('openPopup', async (params: OpenPopupParams) => {
return 'open'
})
showContextMenu
handle(action: 'showContextMenu', handler: (params: ShowContextMenuParams) => ContextMenu | 'suppress'): void;
removeHandler(action: 'showContextMenu'): void;
Invoked when the browser is about to show a context menu.
handler: The handler function to be invoked when the action is triggered.
Example
import { app, ShowContextMenuParams } from '@mobrowser/api';
const win = app.createWindow()
win.browser.handle('showContextMenu', async (params: ShowContextMenuParams) => {
if (params.contentType === 'page') {
return new ContextMenu({
items: [
'goBack',
'goForward',
'reload',
'separator',
'savePageAs',
'print',
'separator',
'inspectElement',
]
})
}
return 'suppress'
})
startDownload
handle(action: 'startDownload', handler: (params: StartDownloadParams) => Promise<StartDownloadAction>): void;
removeHandler(action: 'startDownload'): void;
Invoked when the browser is about to start a download.
handler: The handler function to be invoked when the action is triggered.
Example
import { app, StartDownloadParams } from '@mobrowser/api';
const win = app.createWindow()
win.browser.handle('startDownload',
async (params: StartDownloadParams) => {
return 'start'
})
win.browser.downloadUrl('https://example.com/file.zip')
close
handle(action: 'close', handler: () => Promise<CloseAction>): void;
removeHandler(action: 'close'): void;
Invoked when the browser is about to close.
handler: The handler function to be invoked when the action is triggered.
Example
import { app, CloseAction } from '@mobrowser/api';
const win = app.createWindow()
win.browser.handle('close', async () => {
return 'close'
})
win.browser.close()