简体   繁体   English

如何向我的 Electron 应用程序添加自定义 chrome 扩展?

[英]How can I add a custom chrome extension to my Electron app?

I am facing some trouble adding chrome addons into my Electron BrowserWindow.我在将 chrome 插件添加到我的 Electron BrowserWindow 时遇到了一些麻烦。

Before creating my window (and after the ready event has fired), I try to add a devtools extension that my browser needs to do screen sharing.在创建我的窗口之前(以及在ready事件触发之后),我尝试添加一个我的浏览器需要进行屏幕共享的 devtools 扩展。

BrowserWindow.addDevToolsExtension('/home/USER/.config/chromium/Default/Extensions/dkjdkjlcilokfaigbckcipicchgoazeg/1.5_0');

I followed this Electron guide , and it worked for their example (adding the react develop tool).我遵循了这个电子指南,它适用于他们的示例(添加反应开发工具)。 When I do the exact same thing with my own chrome extension I have this error:当我使用自己的 chrome 扩展程序执行完全相同的操作时,出现此错误:

[4735:1116/163422.268391:ERROR:CONSOLE(7701)] "Skipping extension with invalid URL: chrome-extension://extension-name", source: chrome-devtools://devtools/bundled/shell.js (7701)

I don't really get why the error specified is "invalid URL" since I'm doing the exact same thing / process with the react addon without a problem.我真的不明白为什么指定的错误是“无效的 URL”,因为我正在用 react 插件做完全相同的事情/过程而没有问题。 I also have no idea what to do.我也不知道该怎么办。 Is it possible that my chrome addon is not Electron-compatible?我的 chrome 插件是否可能与 Electron 不兼容?

It looks like you're trying to add a regular Chrome extension instead of a Dev Tools extension.您似乎正在尝试添加常规 Chrome 扩展程序而不是开发工具扩展程序。

The BrowserWindow.addExtension(path) method is for regular Chrome extensions: BrowserWindow.addExtension(path)方法用于常规 Chrome 扩展:

BrowserWindow.addExtension(path)

  • path String path字符串

Adds Chrome extension located at path , and returns extension's name.添加位于path Chrome 扩展程序,并返回扩展程序的名称。

The method will also not return if the extension's manifest is missing or incomplete.如果扩展的清单丢失或不完整,该方法也不会返回。

Note: This API cannot be called before the ready event of the app module is emitted.注意:在发出app模块的ready事件之前不能调用此 API。

- https://electronjs.org/docs/api/browser-window#browserwindowaddextensionpath - https://electronjs.org/docs/api/browser-window#browserwindowaddextensionpath

Conversely, the BrowserWindow.addDevToolsExtension(path) method is for Dev Tools extensions:相反, BrowserWindow.addDevToolsExtension(path)方法用于开发工具扩展:

BrowserWindow.addDevToolsExtension(path)

  • path String path字符串

Adds DevTools extension located at path , and returns extension's name.添加位于path DevTools 扩展,并返回扩展的名称。

The extension will be remembered so you only need to call this API once, this API is not for programming use.扩展会被记住,所以你只需要调用一次这个 API,这个 API 不是为了编程使用。 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.如果您尝试添加已加载的扩展,则此方法不会返回,而是将警告记录到控制台。

The method will also not return if the extension's manifest is missing or incomplete.如果扩展的清单丢失或不完整,该方法也不会返回。

Note: This API cannot be called before the ready event of the app module is emitted.注意:在发出app模块的ready事件之前不能调用此 API。

- https://electronjs.org/docs/api/browser-window#browserwindowadddevtoolsextensionpath - https://electronjs.org/docs/api/browser-window#browserwindowadddevtoolsextensionpath

Note that in both cases you need to wait for the ready event from the app module to be emitted:请注意,在这两种情况下,您都需要等待来自app模块的ready事件被发出:

const { BrowserWindow, app } = require('electron')

let mainWindow = null

function main() {
  BrowserWindow.addExtension('/path/to/extension')
  mainWindow = new BrowserWindow()
  mainWindow.loadURL('https://google.com')
  mainWindow.on('close', event => {
    mainWindow = null
  })
}

app.on('ready', main)

Support for Chromium extensions in Electron is actively being worked on at the moment.目前正在积极支持 Electron 中的 Chromium 扩展。 The support isn't complete yet, but the GitHub issue seems to have regular updates being posted.支持尚未完成,但GitHub 问题似乎定期发布更新。

Fingers crossed!手指交叉!

A current pull request is open for 'just enough extensions [api] to load a simple ... extension'当前的拉取请求是为“刚好足够的扩展 [api] 加载一个简单的...扩展”打开

Electron 9 has much more support for extensions! Electron 9 对扩展有更多的支持!

To load them, use session.loadExtension : https://github.com/electron/electron/blob/master/docs/api/extensions.md要加载它们,请使用session.loadExtensionhttps : //github.com/electron/electron/blob/master/docs/api/extensions.md

const { app, BrowserWindow, session } = require('electron')

// ... in your createWindow function, which is called after app.whenReady

const mainWindow = new BrowserWindow({...})

const ext = await session.defaultSession.loadExtension('/path/to/unpacked/chrome-ext')

console.log('ext', ext)
// outputs config file
// {
//   id: 'dcpdbjjnmhhlnlbibpeeiambicbbndim',
//   name: 'Up! – Free Social Bot',
//   path: '/Users/caffeinum/Development/GramUp/chrome-ext',
//   url: 'chrome-extension://dcpdbjjnmhhlnlbibpeeiambicbbndim/',
//   version: '1.7.0',
//   manifest: { ... }
// }


Read more: https://github.com/electron/electron/blob/master/docs/api/extensions.md阅读更多: https : //github.com/electron/electron/blob/master/docs/api/extensions.md

Also, there's another project that helps you do that, also adds additional functionality: https://github.com/sentialx/electron-extensions此外,还有另一个项目可以帮助您做到这一点,还添加了其他功能: https : //github.com/sentialx/electron-extensions

虽然有记录的方法来注册一个普通的扩展,但在大多数情况下它不会做太多,因为 Electron 只支持chrome.* API 的一个可访问子集显然只有Spectron 和 Devtron需要的东西)并且因为它们前阵子说过,他们没有任何计划全面支持 Chrome 扩展 API。

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

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