Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use electron-devtools-installer?

What I have

When it comes to installation and configuration the official documentation states the following:

All you have to do now is

import installExtension, { REACT_DEVELOPER_TOOLS } from 'electron-devtools-installer';

installExtension(REACT_DEVELOPER_TOOLS)
.then((name) => console.log(`Added Extension:  ${name}`))
.catch((err) => console.log('An error occurred: ', err));

That is a bit concise.

Questions

  • Where should I place this code?
  • How should I initialize it?
  • How can I add multiple extensions?
like image 466
totymedli Avatar asked Jul 19 '17 19:07

totymedli


People also ask

How do you open Devtools in electron app?

To open DevTools, use the shortcut "Ctrl+Shift+I" or the <F12> key. You can check out how to use devtools here.

How do you install an electron extension?

To load an extension in Electron, you need to download it via Chrome, locate its filesystem path, and then load it into your Session by calling the [ ses. loadExtension ] API. Using the React Developer Tools as an example: Install the extension in Google Chrome.

How do I disable developer tools in electron?

Disable developer tools in productionThe key-combination CTRL + SHIFT + I (or ALT + CMD + I on Mac) will open the dev tools and enable inspection of the application. It will even enable some degree of modification.


1 Answers

tl;dr

Place it in the file where you include Electron and run it after the ready event was emitted by it:

const { app } = require('electron');
app.on('ready', functionWithTheCodeFromDocs);

You only need to do this once! The extensions will persist after this code have been run.

In depth explanation

Install the package

Install the package just like the docs instruct you. In case of npm:

npm install electron-devtools-installer --save-dev

Require the package

You may require the package and configure it in the file where you build up your Electron app. You need to include the installer function and the (possibly multiple) needed extension(s):

With ES6 modules:

import installExtension, { REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS } from 'electron-devtools-installer';

With require:

const { default: installExtension, REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS } = require('electron-devtools-installer');

Configuration and usage

I will presume that somewhere you required Electron:

const { app } = require('electron');

The installExtension function have to be called after the ready event was emitted by the application. If you want to add multiple extensions you have to call the function multiple times with the different extensions by copy-paste:

app.on('ready', () => {
    installExtension(REACT_DEVELOPER_TOOLS)
        .then((name) => console.log(`Added Extension: ${name}`))
        .catch((err) => console.log('An error occurred: ', err));
});
app.on('ready', () => {
    installExtension(REDUX_DEVTOOLS)
        .then((name) => console.log(`Added Extension: ${name}`))
        .catch((err) => console.log('An error occurred: ', err));
});

Or you can write a loop:

 app.on('ready', () => {
    [REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS].forEach(extension => {
      installExtension(extension)
          .then((name) => console.log(`Added Extension: ${name}`))
          .catch((err) => console.log('An error occurred: ', err));
    });
});

If you have done everything properly, after you run electron . in your project's folder, you should see this in the console:

Added Extension: React Developer Tools
Added Extension: Redux DevTools

Keep in mind: You only need to run this code once. As Electron's BrowserWindow.addDevToolsExtension documentation states:

The extension will be remembered so you only need to call this API once, this API is not for programming use. If you try to add an extension that has already been loaded, this method will not return and instead log a warning to the console.

like image 195
totymedli Avatar answered Oct 20 '22 00:10

totymedli