当前位置: 首页 > 文档资料 > Electron 中文文档 >

开发工具扩展程序

优质
小牛编辑
200浏览
2023-12-01

Electron支持Chrome DevTools 扩展程序,可增强开发工具调试流行web框架的能力

DevTools Extension

Electron supports the Chrome DevTools Extension, which can be used to extend the ability of devtools for debugging popular web frameworks.

如何加载一个 DevTools 扩展程序

本文简要描述了手动加载一个扩展程序的过程 你也可以尝试一下electron-devtools-installer,这个第三方工具可以直接从Chrome的WebStore下载扩展程序

为了在Electron中加载一个扩展程序,你需要在Chrome浏览器中下载它,找到它在系统目录中位置,然后调用BrowserWindow.addDevToolsExtension(extension)API 加载它

下面以React Developer Tools为例:

  1. 在 Chrome 中安装React Developer Tools 。

  2. 打开chrome://extensions,找到扩展程序的ID,形如fmkadmapgofadopljbjfkapdkoienihi的hash字符串。

  3. 找到Chrome 扩展程序 的存放目录:

    • 在Windows 下为 %LOCALAPPDATA%GoogleChromeUser DataDefaultExtensions;
    • 在 Linux下为:

      • ~/.config/google-chrome/Default/Extensions/
      • ~/.config/google-chrome-beta/Default/Extensions/
      • ~/.config/google-chrome-canary/Default/Extensions/
      • ~/.config/chromium/Default/Extensions/
    • 在 macOS下为~/Library/Application Support/Google/Chrome/Default/Extensions
  4. Pass the location of the extension to BrowserWindow.addDevToolsExtension API, for the React Developer Tools, it is something like:

    const path = require('path')
    const os = require('os')
    BrowserWindow.addDevToolsExtension(
      path.join(os.homedir(), '/Library/Application Support/Google/Chrome/Default/Extensions/fmkadmapgofadopljbjfkapdkoienihi/0.15.0_0')
    )

注意:只有在app模块的ready事件触发之后,才可以调用BrowserWindow.addDevToolsExtension API

The extension will be remembered so you only need to call this API once per extension. 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.

How to load a DevTools Extension

This document outlines the process for manually loading an extension. You may also try electron-devtools-installer, a third-party tool that downloads extensions directly from the Chrome WebStore.

To load an extension in Electron, you need to download it in Chrome browser, locate its filesystem path, and then load it by calling the BrowserWindow.addDevToolsExtension(extension) API.

Using the React Developer Tools as example:

  1. Install it in Chrome browser.

  2. Navigate to chrome://extensions, and find its extension ID, which is a hash string like fmkadmapgofadopljbjfkapdkoienihi.

  3. Find out filesystem location used by Chrome for storing extensions:

    • on Windows it is %LOCALAPPDATA%GoogleChromeUser DataDefaultExtensions;
    • on Linux it could be:

      • ~/.config/google-chrome/Default/Extensions/
      • ~/.config/google-chrome-beta/Default/Extensions/
      • ~/.config/google-chrome-canary/Default/Extensions/
      • ~/.config/chromium/Default/Extensions/
    • on macOS it is ~/Library/Application Support/Google/Chrome/Default/Extensions.
  4. Pass the location of the extension to BrowserWindow.addDevToolsExtension API, for the React Developer Tools, it is something like:

    const path = require('path')
    const os = require('os')
    BrowserWindow.addDevToolsExtension(
       path.join(os.homedir(), '/Library/Application Support/Google/Chrome/Default/Extensions/fmkadmapgofadopljbjfkapdkoienihi/0.15.0_0')
    )

Note: The BrowserWindow.addDevToolsExtension API cannot be called before the ready event of the app module is emitted.

The extension will be remembered so you only need to call this API once per extension. 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.

如何移除一个 DevTools 扩展程序

您可以传递扩展程序的名称到 BrowserWindow.remoeDeveToolsExtension API 移除它。 The name of the extension is returned by BrowserWindow.addDevToolsExtension and you can get the names of all installed DevTools Extensions using the BrowserWindow.getDevToolsExtensions API.

How to remove a DevTools Extension

You can pass the name of the extension to the BrowserWindow.removeDevToolsExtension API to remove it. The name of the extension is returned by BrowserWindow.addDevToolsExtension and you can get the names of all installed DevTools Extensions using the BrowserWindow.getDevToolsExtensions API.

支持的 DevTools 扩展程序

Electron 只支持有限的chrome.* API,所以,一些扩展程序如果使用了不支持的chrome.* API,它可能会无法正常工作。 以下 DevTools 扩展程序已经通过测试,可以在Electron中正常工作:

  • Ember Inspector
  • React Developer Tools
  • Backbone Debugger
  • jQuery Debugger
  • AngularJS Batarang
  • Vue.js devtools
  • Cerebral Debugger
  • Redux DevTools Extension
  • MobX Developer Tools

Supported DevTools Extensions

Electron only supports a limited set of chrome.* APIs, so some extensions using unsupported chrome.* APIs for chrome extension features may not work. Following Devtools Extensions are tested and guaranteed to work in Electron:

  • Ember Inspector
  • React Developer Tools
  • Backbone Debugger
  • jQuery Debugger
  • AngularJS Batarang
  • Vue.js devtools
  • Cerebral Debugger
  • Redux DevTools Extension
  • MobX Developer Tools

如果 DevTools 扩展不工作, 我该怎么办?

首先请确保扩展仍在维护中, 有些扩展甚至不支持 Chrome 浏览器的最新版本, 对此我们也无能为力。

然后在Electron的问题列表中提交一个 bug, 并描述扩展程序的哪个部分没有按预期的方式工作。

What should I do if a DevTools Extension is not working?

First please make sure the extension is still being maintained, some extensions can not even work for recent versions of Chrome browser, and we are not able to do anything for them.

Then file a bug at Electron's issues list, and describe which part of the extension is not working as expected.