nw.js node-webkit系列(9)Native UI API App的使用

本节主要介绍Native UI API 的 App使用,APP类别的API 是针对当前正在运行的应用程序实例,即获取应用的相关信息,启动应用时的传参及应用运行过程中的底层控制等。

(一)例子

// Load native UI library
var gui = require('nw.gui');

// Print arguments
console.log(gui.App.argv);

// Quit current app
gui.App.quit();

// Get the name field in manifest
gui.App.manifest.name

(二)函数参考

argv

当启动应用时获得命令行参数。

在命令行启动程序:


运行结果如下:



fullArgv

获取所有命令行参数,博主暂时也不太理解,原文解释如下:

Get all the command line arguments when starting the app. Because node-webkit itself used switches like --no-sandbox and --process-per-tab, it would confuse the app when the switches were meant to be given to node-webkit, so App.argv just filtered such switches (arguments' precedence were kept). You can get the switches to be filtered with App.filteredArgv.


dataPath

获取应用在用户机器上的数据存储地址。不懂的系统有不同的路径:

Windows: %LOCALAPPDATA%/<name>; Linux: ~/.config/<name>; OSX: ~/Library/Application Support/<name> 

注 <name> 是在package.json中的name字段的值。


manifest

获取package.json中的配置信息


clearCache()

清除应用在磁盘和内存中的HTTP缓存


closeAllWindows()

关闭所有窗口,该方法会抛出close事件,用户可以监听colse事件阻止窗口关闭,如果用户不阻止该事件,则退出应用。如gui.Window.on('closed', function () { });


crashBrowser(), crashRenderer()

这两个方法是崩溃浏览器和崩溃渲染器,可以用来测试浏览器崩溃时数据转储的过程。

从node-webkit  0.8.0版本开始,如果应用崩溃,一个minidump 文件会被保存到磁盘,用以调试和寻找程序崩溃的原因。默认情况下,dump文件保存在系统的临时文件夹中,我们也可以通过api来设置dump文件的存放目录。以下是个版本系统的临时目录(转目录):
Linux: /tmp
Windows: System temporary directory
Mac: ~/Library/Breakpad/product name (product name is defined in .plist file in the application bundle)


setCrashDumpDir(dir)

设置转目录


getProxyForURL(url)

获得下载url地址时在DOM中使用的代理。返回值的格式和PAC相同(如“直接”,“本地:8080”)。


setProxyConfig(config)

设置代理配置。较复杂,详情请参考https://github.com/nwjs/nw.js/wiki/App


quit()

直接退出应用程序(不发出任何事件)。


addOriginAccessWhitelistEntry(sourceOrigin, destinationProtocol, destinationHost, allowDestinationSubdomains)

添加一个白名单。如允许HTTP重定向从github.com到你的应用程序的页面,使用这样的应用程序协议:

App.addOriginAccessWhitelistEntry('http://github.com/', 'app', 'myapp', true);

removeOriginAccessWhitelistEntry(sourceOrigin, destinationProtocol, destinationHost, allowDestinationSubdomains)

移除一个白名单。参数参考函数addOriginAccessWhitelistEntry。


registerGlobalHotKey(shortcut);

在系统上为应用注册快捷键。详细请参考https://github.com/nwjs/nw.js/wiki/Shortcut

// Load native UI library.
var gui = require('nw.gui');

var option = {
  key : "Ctrl+Shift+A",
  active : function() {
    console.log("Global desktop keyboard shortcut: " + this.key + " active."); 
  },
  failed : function(msg) {
    // :(, fail to register the |key| or couldn't parse the |key|.
    console.log(msg);
  }
};

// Create a shortcut with |option|.
var shortcut = new gui.Shortcut(option);

// Register global desktop shortcut, which can work without focus.
gui.App.registerGlobalHotKey(shortcut);

// If register |shortcut| successfully and user struck "Ctrl+Shift+A", |shortcut|
// will get an "active" event.

// You can also add listener to shortcut's active and failed event.
shortcut.on('active', function() {
  console.log("Global desktop keyboard shortcut: " + this.key + " active."); 
});

shortcut.on('failed', function(msg) {
  console.log(msg);
});

// Unregister the global desktop shortcut.
gui.App.unregisterGlobalHotKey(shortcut);

unregisterGlobalHotKey(shortcut);

移除系统快捷键。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值