目录

App

Provides methods to manage application lifecycle.

import { app } from '@mobrowser/api';

Example 

import { app } from '@mobrowser/api';

console.log(app.name)
console.log(app.version)

Properties 

packaged 

readonly packaged: boolean;

Indicates whether the application is packaged (i.e., running in production mode) or running in development mode.

Example 

import { app } from '@mobrowser/api';

if (app.packaged) {
  console.log('Running in production')
}

url 

readonly url: string;

The URL of the application frontend entry point.

It can be different based on whether the application is running in the development or production mode. The value for each mode is configured in the mobrowser.conf.json file in the configurations section.

Example 

import { app, BrowserWindow } from '@mobrowser/api';

const win = new BrowserWindow()
win.browser.loadUrl(app.url)

name 

readonly name: string;

The application name defined in the mobrowser.conf.json file.

version 

readonly version: string;

The application version defined in the mobrowser.conf.json file.

description 

readonly description: string;

The application description defined in the mobrowser.conf.json file.

readonly copyright: string;

The application copyright information defined in the mobrowser.conf.json file.

launchInfo 

readonly launchInfo: LaunchInfo;

Contains information about the application launch, including whether it is the first run and the version that was previously launched.

Example 

import { app } from '@mobrowser/api';

if (app.launchInfo.isFirstRun) {
  console.log('Welcome!')
}

windows 

readonly windows: BrowserWindow[];

An array of all currently open browser windows. This array is updated automatically as windows are created and closed.

Example 

import { app, BrowserWindow } from '@mobrowser/api';

const win = new BrowserWindow()
win.setTitle('MyApp')
app.windows.forEach(win => console.log(win.title))

theme 

readonly theme: Theme;

The current application theme.

Example 

import { app, BrowserWindow } from '@mobrowser/api';

const win = new BrowserWindow()
if (app.theme === 'dark') {
  win.setTitle('App (Dark Mode)')
}
readonly menu: Menu;

The application main menu.

trustedOrigins 

readonly trustedOrigins: string[];

Origins that are allowlisted for automatic permission grants without invoking the requestPermissions handler. This includes the following permissions:

  • camera access
  • display-capture
  • microphone access
  • notifications
  • persistent-storage access
  • clipboard read and write access
  • geolocation access

To grant permissions for additional origins without handling each request, add entries to app.trustedOrigins in mobrowser.conf.json. Each entry must be an origin or hostname pattern up to eTLD+1. For example, "http://foo.com", "http://foo.com:8000", "*.foo.com", "*.foo.*.bar.com", and "http://*.foo.bar.com", but not "*.co.uk", "*.com", or "test.*.com". Hostname patterns must include a wildcard somewhere, so "test.com" alone is not a valid pattern, and wildcards may only replace full hostname components ("test*.foo.com" is not valid).

Example 

"trustedOrigins": [ "https://foo.com", "*.foo.com", "https://*.foo.bar.com" ],

Methods 

setMenu() 

setMenu(menu: Menu): void;

Sets the application main menu.

ParameterTypeDescription
menuMenuThe menu to set as the application main menu.

Example 

import { app } from '@mobrowser/api';

app.setMenu(new Menu({
  items: [
    new MenuItem({
      id: 'quit',
      label: 'Quit',
      action: (item: MenuItem) => {
        app.quit()
      }
    })
  ]
}))

setTheme() 

setTheme(theme: Theme): void;

Sets the application theme.

ParameterTypeDescription
themeThemeThe theme to set.

Example 

import { app } from '@mobrowser/api';

// Set the dark theme
app.setTheme('dark')

// Set the light theme
app.setTheme('light')

// Set the theme of the operating system
app.setTheme('system')

showMessageDialog() 

showMessageDialog(options: MessageDialogOptions): Promise<MessageDialogResult>;

Opens a message dialog with the given options.

ParameterTypeDescription
optionsMessageDialogOptionsThe options to use for opening the message dialog.

Return value 

The result of the message dialog as a promise that resolves to a MessageDialogResult object.

Example 

import { app, BrowserWindow } from '@mobrowser/api';

// Create a new window.
const win = new BrowserWindow()
win.setTitle('MyApp')
win.show()

// Show a message dialog.
const result = await app.showMessageDialog({
  parentWindow: win,
  title: 'Would you like to delete this conversation?',
  message: 'This conversation will be deleted from all of your devices. ' +
    'You cannot undo this action.',
  buttons: [
    { label: 'Cancel', type: 'secondary' },
    { label: 'Delete', type: 'primary' }
  ]
})

if (result.button.type === 'primary') {
  // The 'Delete' button was clicked.
}

