简体   繁体   English

无法在电子中加载反应开发工具

[英]Unable to load react dev tools in electron

I'm trying to load React and Redux dev tools in electron, so far Redux was loaded successfully, but React was not.我正在尝试在电子中加载 React 和 Redux 开发工具,到目前为止 Redux 已成功加载,但 React 未成功。 I didn't see the React tab in Developer Tools .我没有在Developer Tools看到 React 选项卡。 Here is my code:这是我的代码:

main.js主文件

const electron = require("electron");
const path = require("path");
const url = require("url");
const os = require("os");

const { app, BrowserWindow } = electron;

let win;

const installExtensions = async () => {
  const ses = win.webContents.session;
    // react dev tools
    ses.loadExtension(
      path.join(
        os.homedir(),
        ".config/google-chrome/Default/Extensions/fmkadmapgofadopljbjfkapdkoienihi/4.9.0_0"
      )
    );
    // redux dev tools
    ses.loadExtension(
      path.join(
        os.homedir(),
        ".config/google-chrome/Default/Extensions/lmhkpmbekcpmknklioeibfkpmmfibljd/2.17.0_0"
      )
    );
};

const createWindow = async () => {

  win = new BrowserWindow({
    webPreferences: {
      nodeIntegration: true,
      enableRemoteModule: true,
    },
  });
  win.maximize();

  await installExtensions();

  win.loadURL(
    url.format({
      pathname: path.join(__dirname, "index.html"),
      protocol: "file:",
      slashes: true,
    })
  );

  win.webContents.once("dom-ready", () => {
    win.webContents.openDevTools();
  });

  win.on("closed", () => {
    win = null;
  });
};

app.on("ready", createWindow);

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", () => {
  if (win === null) {
    createWindow();
  }
});

package.json包.json

{
  "name": "electron-react-typescript",
  "version": "0.0.7",
  "description": "",
  "main": "/main.js",
  "scripts": {
    "start": "electron main.js"
  },
  "dependencies": {
    "electron": "^10.1.5",
    "electron-builder": "^22.9.1"
  }
}

I started the program using yarn start , here is the output:我使用yarn start程序,这是输出:

yarn run v1.22.10
warning package.json: No license field
$ electron main.js
(node:8189) ExtensionLoadWarning: Warnings loading extension at /home/searene/.config/google-chrome/Default/Extensions/fmkadmapgofadopljbjfkapdkoienihi/4.9.0_0: Unrecognized manifest key 'browser_action'. Unrecognized manifest key 'minimum_chrome_version'. Unrecognized manifest key 'update_url'. Cannot load extension with file or directory name _metadata. Filenames starting with "_" are reserved for use by the system. 
(node:8189) ExtensionLoadWarning: Warnings loading extension at /home/searene/.config/google-chrome/Default/Extensions/lmhkpmbekcpmknklioeibfkpmmfibljd/2.17.0_0: Unrecognized manifest key 'commands'. Unrecognized manifest key 'homepage_url'. Unrecognized manifest key 'page_action'. Unrecognized manifest key 'short_name'. Unrecognized manifest key 'update_url'. Permission 'notifications' is unknown or URL pattern is malformed. Permission 'contextMenus' is unknown or URL pattern is malformed. Permission 'tabs' is unknown or URL pattern is malformed. Cannot load extension with file or directory name _metadata. Filenames starting with "_" are reserved for use by the system. 

I saw Redux in the developer tools, but I didn't find React .我在开发者工具中看到了Redux ,但是我没有找到React According to this github issue , the above warnings shouldn't block the loading of dev tools.根据this github issue ,上述警告不应阻止开发工具的加载。 I also tried re-opening dev-tools, no luck.我也尝试重新打开开发工具,但没有运气。 How to solve it?如何解决?

under webPreferences add在 webPreferences 下添加

contextIsolation: false

for the loadExtension function add { allowFileAccess: true } as a 2nd argument.对于 loadExtension 函数,添加{ allowFileAccess: true }作为第二个参数。

That should make it work, as they changed the default of contextIsolation to be true in some version (I think 9.0.0), and added allowFileAccess as an extention loading option for security.这应该使它起作用,因为他们在某些版本(我认为是 9.0.0)中将 contextIsolation 的默认值更改为 true,并添加了 allowFileAccess 作为安全性的扩展加载选项。

You can make an isDev boolean to use for setting the allowFileAccess if needs be.如果需要,您可以创建一个 isDev 布尔值以用于设置 allowFileAccess。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM