您的位置:首页 > Web前端 > Node.js

Electron-Nodejs-Addon入门

2017-11-05 00:00 330 查看
摘要: Electron nodejs 原生模块编写
代码下载:https://github.com/juxiangwu/electron-node-addons

1、本次学习使用Electron的版本是1.8.0,Nodejs的版本是7.9.0,操作系统为 Win10 x64,编译器为Microsoft VC++ 14

2、安装Node模块:node-gyp node-pre-gyp nan

3、编写代码如下:

//hello.cc

#include <node.h>
///#include "boost/array.hpp"

namespace demo {

using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
using v8::Array;
using v8::Number;

void Method(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));
//boost::array<int,9> arr = {1,2,3,4,5,6,7,8,9};

//Local<Array> v8array = Array::New(isolate);
//for(int i = 0;i < 9;i++){
// v8array->Set(i,Number::New(isolate,arr[i]));
// }
// args.GetReturnValue().Set(v8array);
}

void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "hello", Method);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, init)

} // namespace demo

4、编写编译配置文件

//binding.gyp

{
"targets": [
{
"target_name": "hello",
"sources": [ "hello.cc" ],
"include_dirs":[

],
"libraries":[],
"link_settings":{
"libraries":[]
},
#"cflags!": [ "-fno-exceptions" ],
#"cflags": [ "-std=c++11" ],
#"cflags_cc!": [ "-fno-exceptions" ]
}
]
}

5、生成编译配置

在CMD里运行:

node-gyp configure --target=1.8.0 --arch=x64 --dist-url=https://atom.io/download/atom-shell

等待依赖文件下载完成,配置完成

6、编译代码

node-gyp build --target=1.8.0 --arch=x64

7、编写Electron启动代码

//main.js

const path = require('path')
const electron = require('electron')

const BrowserWindow = electron.BrowserWindow
const app = electron.app

const debug = true

if (process.mas) app.setName('Electron APIs')

var mainWindow = null

function initialize () {
var shouldQuit = makeSingleInstance()
if (shouldQuit) return app.quit()

// loadDemos()

function createWindow () {
var windowOptions = {
width: 1080,
minWidth: 680,
height: 840,
title: app.getName()
}

if (process.platform === 'linux') {
windowOptions.icon = path.join(__dirname, '/assets/app-icon/png/512.png')
}

mainWindow = new BrowserWindow(windowOptions)
mainWindow.loadURL(path.join('file://', __dirname, '/index.html'))

// Launch fullscreen with DevTools open, usage: npm run debug
if (debug) {
mainWindow.webContents.openDevTools()
mainWindow.maximize()
//require('devtron').install()
}

mainWindow.on('closed', function () {
mainWindow = null
})
}

app.on('ready', function () {
createWindow()
// autoUpdater.initialize()
})

app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
})

app.on('activate', function () {
if (mainWindow === null) {
createWindow()
}
})
}

// Make this app a single instance app.
//
// The main window will be restored and focused instead of a second window
// opened when a person attempts to launch a second instance.
//
// Returns true if the current version of the app should quit instead of
// launching.
function makeSingleInstance () {
if (process.mas) return false

return app.makeSingleInstance(function () {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
})
}

// Require each JS file in the main-process dir
//function loadDemos () {
// var files = glob.sync(path.join(__dirname, 'main-process/**/*.js'))
// files.forEach(function (file) {
// require(file)
// })
//autoUpdater.updateMenu()
//}

// Handle Squirrel on Windows startup events
switch (process.argv[1]) {
case '--squirrel-install':
//autoUpdater.createShortcut(function () { app.quit() })
break
case '--squirrel-uninstall':
//autoUpdater.removeShortcut(function () { app.quit() })
break
case '--squirrel-obsolete':
case '--squirrel-updated':
app.quit()
break
default:
initialize()
}

8、编写Electron页面代码

//index.html

<!doctype html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<script src="./jquery-3.2.1.min.js"></script>
<script>if (typeof module === 'object') {window.jQuery = window.$ = module.exports;};</script>

<script>
var addon = require('./build/Release/hello')
$(document).ready(function(){
$('#test-addon-btn').click(function(){
console.log(addon.hello());
});
});
</script>
</head>

<body>

<button id="test-addon-btn">Test</button>

</div>
</body>

</html>

9、编写Electron运行的package.json文件

{
"name": "ElectronAddonHello",
"productName": "Electron Addon Demos",
"version": "1.3.0",
"description": "Electron Addon demos",
"private": true,
"main": "main.js",
"repository": "https://github.com/juxiangwu/electron-node-addons",
"keywords": [
"Electron",
"API",
"demo"
],
"author": "Jenson",
"license": "MIT",
"devDependencies": {
"chai": "^3.4.1",
"chai-as-promised": "^6.0.0",
"check-for-leaks": "^1.2.0",
"devtron": "^1.3.0",
"electron": "~1.6.2",
"electron-packager": "^8.6.0",
"electron-winstaller": "^2.2.0",
"husky": "^0.14.3",
"mocha": "^3.1.0",
"npm-run-all": "^4.0.2",
"request": "^2.70.0",
"rimraf": "^2.5.2",
"signcode": "^0.5.0",
"spectron": "~3.6.0",
"standard": "^8.2.0"
},
"dependencies": {
"electron-settings": "^3.0.7",
"electron-shortcut-normalizer": "^1.0.0",
"glob": "^7.1.0",
"highlight.js": "^9.3.0"
},
"standard": {
"env": {
"mocha": true
}
}
}

10、运行测试结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Electron Nodejs