最近别的组有项目里面使用了 nw.js 去实现了桌面应用程序,出于好奇,我查找了一些资料,准备了解一下入个门。

什么是 nw.js

https://github.com/nwjs/nw.js

node-webkit 更名为 NW.js。

NW.js 是一个基于 Chromium 和 node.js 的应用运行时。 可以使用 NW.js 以 HTML 和 JavaScript 编写本机应用程序。 它还允许您直接从 DOM 调用 Node.js 模块,并启用一种使用所有 Web 技术编写本机应用程序的新方法。

它是在英特尔开源技术中心创建的。

下载 nwjs-sdk

https://nwjs.io/downloads/

下载完成后,解压一下

运行一下 nw.exe ,就会弹出 nw.js 的窗口了。

demo1:输出 hello world 窗口

在上面解压目录的基础上面

Step 1. 创建一个 package.json 文件,里面添加下面代码

{"name": "helloworld","main": "index.html"
}

Step 2. 创建一个index.html 文件

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>凯小默的博客测试nwjs</title>
</head>
<body><h1>hello world nwjs</h1>
</body>
</html>

Step 3. 启动应用

添加好上面两个文件之后:


我们运行 nw.exe,我们可以看到弹出了这个桌面应用程序。

demo2:使用 nw.js 创建菜单栏

https://nwjs.readthedocs.io/en/latest/#references

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>凯小默的博客测试nwjs</title>
</head><body><h1>hello nwjs Menu</h1><script>// Create an empty menubarvar menu = new nw.Menu({type: 'menubar'});// Create a submenu as the 2nd level menuvar submenu = new nw.Menu();submenu.append(new nw.MenuItem({ label: 'kaimo 666' }));submenu.append(new nw.MenuItem({ label: 'kaimo 777' }));// Create and append the 1st level menu to the menubarmenu.append(new nw.MenuItem({label: 'kaimo',submenu: submenu}));// Assign it to `window.menu` to get the menu displayednw.Window.get().menu = menu;console.log("nw--->", nw);console.log("window.menu--->", window.menu);</script>
</body></html>

点击 kaimo 就可以出现下来的菜单

打印日志输出如下


demo3:使用 node.js 的 api

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>凯小默的博客测试nwjs</title>
</head><body><script>// get the system platform using node.jsvar os = require('os');// os.platform() 方法返回一个字符串, 指定Node.js编译时的操作系统平台document.write(`kaimo is running on ${os.platform()}、${os.version()}`);</script>
</body></html>

demo4:实现切换主题的功能

这里可以参考我之前写的,html + css + js 怎么实现主题样式的切换?

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>凯小默的博客测试nwjs</title><link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.9/theme-chalk/index.css"><style>body {padding: 50px 100px;background-color: var(--background-color);}.box {margin-top: 20px;padding: 10px;border: 1px solid var(--border-color);box-shadow: var(--box-shadow)}.box:hover {box-shadow: var(--box-shadow-hover)}.box .text {color: var(--text-color);}.box .text-sub {margin-top: 10px;color: var(--text-color-sub);}</style>
</head>
<body><div class="btn"><button mode="light" class="el-button el-button--primary">明亮模式</button><button mode="read" class="el-button el-button--success">阅读模式</button><button mode="dark" class="el-button el-button--warning">暗黑模式</button></div><div class="box"><div class="text">凯小默的博客</div><div class="text-sub">测试切换不同主题模式</div><h3 class="text">当前模式:<span class="cur-mode"></span></h3></div><script>// 模式配置const modeOptions = {light: {'--background-color': '#fff','--box-shadow': '0 1px 8px 0 rgba(0, 0, 0, 0.1)','--box-shadow-hover': '0 2px 16px 0 rgba(0, 0, 0, 0.2)','--text-color': '#242424','--text-color-sub': '#7F7F7F','--border-color': '#eaecef',},read: {'--background-color': '#f5f5d5','--box-shadow': '0 1px 8px 0 rgba(0, 0, 0, 0.1)','--box-shadow-hover': '0 2px 16px 0 rgba(0, 0, 0, 0.2)','--text-color': '#004050','--text-color-sub': '#7F7F7F','--border-color': 'rgba(0, 0, 0, 0.15)',},dark: {'--background-color': '#181818','--box-shadow': '0 1px 8px 0 rgba(255, 255, 255, .6)','--box-shadow-hover': '0 2px 16px 0 rgba(255, 255, 255, .7)','--text-color': 'rgba(255, 255, 255, .8)','--text-color-sub': '#8B8B8B','--border-color': 'rgba(255, 255, 255, .3)',}}// 设置模式function setMode(mode) {const rootElement = document.querySelector(':root');const options = modeOptions[mode];// 遍历设置for (const k in options) {rootElement.style.setProperty(k, options[k]);}rootElement.setAttribute("data-theme", mode);// 当前模式const curMode = document.querySelector('.cur-mode');curMode.innerHTML = mode;}// 初始设置为明亮模式setMode("light");document.querySelector(".btn").addEventListener("click", (e) => {setMode(e.target.getAttribute("mode"));})</script>
</body>
</html>

