- app·续1
- 事件: ‘remote-get-builtin’
- 事件: ‘remote-get-current-window’
- 事件: ‘remote-get-current-web-contents’
- 事件: ‘remote-get-guest-web-contents’
- 方法
- app.quit()
- app.exit([exitCode])
- app.relaunch([options])
- app.isReady()
- app.whenReady()
- app.focus()
- app.hide() macOS
- app.show() macOS
- app.setAppLogsPath([path])
- app.getAppPath()
- app.getPath(name)
- app.getFileIcon(path[, options])
- app.setPath(name, path)
- app.getVersion()
- app.getName()
- app.setName(name)
- app.getLocale()
- app.getLocaleCountryCode()
- app.addRecentDocument(path) macOS Windows
- app.clearRecentDocuments() macOS Windows
- app.setAsDefaultProtocolClient(protocol[, path, args])
- app.removeAsDefaultProtocolClient(protocol[, path, args]) macOS Windows
- app.isDefaultProtocolClient(protocol[, path, args])
app·续1
在 webContents 的渲染器进程中调用 remote.getGlobal() 时发出。 调用 event.preventDefault() 将阻止全局返回。 可以通过设置 event.returnValue 返回自定义值。
事件: ‘remote-get-builtin’
返回:
-
event Event
-
webContents WebContents
-
moduleName String
在 webContents 的渲染器进程中调用 remote.getBuiltin() 时发出。 调用 event.preventDefault() 将阻止模块返回。 可以通过设置 event.returnValue 返回自定义值。
事件: ‘remote-get-current-window’
返回:
-
event Event
-
webContents WebContents
在 webContents 的渲染器进程中调用 remote.getCurrentWindow() 时发出。 调用 event.preventDefault() 将阻止对象返回 可以通过设置 event.returnValue 返回自定义值。
事件: ‘remote-get-current-web-contents’
返回:
-
event Event
-
webContents WebContents
在 webContents 的渲染器进程中调用 remote.getCurrentWebContents() 时发出。 调用 event.preventDefault() 将阻止对象返回 可以通过设置 event.returnValue 返回自定义值。
事件: ‘remote-get-guest-web-contents’
返回:
-
event Event
-
webContents WebContents
-
guestWebContents WebContents
在webContents的渲染进程中调用getWebContents时触发 调用 event.preventDefault() 将阻止对象返回 可以通过设置 event.returnValue 返回自定义值。
方法
app 对象具有以下方法:
注意: 某些方法仅在特定的操作系统上可用, 这些方法会被标记出来。
app.quit()
尝试关闭所有窗口 将首先发出 before-quit 事件。 如果所有窗口都已成功关闭, 则将发出 will-quit 事件, 并且默认情况下应用程序将终止。
此方法会确保执行所有beforeunload 和 unload事件处理程序。 可以在退出窗口之前的beforeunload事件处理程序中返回false取消退出。
app.exit([exitCode])
- exitCode Integer (可选)
Exits immediately with exitCode. exitCode defaults to 0.
所有窗口都将立即被关闭,而不询问用户,而且 before-quit 和 will-quit 事件也不会被触发。
app.relaunch([options])
-
options Object (optional)
-
args String[] (可选)
-
execPath String (可选)
从当前实例退出,重启应用。
默认情况下,新的实例将会使用和当前实例相同的工作目录以及命令行参数。 当设置了 args 参数时, args 将作为命令行参数传递。 当设置了 execPath ,execPath 将被执行以重新启动,而不是当前的应用程序。
请注意, 此方法在执行时不会退出当前的应用程序, 你需要在调用 app.relaunch 方法后再执行 app. quit 或者 app.exit 来让应用重启。
当 app.relaunch 被多次调用时,多个实例将在当前实例退出后启动。
立即重启当前实例并向新的实例添加新的命令行参数的示例:
const { app } = require('electron')app.relaunch({ args: process.argv.slice(1).concat(['--relaunch']) })app.exit(0)
app.isReady()
返回 Boolean 类型 - 如果 Electron 已经完成初始化,则返回 true, 其他情况为 false
app.whenReady()
返回 Promise<void> - 当Electron 初始化完成。 可用作检查 app.isReady() 的方便选择,假如应用程序尚未就绪,则订阅ready事件。
app.focus()
On Linux, focuses on the first visible window. On macOS, makes the application the active app. On Windows, focuses on the application’s first window.
app.hide() macOS
隐藏所有的应用窗口,不是最小化.
app.show() macOS
Shows application windows after they were hidden. Does not automatically focus them.
app.setAppLogsPath([path])
- path String (optional) - A custom path for your logs. Must be absolute.
Sets or creates a directory your app’s logs which can then be manipulated with app.getPath() or app.setPath(pathName, newPath).
Calling app.setAppLogsPath() without a path parameter will result in this directory being set to ~/Library/Logs/YourAppName on macOS, and inside the userData directory on Linux and Windows.
app.getAppPath()
返回 String 类型 - 当前应用程序所在目录
app.getPath(name)
-
name String - You can request the following paths by the name:
-
home 用户的 home 文件夹(主目录)
-
appData Per-user application data directory, which by default points to:
-
%APPDATA% Windows 中
-
$XDG_CONFIG_HOME or ~/.config Linux 中
-
~/Library/Application Support macOS 中
-
userData 储存你应用程序设置文件的文件夹,默认是 appData 文件夹附加应用的名称
-
缓存
-
temp 临时文件夹
-
exe当前的可执行文件
-
module The libchromiumcontent 库
-
desktop 当前用户的桌面文件夹
-
documents 用户文档目录的路径
-
downloads 用户下载目录的路径
-
music 用户音乐目录的路径
-
pictures 用户图片目录的路径
-
videos 用户视频目录的路径
-
logs应用程序的日志文件夹
-
pepperFlashSystemPlugin Pepper Flash 插件的系统版本的完成路径。
Returns String - A path to a special directory or file associated with name. On failure, an Error is thrown.
If app.getPath('logs') is called without called app.setAppLogsPath() being called first, a default log directory will be created equivalent to calling app.setAppLogsPath() without a path parameter.
app.getFileIcon(path[, options])
-
path String
-
options Object (optional)
-
size String
-
small - 16x16
-
normal - 32x32
-
large - 48x48 on Linux, 32x32 on Windows, unsupported on macOS.
返回 Promise<NativeImage> - 完成后返回当前应用的图标, 类型是 NativeImage.
读取文件的关联图标。
在 Windows 上, 会有两种图标:
-
与某些文件扩展名相关联的图标, 比如 . mp3 ,. png 等。
-
文件本身就带图标,像是 .exe, .dll, .ico
在 Linux 和 macOS 系统中,图标取决于和应用程序绑定的 文件 mime 类型
app.setPath(name, path)
-
name 字符串
-
path String
重写 name 的路径为 path,一个特定的文件夹或者文件。 If the path specifies a directory that does not exist, an Error is thrown. In that case, the directory should be created with fs.mkdirSync or similar.
name 参数只能使用 app.getPath 定义过的 name
默认情况下, 网页的 cookie 和缓存将存储在 userData 目录下。 如果要更改这个位置, 你需要在 app 模块中的 ready 事件被触发之前重写 userData 的路径。
app.getVersion()
返回 String-加载的应用程序的版本。 如果应用程序的 package. json 文件中找不到版本号, 则返回当前包或者可执行文件的版本。
app.getName()
返回 String-当前应用程序的名称, 它是应用程序的 package. json 文件中的名称。
Usually the name field of package.json is a short lowercase name, according to the npm modules spec. 通常还应该指定一个 productName 字段, 是首字母大写的完整名称,用于表示应用程序的名称。Electron 会优先使用这个字段作为应用名。
过时的
app.setName(name)
- name 字符串
设置当前应用程序的名字
Note: This function overrides the name used internally by Electron; it does not affect the name that the OS uses.
过时的
app.getLocale()
Returns String - The current application locale. Possible return values are documented here.
要设置区域,则需要在应用启动时使用命令行时打开开关,你可以在这里找到。
注意: 分发打包的应用程序时, 你必须指定 locales 文件夹。
注意: 在 Windows 上,你必须得等 ready 事件触发之后,才能调用该方法
app.getLocaleCountryCode()
Returns String - User operating system’s locale two-letter ISO 3166 country code. The value is taken from native OS APIs.
注意: 当无法检测本地国家代码时,它返回空字符串。
app.addRecentDocument(path) macOS Windows
- path String
将此 path 添加到最近打开的文件列表中
This list is managed by the OS. On Windows, you can visit the list from the task bar, and on macOS, you can visit it from dock menu.
app.clearRecentDocuments() macOS Windows
清空最近打开的文档列表
app.setAsDefaultProtocolClient(protocol[, path, args])
-
protocol String - 协议的名称, 不包含 ://。 For example, if you want your app to handle electron:// links, call this method with electron as the parameter.
-
path String (optional) Windows - The path to the Electron executable. Defaults to process.execPath
-
args String[] (optional) Windows - Arguments passed to the executable. Defaults to an empty array
返回 Boolean-是否成功调用。
Sets the current executable as the default handler for a protocol (aka URI scheme). It allows you to integrate your app deeper into the operating system. Once registered, all links with your-protocol:// will be opened with the current executable. The whole link, including protocol, will be passed to your application as a parameter.
Note: On macOS, you can only register protocols that have been added to your app’s info.plist, which cannot be modified at runtime. However, you can change the file during build time via Electron Forge, Electron Packager, or by editing info.plist with a text editor. 有关详细信息,请参阅 Apple’s documentation
Note: In a Windows Store environment (when packaged as an appx) this API will return true for all calls but the registry key it sets won’t be accessible by other applications. In order to register your Windows Store application as a default protocol handler you must declare the protocol in your manifest.
The API uses the Windows Registry and LSSetDefaultHandlerForURLScheme internally.
app.removeAsDefaultProtocolClient(protocol[, path, args]) macOS Windows
-
protocol String - 协议的名称, 不包含 ://。
-
pathString (可选) Windows -默认为 process.execPath
-
args String[] (可选) Windows - 默认为空数组
返回 Boolean-是否成功调用。
This method checks if the current executable as the default handler for a protocol (aka URI scheme). If so, it will remove the app as the default handler.
app.isDefaultProtocolClient(protocol[, path, args])
-
protocol String - 协议的名称, 不包含 ://。
-
pathString (可选) Windows -默认为 process.execPath
-
args String[] (可选) Windows - 默认为空数组
Returns Boolean - Whether the current executable is the default handler for a protocol (aka URI scheme).
