简体   繁体   English

Electron, Webpack, 反应。 使用剪贴板时; 'fs' 未定义

[英]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.

相关问题 将 Electron 与 React 一起使用时出现“未捕获的类型错误:fs.writeFile 不是函数” - “Uncaught TypeError: fs.writeFile is not a function” when using Electron with React 与webpack一起安装时未定义React - React not defined when installed with webpack Webpack 反应。 配置包含图片的文件夹 - Webpack react. Configure a folder with pictures Uncaught ReferenceError: Clipboard is not defined: when using clipboard.js with Rails 6 - Uncaught ReferenceError: Clipboard is not defined: when using clipboard.js with Rails 6 将电子渲染器添加到 webpack 时“需要未定义” - 'Require is not defined' when adding electron-renderer to webpack Webpack 开发服务器并做出反应。 仅在运行应用程序时获取目录列表 - Webpack dev server and react. Only get directory listing when running app 流星+ React中出现错误提示“状态未定义”。 - Error mensage “state is not defined” in Meteor + React. 使用webpack定位网络时fs.readFile的替代方法 - Alternative for fs.readFile when targeting the web using webpack Webpack:使用Bundle.js,“React未定义” - Webpack: Using Bundle.js, “React is not defined” 在使用 React 和 Webpack 的 Electron 应用程序中违反内容安全策略 - Violating Content Secuirty Policy in Electron app using React and Webpack
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM