[英]Electron, Webpack, React. When using clipboard; 'fs' is not defined
So I'm running electron via export ELECTRON_START_URL=http://localhost:8080 && electron.
In a react / webpack / electron desktop app.所以我通过
export ELECTRON_START_URL=http://localhost:8080 && electron.
在反应 / webpack / electron 桌面应用程序中。 I'm trying to read from the system clipboard upon paste key combination (ctrl + v).我试图在粘贴组合键 (ctrl + v) 时从系统剪贴板中读取。
So electron works, I DO NOT have a Copy & Paste shortcut enabled in my menu definitions (I don't plan on having a menu).所以 electron 有效,我没有在我的菜单定义中启用复制和粘贴快捷方式(我不打算有菜单)。
I'm trying to read a key combination on one component within the react app...similar to:我正在尝试读取 React 应用程序中一个组件上的组合键...类似于:
import React, { useState, useCallback, useEffect, useRef } from "react";
import {clipboard} from 'electron';
const Thing = (props) {
const keyboardHandler = (e) => {
if (e.keyCode === 86 && e.ctrlKey) {
let pasted = clipboard.readText();
console.log(pasted);
}
}
useEffect(() => {
window.addEventListener('keydown', keyboardHandler);
// Remove event listeners on cleanup
return () => {
window.removeEventListener('keydown', keyboardHandler);
};
}, []);
return <SomeComp onKeyDown={ (e) => keyBoardHandler(e)}/>
}
export default Thing;
I'm getting the error:我收到错误:
external_"electron":1 Uncaught ReferenceError: require is not defined
at eval (external_"electron":1)
at Object.electron (bundle.js:1167)
EDIT: this is my main.js (electron) code:编辑:这是我的 main.js(电子)代码:
const { app, BrowserWindow, clipboard } = require('electron');
const path = require('path');
const url = require('url');
let mainWindow;
function createWindow () {
const startUrl = process.env.ELECTRON_START_URL || url.format({
pathname: path.join(__dirname, '../index.html'),
protocol: 'file:',
slashes: true,
});
mainWindow = new BrowserWindow({ width: 700, height: 210, frame: true, transprent: true, hasShadow: true });
mainWindow.loadURL(startUrl);
mainWindow.on('closed', function () {
mainWindow = null;
});
}
app.commandLine.appendSwitch("enable-transparent-visuals");
app.on('ready', createWindow);
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function () {
if (mainWindow === null) {
createWindow();
}
});
add "window."添加“窗口”。 to the require node function as following:
到 require 节点 function 如下:
window.require('electron');
That works for me, hope with you too.这对我有用,希望对你也有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.