运行之后:


切换 read 模式

切换到 dark 模式


今天就到这里,等我研究研究,到时有时间在写一写博客。

参考资料

  • https://github.com/nwjs/nw.js
  • https://nwjs.readthedocs.io/en/latest/For%20Users/Getting%20Started/
  • https://nwjs.readthedocs.io/en/latest/References/Menu/
  • https://www.nodeapp.cn/os.html
  • html + css + js 怎么实现主题样式的切换?

十分钟轻松入门 nw.js 实现桌面应用程序相关推荐

  1. window下使用nw.js开发桌面应用程序环境的搭建

    安装node window下去node官网下载长期支持版本或者最新稳定版都可以. 按装nw用来管理和按装nw.js npm install -g nrm # 用来设置npm镜像地址 npm insta ...

  2. mac nw.js 打包桌面应用程序

    参考https://segmentfault.com/a/1190000007564694和https://zhuanlan.zhihu.com/p/20070166 1.mac电脑下打包,需要先下载 ...

  3. Python十分钟轻松入门

    简介 Python是一种动态解释型的编程语言.Python可以在Windows.UNIX.MAC等多种操作系统上使用,也可以在Java..NET开发平台上使用. 特点 1 Python使用C语言开发, ...

  4. mac新手教程:十分钟轻松熟悉操作Mac系统

    习惯了Windows系统,刚刚触摸到Mac电脑是不是感觉很不习惯呢?mac os是 苹果 公司为苹果品牌计算机打造的操作系统.如果您是初次接触Mac系统,那么不用紧张,建议您从这里开始学起.macw小 ...

  5. python新手教程 从零开始-Python零基础从零开始学习Python十分钟快速入门

    原标题:Python零基础从零开始学习Python十分钟快速入门 学习Python的,都知道Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言.Python是一种动态解释型的 ...

  6. python十分钟教程_简洁的十分钟Python入门教程

    [简介] Python是一种动态解释型的编程语言.Python可以在Windows.UNIX.MAC等多种操作系统上使用,也可以在Java..NET开发平台上使用. [特点] 1 Python使用C语 ...

  7. 十分钟教会你原生JS压缩图片,极其精简版

    十分钟教会你原生JS压缩图片,极其精简版 原文链接:https://blog.csdn.net/yasha97/article/details/83629510 (一)实现思路 先通过input标签获 ...

  8. 用node-webkit(NW.js)创建桌面程序

    以往写windows桌面程序需要用MFC.C#之类的技术,那么如果你只会web开发技术呢?或者说你有一个网站,但是你想把你的网站打包成一个桌面应用程序,该如何做呢? 答案就是用node-webkit这 ...

  9. vue-cli十分钟学习入门笔记――开袋即食

    vue-cli十分钟学习之从一无所知到糊里糊涂 文章目录 vue-cli十分钟学习之从一无所知到糊里糊涂 1.为啥要学习Vue? 2.NodeJs安装及其npm介绍 3.脚手架vue-cli安装 4. ...

最新文章

  1. c++11 function
  2. 简单01背包 POJ3211 Washing Clothes 多种衣服分别dp
  3. 转载 cFos vs cFosSpeed
  4. 无广告的pdf阅读器_奥利给!免费无广告!功能超齐全!这样的良心国产软件,真的不多了!...
  5. vh,vw单位你知道多少?
  6. GenePix Pro 3.0
  7. linux内核启动过程2:保护模式执行流程
  8. Android开发笔记(一百五十二)H5通过WebView上传图片
  9. prim算法适用条件_内部排序算法的比较及应用
  10. 下一个十年,什么样的测试会被大厂争抢?
  11. 解决AssetBundle包加载预制体时,Shader显示异常的问题
  12. sql语句之查询操作
  13. Spring boot Redis客户端 乱码
  14. linux 下查看文件字符编码和转换编码 360doc,Linux 下查看文件字符编码和转换编码...
  15. 软件设计大赛编程题《拼音字母》
  16. 4.1.1 OS之初识文件管理概念和功能
  17. 这款游戏可能是minecraft和迷你世界的共同敌人了吧!
  18. 1049:晶晶赴约会
  19. 【2015-CVPR】Fully Convolutional Networks for Semantic Segmentation
  20. USACO-Section 1.3 Combination Lock[...]

热门文章

  1. javascript 的script标签
  2. fastAdmin Echarts图形
  3. 传统IT人的崩溃瞬间……
  4. C++中引用符号“”的作用总结
  5. 西门子PLC配KUKA机器人程序 程序为西门子S7-1500PLC博途调试: 西门子与KUKA机器人通讯
  6. win10关闭快速启动
  7. 一步一步教你搭建外卖cps小程序(分销裂变版本)附源码
  8. html播放rtsp流,浏览器播放rtsp视频流解决方案
  9. linux mysql 清空缓存吗_linux怎么清除缓存(转)
  10. 2315 Time(哈尔滨理工大学)