应用


App.argv

获取启动应用程序时过滤后的命令行参数。在 NW.js 中,一些命令行参数由 NW.js 使用,您的应用程序不应该关心它们。App.argv 将过滤掉这些参数并返回剩余的参数。您可以从 App.filteredArgv 获取过滤后的模式,从 App.fullArgv 获取完整参数。

App.fullArgv

获取启动应用程序时的所有命令行参数。返回值包含 NW.js 使用的参数,例如 --nwapp--remote-debugging-port 等。

App.filteredArgv

获取 App.argv 使用的过滤后的命令行参数模式列表。默认情况下,以下模式用于过滤参数

[
  /^--url=/,
  /^--remote-debugging-port=/,
  /^--renderer-cmd-prefix=/,
  /^--nwapp=/
]

App.startPath

获取应用程序启动的目录。应用程序将在启动后将当前目录更改为包文件所在的目录。

App.dataPath

获取应用程序在用户目录中的数据路径。

  • Windows: %LOCALAPPDATA%/<name>
  • Linux: ~/.config/<name>
  • OS X: ~/Library/Application Support/<name>/Default (在 v0.12.3 及以下版本中为 ~/Library/Application Support/<name>)

<name>package.json 清单中的 **name** 字段。

App.manifest

获取清单文件的 JSON 对象。

App.clearCache()

清除内存中的 HTTP 缓存和磁盘上的 HTTP 缓存。此方法调用是同步的。

App.clearAppCache(manifest_url)

将清单 URL 指定的应用程序缓存组标记为已过时。此方法调用是同步的。

App.closeAllWindows()

close 事件发送到当前应用程序的所有窗口。如果没有任何窗口阻止 close 事件,则在所有窗口完成关闭后,应用程序将退出。使用此方法退出应用程序将使窗口有机会保存数据。

App.crashBrowser()

App.crashRenderer()

这两个函数分别使浏览器进程和渲染器进程崩溃,以测试 崩溃转储 功能。

App.enableComponent(component, callback)

实验性

此 API 处于实验阶段,可能会发生变化。

  • component {String} 组件 ID;目前仅支持 WIDEVINE
  • callback function(version) 启用组件后的回调;version 字符串参数是已启用组件的版本。'0.0.0.0' 表示未安装。使用 App.updateComponent() 安装它。

App.getProxyForURL(url)

  • url {String} 要查询代理的 URL

查询用于在 DOM 中加载 url 的代理。返回值与 PAC 中使用的格式相同(例如,“DIRECT”、“PROXY localhost:8080”)。

App.setProxyConfig(config, pac_url)

  • config {String} 代理规则
  • pac_url {String} PAC URL

设置代理配置,Web 引擎将使用该配置来请求网络资源或 PAC URL 以自动检测代理。

规则(从 net/proxy/proxy_config.h 复制)

    // Parses the rules from a string, indicating which proxies to use.
    //
    //   proxy-uri = [<proxy-scheme>"://"]<proxy-host>[":"<proxy-port>]
    //
    //   proxy-uri-list = <proxy-uri>[","<proxy-uri-list>]
    //
    //   url-scheme = "http" | "https" | "ftp" | "socks"
    //
    //   scheme-proxies = [<url-scheme>"="]<proxy-uri-list>
    //
    //   proxy-rules = scheme-proxies[";"<scheme-proxies>]
    //
    // Thus, the proxy-rules string should be a semicolon-separated list of
    // ordered proxies that apply to a particular URL scheme. Unless specified,
    // the proxy scheme for proxy-uris is assumed to be http.
    //
    // Some special cases:
    //  * If the scheme is omitted from the first proxy list, that list applies
    //    to all URL schemes and subsequent lists are ignored.
    //  * If a scheme is omitted from any proxy list after a list where a scheme
    //    has been provided, the list without a scheme is ignored.
    //  * If the url-scheme is set to 'socks', that sets a fallback list that
    //    to all otherwise unspecified url-schemes, however the default proxy-
    //    scheme for proxy urls in the 'socks' list is understood to be
    //    socks4:// if unspecified.
    //
    // For example:
    //   "http=foopy:80;ftp=foopy2"  -- use HTTP proxy "foopy:80" for http://
    //                                  URLs, and HTTP proxy "foopy2:80" for
    //                                  ftp:// URLs.
    //   "foopy:80"                  -- use HTTP proxy "foopy:80" for all URLs.
    //   "foopy:80,bar,direct://"    -- use HTTP proxy "foopy:80" for all URLs,
    //                                  failing over to "bar" if "foopy:80" is
    //                                  unavailable, and after that using no
    //                                  proxy.
    //   "socks4://foopy"            -- use SOCKS v4 proxy "foopy:1080" for all
    //                                  URLs.
    //   "http=foop,socks5://bar.com -- use HTTP proxy "foopy" for http URLs,
    //                                  and fail over to the SOCKS5 proxy
    //                                  "bar.com" if "foop" is unavailable.
    //   "http=foopy,direct://       -- use HTTP proxy "foopy" for http URLs,
    //                                  and use no proxy if "foopy" is
    //                                  unavailable.
    //   "http=foopy;socks=foopy2   --  use HTTP proxy "foopy" for http URLs,
    //                                  and use socks4://foopy2 for all other
    //                                  URLs.

