javascript - 渲染器问题中的电子非上下文感知本机模块
问题描述
我已经尝试了很多事情,即使使用重建以及我见过的其他示例,我似乎也无法执行 robotsjs 代码。
我仍然在渲染器错误中收到 Loading non-context-aware native module。
我有一个按钮,单击该按钮我希望执行 robotsjs 代码。
这是我当前的代码:
bot.js
const robot = require('robotjs')
const electron = require('electron')
const ipc = electron.ipcRenderer
const Bot = () => {
const button = document.getElementById('start');
// button.addEventListener('click', () => console.log('Click'))
button.addEventListener('click', function (e) {
// Get mouse position.
var mouse = robot.getMousePos();
// Get pixel color in hex format.
var hex = robot.getPixelColor(mouse.x, mouse.y);
console.log("#" + hex + " at x:" + mouse.x + " y:" + mouse.y);
});
}
Bot();
我的主文件:index.js
const { app, BrowserWindow, ipcMain } = require('electron');
const robot = require('robotjs')
const path = require('path');
// const Bot = require('./bot')
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
app.quit();
}
const createWindow = () => {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
}
});
// and load the index.html of the app.
mainWindow.loadFile(path.join(__dirname, 'index.html'));
// Open the DevTools.
mainWindow.webContents.openDevTools();
};
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.
这是 index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello World!</title>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<div class="App">
<div class="Header">
<h1>Checkout-Bot</h1>
</div>
<div class="Wrapper">
<p>Click Start</p>
<button id="start">Start</button>
</div>
</div>
<script src="bot.js">
Bot();
</script>
</body>
</html>
解决方案
你在 Github 上读过这个问题吗?
他们删除了使用non-NAPI
和non-context aware
模块的能力。
如果您还没有使用 Electron 11,您仍然可以使用这些模块,前提是您在主进程中添加以下行:
app.allowRendererProcessReuse = false
推荐阅读
- python - KeyError:“['month''day'] 不在索引中”
- mysql - 如何使用表 1 和表 2 中的特定匹配行从表 1 中获取所有行
- hyperledger-fabric - fabric-sdk-java:NsReadWriteSet.parseFrom 抛出 InvalidProtocolBufferException
- android - 如何重命名谷歌播放控制台暂停的应用程序
- r - 在 R Shiny 中调用 downloadHandler 函数时,如何生成带有选择提示的弹出窗口,提示要下载哪个对象?
- java - Jira ServiceDesk 创建请求 API - 将字符串 [] 添加到自定义字段 (Java)
- android - 为什么 ExifInterfac 返回 0 的纬度和经度值?
- c# - 如何将 json 附加到 Elastic Search .NEST 客户端中的现有 json?
- r - 使用快捷方式引用将 R 对象名称转换为字符串
- react-native - 如何使用 expo 通过 React Native 收听 Mercure 服务器?