node-webkit教程(7)Platform Service之APP

node-webkit教程(7)Platform ServiceAPP

/玄魂html

 

前言

几个月前,要开发一个简易的展现应用,要求支持离线播放(桌面应用)和在线播放(web应用)。html5

当时第一想到的是flex,同一套代码(或者只需少许的更改)就能够同时运行在桌面和浏览器上。因为不少展示效果要全新开发,我想到了impress.js(https://github.com/bartaz/impress.js/)。若是选择impress.js,就意味着要将html5做为桌面应用,当时想到要封装webkit,可是本人对这方面也不是很熟悉,时间也颇有限,就又沿着这个方向搜索,找到了node-webkit(https://github.com/rogerwang/node-webkit)node

node-webkit解决了我经过htmljs来编写桌面应用的难题ios

至于node-webkit的定义,按照做者的说法:git

 基于node.jschromium的应用程序实时运行环境,可运行经过HTML(5)CSS(3)Javascript来编写的本地应用程序。node.jswebkit的结合体,webkit提供DOM操做,node.js提供本地化操做;且将两者的context彻底整合,可在HTML代码中直接使用node.jsAPIgithub

从本篇文章开始,为您介绍Platform Services些列的API,本系列由如下类别:web

·             App – 每一个应用运行时全局apijson

·             Clipboard – 剪贴板windows

·             Tray – 状态栏图标,消息通知api

·             File dialogs-文件选择对话框

·             Shell – 桌面相关

·             Handling files and arguments-处理文件和相关参数

 

7.1  APP 概述

APP类别的API 是针对当前正在运行的应用程序实例的,换个说法是进程级别的(这样说还不许确,node-webkit每个窗口在单独进程中,应用自己是多进程的)。这些API和程序的启动、关闭关系最密切。可是从目前文档中的API来看,APP类别的API显得不是很丰富。

新建appDemo.htmlpackage.json文件。

package.json内容以下:

{

  "name": "app-demo",

  "main": "appDemo.html",

  "nodejs":true,

   "window": {

    "title": "appDemo",

    "toolbar": true, 

    "width": 800, 

    "height": 600,

   "resizable":true,

   "show_in_taskbar":true,

   "frame":true,

   "kiosk":false,

   "icon": "2655716405282662783.png",

  },

  "webkit":{

  "plugin":true

  }

}

appDemo.html内容以下:

<html>

<head>

    <title>appDemo</title>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</head>

<body >

    <h1>app api 测试</h1>

    <script>

        // Load native UI library

        var gui = require('nw.gui');

        var win = gui.Window.get();

    </script> 

</body>

</html>

7.1  获取APP对象

经过以下方式得到APP对象:

  // Load native UI library

        var gui = require('nw.gui');

        var app = gui.App;

7.2 获取命令行参数

鄙视不标明出处的转载,更多相关内容,欢迎访问玄魂的博客www.xuanhun521.com

 

不少时候,咱们启动程序须要从命令行输入参数,能够经过argvfullArgvfilteredArgv获取输入参数。关于三者的区别参考:https://github.com/rogerwang/node-webkit/wiki/App#fullargv。个人测试结果和文档仍是有出入的。

修改appDemo.html以下:

<html>

<head>

    <title>appDemo</title>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</head>

<body >

    <h1>app api 测试</h1>

    <script>

        // Load native UI library

        var gui = require('nw.gui');

        var app = gui.App;

        apendText(app.argv);

        apendText(app.fullArgv);

        apendText(app.filteredArgv);

        function apendText(text)

        {

            var element = document.createElement('div');

            element.appendChild(document.createTextNode(text));

            document.body.appendChild(element);

        }

    </script> 

</body>

</html>

在命令行启动程序:

运行结果以下:

7.3 dataPath

应用的数据存储目录,在不一样的操做系统上路径不一样,Windows: %LOCALAPPDATA%/<name>

Linux: ~/.config/<name>;

OSX:~/Library/Application Support/<name>

这里的<name>是在package.json中定义的name字段的值,因此须要在定义name值的时候保证全局惟一。

7.4 获取manifest

使用manifest属性,能够获取package.json中的json对象。修改appDemohtml的脚本内容以下:

    <script>

        // Load native UI library

        var gui = require('nw.gui');

        var app = gui.App;

 

        var manifest = app.manifest;

        apendText(manifest.name);

 

        function apendText(text)

        {

            var element = document.createElement('div');

            element.appendChild(document.createTextNode(text));

            document.body.appendChild(element);

        }

    </script>

结果以下:

7.5 清除缓存

鄙视不标明出处的转载,更多相关内容,欢迎访问玄魂的博客www.xuanhun521.com

能够调用clearCache()方法,清除应用在内存和磁盘上的缓存。

7.6 关闭程序

关闭程序有两个函数能够调用,分别为closeAllWindows()quit()方法,二者的区别在于closeAllWindows()方法会发送窗口的关闭消息,咱们能够监听close事件(参考:http://www.xuanhun521.com/Blog/2014/4/14/node-webkit%E5%AD%A6%E4%B9%A04native-ui-api-%E4%B9%8Bwindow),阻止窗口关闭或者作其余日志等工做。quit()方法不会发送任何消息,直接退出程序。

7.7 Crash dump

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)

为了方便测试,node-webkit提供了App.crashBrowser()App.crashRenderer()两个api,分别保存browser 进程和render进程的数据。下面咱们经过实例演示将dump文件保存到本地磁盘D

    <script>

        // Load native UI library

        var gui = require('nw.gui');

        var app = gui.App;

 

        app.setCrashDumpDir('d:\\');//设置转储目录

        app.crashBrowser();

        app.crashRenderer();

   

        function apendText(text)

        {

            var element = document.createElement('div');

            element.appendChild(document.createTextNode(text));

            document.body.appendChild(element);

        }

    </script>

运行程序,应用启动后会崩溃退出,在D盘会看到转储文件:

如何查看转储文件,这里就不详细介绍了,会在专门的文章中讲解,读者如今能够参考文档中的连接:

Decoding the stack trace

To extract the stack trace from the minidump file, you need the minidump_stackwalk tool, symbols file of node-webkit binary and the minidump (.dmp) file generated from the crash.

See http://www.chromium.org/developers/decoding-crash-dumps

http://code.google.com/p/google-breakpad/wiki/GettingStartedWithBreakpad

Symbols file of official node-webkit binary is provided staring from 0.8.0. It can be downloaded from:

Resources

Linux symbol files of breakpad

https://s3.amazonaws.com/node-webkit/v0.8.0/nw.breakpad.ia32.gz

https://s3.amazonaws.com/node-webkit/v0.8.0/nw.breakpad.x64.gz

windows pdb file

https://s3.amazonaws.com/node-webkit/v0.8.0/nw.exe.pdb.zip

mac dSYM files

https://s3.amazonaws.com/node-webkit/v0.8.0/node-webkit-osx-dsym-v0.8.0.tar.gz

7.8  获取代理

使用getProxyForURL(url),能够得到加载该url时使用的代理信息。返回值使用PAC格式(参考:http://en.wikipedia.org/wiki/Proxy_auto-config)。

 

7.6 小结

本文内容主要参考node-webkit的官方英文文档,作了适当的调整(https://github.com/rogerwang/node-webkit/wiki/App

https://github.com/rogerwang/node-webkit/wiki/Crash-dump)。

下一篇文章,介绍Clipboard

鄙视不标明出处的转载,更多相关内容,欢迎访问玄魂的博客www.xuanhun521.com

更多相关内容,欢迎访问玄魂的博客(更多node-webkit相关内容 http://www.xuanhun521.com/Blog/Tag/node-webkit)

ps:nw.js,electron交流群 313717550 

相关文章
相关标签/搜索