- app
- app
- 事件
- 事件: ‘will-finish-launching’
- 事件: ‘ready’
- 事件: ‘window-all-closed’
- 事件:’before-quit’
- 事件: ‘will-quit’
- 事件: ‘quit’
- 事件: ‘open-file’ macOS
- 事件: ‘open-url’ macOS
- 事件: ‘activate’ macOS
- 事件: ‘continue-activity’ macOS
- 事件: ‘will-continue-activity’ macOS
- 事件: ‘continue-activity-error’ macOS
- 事件: ‘activity-was-continued’ macOS
- 事件: ‘update-activity-state’ macOS
- 事件: ‘new-window-for-tab’ macOS
- 事件: ‘browser-window-blur’
- 事件: ‘browser-window-focus’
- 事件: ‘browser-window-created’
- 事件: ‘web-contents-created’
- 事件: ‘certificate-error’
- 事件: ‘select-client-certificate’
- 事件: “login”
- Event: ‘gpu-info-update’
- Event: ‘gpu-process-crashed’
- Event: ‘renderer-process-crashed’
- 事件: “accessibility-support-changed” macOS Windows
- 事件:’session-created’
- 事件: ‘second-instance’
- 事件: ‘desktop-capturer-get-sources’
- 事件: ‘remote-require’
- 事件: ‘remote-get-global’
- 事件
app
app
控制应用程序的事件生命周期。
进程:主进程
下面的这个例子将会展示如何在最后一个窗口被关闭时退出应用:
const { app } = require('electron')app.on('window-all-closed', () => { app.quit()})
事件
app 对象会发出以下事件:
事件: ‘will-finish-launching’
当应用程序完成基础的启动的时候被触发。 在 Windows 和 Linux 中, will-finish-launching 事件与 ready 事件是相同的; 在 macOS 中,这个事件相当于 NSApplication 中的 applicationWillFinishLaunching 提示。 通常会在这里为 open-file 和 open-url 设置监听器,并启动崩溃报告和自动更新。
绝大部分情况下,你必须在ready事件句柄中处理所有事务。
事件: ‘ready’
返回:
- launchInfo unknown macOS
当 Electron 完成初始化时被触发。 在 macOS 中, 如果从通知中心中启动,那么 launchInfo 中的 userInfo 包含用来打开应用程序的 NSUserNotification 信息。 你可以通过调用 app.isReady() 方法来检查此事件是否已触发。
事件: ‘window-all-closed’
当所有的窗口都被关闭时触发。
如果你没有监听此事件并且所有窗口都关闭了,默认的行为是控制退出程序;但如果你监听了此事件,你可以控制是否退出程序。 如果用户按下了 Cmd + Q,或者开发者调用了 app.quit(),Electron 会首先关闭所有的窗口然后触发 will-quit 事件,在这种情况下 window-all-closed 事件不会被触发。
事件:’before-quit’
返回:
- event Event
Emitted before the application starts closing its windows. Calling event.preventDefault() will prevent the default behavior, which is terminating the application.
Note: If application quit was initiated by autoUpdater.quitAndInstall(), then before-quit is emitted after emitting close event on all windows and closing them.
注:在 Windows 系统中,如果应用程序因系统关机/重启或用户注销而关闭,那么这个事件不会被触发。
事件: ‘will-quit’
返回:
- event Event
Emitted when all windows have been closed and the application will quit. Calling event.preventDefault() will prevent the default behaviour, which is terminating the application.
关于 window-all-closed 和 will-quit 事件之间的差异, 请参见 window-all-closed 事件的说明。
注:在 Windows 系统中,如果应用程序因系统关机/重启或用户注销而关闭,那么这个事件不会被触发。
事件: ‘quit’
返回:
-
event Event
-
exitCode Integer
在应用程序退出时发出。
注:在 Windows 系统中,如果应用程序因系统关机/重启或用户注销而关闭,那么这个事件不会被触发。
事件: ‘open-file’ macOS
返回:
-
event Event
-
path String
当用户想要在应用中打开一个文件时发出。 open-file 事件通常在应用已经打开,并且系统要再次使用该应用打开文件时发出。 open-file也会在一个文件被拖到 dock 并且还没有运行的时候发出。 请确认在应用启动的时候(甚至在 ready 事件发出前) 就对 open-file 事件进行监听。
如果你想处理这个事件,你应该调用 event.preventDefault() 。
在 Windows 系统中,你需要解析 process.argv (在主进程中) 来获取文件路径
事件: ‘open-url’ macOS
返回:
-
event Event
-
url String
当用户想要在应用中打开一个 URL 时发出。 Your application’s Info.plist file must define the URL scheme within the CFBundleURLTypes key, and set NSPrincipalClass to AtomApplication.
如果你想处理这个事件,你应该调用 event.preventDefault() 。
事件: ‘activate’ macOS
返回:
-
event Event
-
hasVisibleWindows Boolean
当应用被激活时发出。 各种操作都可以触发此事件, 例如首次启动应用程序、尝试在应用程序已运行时或单击应用程序的坞站或任务栏图标时重新激活它。
事件: ‘continue-activity’ macOS
返回:
-
event Event
-
typeString-标识活动的字符串。 映射到 NSUserActivity. activityType。
-
userInfo unknown - Contains app-specific state stored by the activity on another device.
当来自不同设备的活动通过 Handoff 想要恢复时触发。 如果你想处理这个事件,你应该调用 event.preventDefault() 。
只有具有支持相应的活动类型并且相同的开发团队 ID 作为启动程序时,用户行为才会进行。 所支持活动类型已在应用的 Info.plist 中的 NSUserActivityTypes 里明确定义。
事件: ‘will-continue-activity’ macOS
返回:
-
event Event
-
typeString-标识活动的字符串。 映射到 NSUserActivity. activityType。
当来自不同设备的活动通过 Handoff 恢复之前触发。 如果你想处理这个事件,你应该调用 event.preventDefault() 。
事件: ‘continue-activity-error’ macOS
返回:
-
event Event
-
typeString-标识活动的字符串。 映射到 NSUserActivity. activityType。
-
error String - 详细的错误信息
当来自不同设备的活动通过 Handoff 恢复失败时触发。
事件: ‘activity-was-continued’ macOS
返回:
-
event Event
-
typeString-标识活动的字符串。 映射到 NSUserActivity. activityType。
-
userInfo unknown - Contains app-specific state stored by the activity.
当来自不同设备的活动通过 Handoff 成功恢复后触发。
事件: ‘update-activity-state’ macOS
返回:
-
event Event
-
typeString-标识活动的字符串。 映射到 NSUserActivity. activityType。
-
userInfo unknown - Contains app-specific state stored by the activity.
当 Handoff 即将通过另一个设备恢复时触发。 如果需要更新要传输的状态, 应立即调用 事件. preventDefault (), 构造新的 用户信息 字典, 并及时调用 应用程序 updateCurrentActiviy ()。 否则,操作会失败,并且触发 continue-activity-error
事件: ‘new-window-for-tab’ macOS
返回:
- event Event
Emitted when the user clicks the native macOS new tab button. The new tab button is only visible if the current BrowserWindow has a tabbingIdentifier
事件: ‘browser-window-blur’
返回:
-
event Event
-
window BrowserWindow
在 browserWindow 失去焦点时发出。
事件: ‘browser-window-focus’
返回:
-
event Event
-
window BrowserWindow
在 browserWindow 获得焦点时发出。
事件: ‘browser-window-created’
返回:
-
event Event
-
window BrowserWindow
在创建新的 browserWindow 时发出。
事件: ‘web-contents-created’
返回:
-
event Event
-
webContents WebContents
在创建新的 webContents 时发出。
事件: ‘certificate-error’
返回:
-
event Event
-
webContents WebContents
-
url String
-
error String - 错误码
-
certificate 证书
-
callback Function
-
isTrustedBoolean-是否将证书视为可信的
当对 url 的 certificate 证书验证失败的时候发出。如果需要信任这个证书,你需要阻止默认行为 event.preventDefault() 并且调用 callback(true)。
const { app } = require('electron')app.on('certificate-error', (event, webContents, url, error, certificate, callback) => { if (url === 'https://github.com') { // Verification logic. event.preventDefault() callback(true) } else { callback(false) }})
事件: ‘select-client-certificate’
返回:
-
event Event
-
webContents WebContents
-
url URL
-
certificateList 证书[]
-
callback Function
-
certificate 证书 (可选)
当一个客户证书被请求的时候发出。
url 指的是请求客户端认证的网页地址,调用 callback 时需要传入一个证书列表中的证书。 需要通过调用 event.preventDefault() 来防止应用自动使用第一个证书进行验证。
const { app } = require('electron')app.on('select-client-certificate', (event, webContents, url, list, callback) => { event.preventDefault() callback(list[0])})
事件: “login”
返回:
-
event Event
-
webContents WebContents
-
authenticationResponseDetails Object
-
url URL
-
authInfo Object
-
isProxy Boolean
-
scheme String
-
host String
-
port Integer
-
realm String
-
callback Function
-
username String (optional)
-
password String (optional)
当 webContents 要进行基本身份验证时触发。
默认行为是取消所有身份验证。 默认行为是取消所有的验证行为,如果需要重写这个行为,你需要用 event.preventDefault() 来阻止默认行为,并且使用 callback(username, password) 来验证。
const { app } = require('electron')app.on('login', (event, webContents, details, authInfo, callback) => { event.preventDefault() callback('username', 'secret')})
If callback is called without a username or password, the authentication request will be cancelled and the authentication error will be returned to the page.
Event: ‘gpu-info-update’
Emitted whenever there is a GPU info update.
Event: ‘gpu-process-crashed’
返回:
-
event Event
-
killed Boolean
Emitted when the GPU process crashes or is killed.
Event: ‘renderer-process-crashed’
返回:
-
event Event
-
webContents WebContents
-
killed Boolean
Emitted when the renderer process of webContents crashes or is killed.
事件: “accessibility-support-changed” macOS Windows
返回:
-
event Event
-
accessibilitySupportEnabled当启用了 Chrome 的辅助功能时为 true, 其他情况为 false。
当 Chrome 的辅助功能状态改变时触发。 当启用或禁用辅助技术时将触发此事件,例如屏幕阅读器 。 查看更多详情 https://www.chromium.org/developers/design-documents/accessibility
事件:’session-created’
返回:
- session Session
当 Electron创建了一个新的 session后被触发.
const { app } = require('electron')app.on('session-created', (session) => { console.log(session)})
事件: ‘second-instance’
返回:
-
event Event
-
argv String[] - 第二个实例的命令行参数数组
-
workingDirectory String - 第二个实例的工作目录
当第二个实例被执行并且调用 app.requestSingleInstanceLock() 时,这个事件将在你的应用程序的首个实例中触发
argv 是第二个实例的命令行参数的数组, workingDirectory 是这个实例当前工作目录。 通常, 应用程序会激活窗口并且取消最小化来响应。
保证在 app 的 ready 事件发出后发出此事件。
注意: 额外命令行参数可能由 Chromium 添加, ,例如 --original-process-start-time。
事件: ‘desktop-capturer-get-sources’
返回:
-
event Event
-
webContents WebContents
Emitted when desktopCapturer.getSources() is called in the renderer process of webContents. Calling event.preventDefault() will make it return empty sources.
事件: ‘remote-require’
返回:
-
event Event
-
webContents WebContents
-
moduleName String
在 webContents 的渲染器进程中调用 remote.require() 时发出。 调用 event.preventDefault() 将阻止模块返回。 可以通过设置 event.returnValue 返回自定义值。
事件: ‘remote-get-global’
返回:
-
event Event
-
webContents WebContents
-
globalName String