App.quit()

退出当前应用程序。此方法 **不会** 将 close 事件发送到窗口,应用程序将静默退出。

App.setCrashDumpDir(dir)

已弃用

此 API 自 0.11.0 版本起已弃用。

  • dir {String} 生成崩溃转储的路径

设置崩溃时保存小型转储文件的目录。有关更多信息,请参阅 崩溃转储

App.addOriginAccessWhitelistEntry(sourceOrigin, destinationProtocol, destinationHost, allowDestinationSubdomains)

  • sourceOrigin {String} 源代码来源。例如 http://github.com/
  • destinationProtocol {String} sourceOrigin 可以访问到的目标协议。例如 app
  • destinationHost {String} sourceOrigin 可以访问到的目标主机。例如 myapp
  • allowDestinationSubdomains {Boolean} 如果设置为 true,则允许 sourceOrigin 访问目标的子域。

向用于控制跨源访问的白名单添加一个条目。假设您要允许从 github.com 到应用程序页面的 HTTP 重定向,请使用以下类似内容

App.addOriginAccessWhitelistEntry('http://github.com/', 'chrome-extension', location.host, true);

使用与 App.addOriginAccessWhitelistEntry 完全相同的参数调用 App.removeOriginAccessWhitelistEntry 来执行相反的操作。

App.removeOriginAccessWhitelistEntry(sourceOrigin, destinationProtocol, destinationHost, allowDestinationSubdomains)

  • sourceOrigin {String} 源代码来源。例如 http://github.com/
  • destinationProtocol {String} sourceOrigin 可以访问到的目标协议。例如 app
  • destinationHost {String} sourceOrigin 可以访问到的目标主机。例如 myapp
  • allowDestinationSubdomains {Boolean} 如果设置为 true,则允许 sourceOrigin 访问目标的子域。

从用于控制跨域访问的白名单中删除一个条目。请参阅上面的 addOriginAccessWhitelistEntry

App.registerGlobalHotKey(shortcut)

  • shortcut {Shortcut} 要注册的 Shortcut 对象。

向系统注册一个全局键盘快捷键(也称为系统范围的热键)。

有关更多信息,请参阅 Shortcut

App.unregisterGlobalHotKey(shortcut)

  • shortcut {Shortcut} 要取消注册的 Shortcut 对象。

取消注册一个全局键盘快捷键。

有关更多信息,请参阅 Shortcut

App.updateComponent(component, callback)

实验性

此 API 处于实验阶段,可能会发生变化。

  • component {String} 组件 ID;目前仅支持 WIDEVINE
  • callback function(success) 组件更新后的回调;success 是一个布尔参数,表示更新结果。

事件: open(args)

  • args {String} 程序的完整命令行。

当用户使用您的应用程序打开文件时发出。

事件: reopen

这是一个 Mac 特定的功能。当用户点击已运行应用程序的 Dock 图标时,会发送此事件。