当前位置: 首页 > news >正文

Electron调用nodejs的cpp .node扩展【非安全】

Electron调用nodejs的cpp .node扩展【非安全】

环境:

electron: 30.1.1
nodejs: 20.14.0

前言

Electron中可以非常容易的调用nodejs的js代码,但是对于cpp .node扩展需要一定的配置才能调用,下面介绍一种最简单的cpp扩展的调用方法,该方法的优点是调用简单,缺点是会降低应用程序的安全性,生产环境中需谨慎使用。

代码

$ tree
.
+--- build
|   +--- Release
|   |   +--- addon.node
+--- addon.cpp
+--- binding.gyp
+--- CMakeLists.txt
+--- index.html
+--- index.js
+--- package.json

index.html

<!DOCTYPE html>
<html><head><meta charset="UTF-8" /><title>Hello Electron</title><meta http-equiv="Content-Security-Policy" content="default-src 'self' 'unsafe-inline';">
</head><body><p id="version"></p><p id="napi"></p><script>let info = `electron: ${process.versions.electron}, nodejs: ${process.versions.node}, chrome: ${process.versions.chrome}, v8: ${process.versions.v8}`;document.getElementById("version").innerHTML = info;console.log(info);const addon = require('./build/Release/addon.node');info = addon.hello();document.getElementById("napi").innerHTML = info;console.log(info);</script>
</body></html>

index.js

const { app, BrowserWindow } = require('electron/main');
// app.commandLine.appendSwitch('remote-debugging-port', '9222');const createWindow = () => {const win = new BrowserWindow({width: 800,height: 600,webPreferences: {nodeIntegration: true,contextIsolation: false}})win.loadFile('index.html');
}app.whenReady().then(() => {createWindow();
})

addon.cpp

#include <node_api.h>static napi_value helloMethod(napi_env env, napi_callback_info info)
{napi_value result;napi_create_string_utf8(env, "hello world from napi", NAPI_AUTO_LENGTH, &result);return result;
}static napi_value Init(napi_env env, napi_value exports)
{napi_property_descriptor desc = {"hello", 0, helloMethod, 0, 0, 0, napi_default, 0};napi_define_properties(env, exports, 1, &desc);return exports;
}NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)

binding.gyp

{"targets": [{"target_name": "addon","sources": [ "addon.cpp" ]}]
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)project(addon)message(STATUS "operation system is ${CMAKE_SYSTEM}")add_definitions(-std=c++11)include_directories(${CMAKE_JS_INC})
include_directories(.)file(GLOB SOURCE_FILES addon.cpp)add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB})

package.json

{"name": "my-electron-app","version": "1.0.0","description": "","main": "index.js","scripts": {"start": "electron ."}
}

编译

node-gyp configure build

结果

electron: 30.1.1, nodejs: 20.14.0, chrome: 124.0.6367.243, v8: 12.4.254.20-electron.0hello world from napi

禁用 contextIsolation 和启用 nodeIntegration,会降低应用的安全性。务必谨慎使用,并确保你信任加载的所有代码和资源。

http://www.lryc.cn/news/467899.html

相关文章:

  • 一文了解AOSP是什么?
  • ffmpeg视频边缘模糊,打造梦幻般的视觉效果!
  • [Wireshark] 使用Wireshark抓包https数据包并显示为明文、配置SSLKEYLOGFILE变量(附下载链接)
  • 大话红黑树之(1)入门介绍
  • ESC/POS图片打印指令
  • Unity之如何在Linux上部署Dedicated Server专用服务器
  • 十、Linux 故障排除专业案例分享
  • 智慧楼宇平台,构筑未来智慧城市的基石
  • JVM 实战篇(一万字)
  • 线程同步之双摄
  • 使用 PyTorch 构建 LSTM 股票价格预测模型
  • 【C++篇】C++类与对象深度解析(五):友元机制、内部类与匿名对象的讲解
  • 模型训练进度条的代码
  • 直观理解反向传播 | Chapter 3 | Deep Learning | 3Blue1Brown
  • 052_python基于Python高校岗位招聘和分析平台
  • 基于物联网、大数据、人工智能等技术开发的Spring Cloud 智慧工地云平台源码,支持多端应用
  • 常见的跨境电商平台对比【总结表】
  • perl批量改文件后缀
  • 【Python中的字符串处理】正则表达式与常用字符串操作技巧!
  • 又是一年一度的1024,那就记录一篇算法博客吧~ 【二进制加法探秘】
  • LeetCode--买卖股票的最佳时机含冷冻期--动态规划
  • 装了Ubuntu和Windows双系统,如何设置默认启动Windows
  • WPF+MVVM案例实战-设备状态LED灯变化实现
  • MySQL--基本介绍
  • PAT甲级1008 Elevator
  • 数据导入导出
  • git的安装以及入门使用
  • 【acwing】算法基础课-搜索与图论
  • 502 错误码通常出现在什么场景?
  • 面试经典算法题69-两数之和