我在将 chrome 插件添加到我的 Electron BrowserWindow 中时遇到一些麻烦。
在创建窗口之前(以及在 ready
事件触发之后),我尝试添加浏览器需要执行屏幕共享的开发工具扩展。
BrowserWindow.addDevToolsExtension('/home/USER/.config/chromium/Default/Extensions/dkjdkjlcilokfaigbckcipicchgoazeg/1.5_0');
我关注了这个Electron guide ,并且它适用于他们的示例(添加 react 开发工具)。当我使用自己的 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)
我真的不明白为什么指定的错误是“无效的URL”,因为我正在使用 react 插件执行完全相同的操作/过程,没有任何问题。我也不知道该怎么办。我的 chrome 插件是否可能不兼容 Electron?
最佳答案
您似乎正在尝试添加常规 Chrome 扩展程序,而不是开发工具扩展程序。
BrowserWindow.addExtension(path)
方法适用于常规 Chrome 扩展:
BrowserWindow.addExtension(path)
path
StringAdds Chrome extension located at
path
, and returns extension's name.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 theapp
module is emitted.- https://electronjs.org/docs/api/browser-window#browserwindowaddextensionpath
相反,BrowserWindow.addDevToolsExtension(path)
方法用于开发工具扩展:
BrowserWindow.addDevToolsExtension(path)
path
StringAdds DevTools extension located at
path
, and returns extension's name.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.
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 theapp
module is emitted.- https://electronjs.org/docs/api/browser-window#browserwindowadddevtoolsextensionpath
请注意,在这两种情况下,您都需要等待 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)
关于javascript - 如何向我的 Electron 应用程序添加自定义 chrome 扩展?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53341132/