我正在尝试在 Chrome 扩展程序和 OSX 上的 Electron 应用程序之间建立通信。
我的 JSON 文件位于 ~/Library/Application\Support/Google/Chrome/NativeMessagingHosts/com.company.app.json
{
"name": "com.company.app",
"description": "MyApp",
"path": "/Users/johnryan/Desktop/Code/electron-app/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar",
"type": "stdio",
"allowed_origins": ["chrome-extension://xxxxxxxxxxxx"]
}
在 Chrome 扩展上,我有一个简单的 native 消息传递调用:
chrome.runtime.sendNativeMessage('com.company.app',
{ text: "Hello" },
function(response) {
console.log("Received " + response);
});
然后在 main.development.js 中我有:
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', function(line){
console.log("RECEIVED:" + line);
})
但是,当我执行 sendNativeMessage
时,我在日志中看不到任何内容。我在这里缺少什么吗?
最佳答案
最近偶然发现了这个问题,发现在windows平台上不支持从主线程访问stdin和stdio。我暂时通过在我的 Electron 应用程序中启动 http 服务器并从 Windows 平台中的 http 端口监听来使其工作。
但是,在 MAC 操作系统上也应该同样有效,只需使用 electro-packager 构建您的 Electron 应用程序,并在路径变量中给出 exe 的路径( Electron 应用程序的 exe),如下所示。
{
"name": "com.company.app",
"description": "MyApp",
"path": "/Users/johnryan/Desktop/Code/electron-app/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.exe",
"type": "stdio",
"allowed_origins": ["chrome-extension://xxxxxxxxxxxx"]
}
为了让它在 Windows 中工作,请参阅这篇文章,它可能有一个解决方案,
关于google-chrome-extension - Chrome Native Messaging 与 Electron 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42256410/