showOpenDialog() 

showOpenDialog(options: OpenDialogOptions): Promise<OpenDialogResult>;

Opens a file open dialog with the given options.

ParameterTypeDescription
optionsOpenDialogOptionsThe options to use for opening the file open dialog.

Return value 

The result of the file open dialog as a promise that resolves to a OpenDialogResult object.

Example 

import { app, BrowserWindow } from '@mobrowser/api';

// Create a new window.
const win = new BrowserWindow()
win.setTitle('MyApp')
win.show()

// Show a file open dialog.
const result = await app.showOpenDialog({
 parentWindow: win,
 title: 'Open Files',
 defaultPath: '/Users/john/Desktop',
 selectionPolicy: 'files',
 features: {
   allowMultiple: true,
   canCreateDirectories: true
 },
})

if (!result.canceled) {
  console.log('Selected paths: ', result.paths[0])
}

showSaveDialog() 

showSaveDialog(options: SaveDialogOptions): Promise<SaveDialogResult>;

Opens a file save dialog with the given options.

ParameterTypeDescription
optionsSaveDialogOptionsThe options to use for opening the file save dialog.

Return value 

The result of the file save dialog as a promise that resolves to a SaveDialogResult object.

Example 

import { app, BrowserWindow } from '@mobrowser/api';

// Create a new window.
const win = new BrowserWindow()
win.setTitle('MyApp')
win.show()

// Show a file save dialog.
const result = await app.showSaveDialog({
  parentWindow: win,
  title: 'Save File',
  defaultPath: '/Users/john/Desktop',
  filters: [{ name: 'Text Files', extensions: ['txt'] }]
})

if (!result.canceled) {
  console.log('Selected path: ', result.path)
}

getPath() 

getPath(name: PathName): FilePath;

Gets the absolute path to a directory identified by the given name.

ParameterTypeDescription
namePathNameIdentifies which path should be retrieved.

Return value 

The absolute path to the directory or an empty string if the path is not found.

Example 

import { app } from '@mobrowser/api';

// Get the path to the app resources directory.
const dirPath = app.getPath('appResources')
console.log(dirPath)

quit() 

quit(): void;

Quits the application, closing all windows and terminating the process.

Example 

import { app, BrowserWindow } from '@mobrowser/api';

const win = new BrowserWindow()
win.show()
const result = await app.showMessageDialog({
  parentWindow: win,
  title: 'Quit',
  message: 'Are you sure you want to quit?',
  buttons: [
    { label: 'Cancel', type: 'secondary' },
    { label: 'Quit', type: 'primary' }
  ]
})
if (result.button.type === 'primary') {
  app.quit()
}

restart() 

restart(): void;

Restarts the application, closing all windows and terminating the process.

The new application instance will use the same working directory and the command line arguments as the current one.

Example 

import { app, BrowserWindow } from '@mobrowser/api';

const win = new BrowserWindow()
win.show()
const result = await app.showMessageDialog({
  parentWindow: win,
  title: 'Restart',
  message: 'Restart to apply the new settings?',
  buttons: [
    { label: 'Later', type: 'secondary' },
    { label: 'Restart', type: 'primary' }
  ]
})
if (result.button.type === 'primary') {
  app.restart()
}

checkForUpdate() 

checkForUpdate(source: string): Promise<AppUpdate | undefined | string>;

Checks for an available application update.

Available only on Windows and macOS.

ParameterTypeDescription
sourcestringa web server with files for application updates.

Return value 

A promise that resolves to the application update object if an update is available, or undefined if no update is available, or a string containing the error message if an error occurs.

Example 

import { app, AppUpdate } from '@mobrowser/api';

const result = await app.checkForUpdate('https://app.com/updates')
if (!result) {
  // No update is available or do nothing.
} else if (typeof result === 'string') {
  // An error occurred while checking for updates.
} else {
  // An update is available.
  const appUpdate: AppUpdate = result
  console.log('Update available: ', appUpdate.version)
}

Events 

‘activated’ 

on(event: 'activated', listener: () => void): void;
off(event: 'activated', listener: () => void): void;

Emitted when the application is activated. On macOS, this event can be triggered by various actions, such as clicking the Dock icon or switching to the application using Cmd+Tab. On Windows and Linux, this event is not triggered because these platforms do not have the same concept of application activation.

Example 

import { app, BrowserWindow } from '@mobrowser/api';

app.on('activated', () => {
  if (app.windows.length === 0) {
    const win = new BrowserWindow({
      size: { width: 800, height: 650 },
    })
    win.browser.loadUrl(app.url)
    win.show()
  }
})