Google chrome extension 使用electron应用程序的Chrome本机消息传递

Google chrome extension 使用electron应用程序的Chrome本机消息传递,google-chrome-extension,stdin,electron,stdio,chrome-native-messaging,Google Chrome Extension,Stdin,Electron,Stdio,Chrome Native Messaging,我正试图在OSX上的chrome扩展和electron应用程序之间建立通信 我在~/Library/Application\Support/Google/Chrome/NativeMessagingHosts/com.company.app.JSON中有JSON文件 { "name": "com.company.app", "description": "MyApp", "path": "/Users/johnryan/Desktop/Code/electron-app/

我正试图在OSX上的chrome扩展和electron应用程序之间建立通信

我在
~/Library/Application\Support/Google/Chrome/NativeMessagingHosts/com.company.app.JSON中有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分机上,我有一个简单的本机消息呼叫:

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);
})

但是,当我执行
sendinativemessage
时,在日志中看不到任何内容。这里有我遗漏的东西吗

最近偶然发现了这个问题,并发现在windows平台上不支持从主线程访问stdin和stdio。通过在我的electron应用程序中启动http服务器,并在windows平台中从http端口进行侦听,我暂时实现了这一功能

但是,同样的方法也适用于MAC OS,只需使用electron packager构建您的electron应用程序,并在path变量中给出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中工作,请参阅本文,它可能有一个解决方案


猜测:因为短消息的第一个字节是0,所以您应该以二进制模式读取,否则它可能会被readline解释为字符串的标准结尾。@wOxxOm有趣,我将尝试一下。我的猜测是,在electron方面出了问题,默认的_app.asar似乎可疑,在其他示例中,它似乎指向一个.js文件。但不确定这是否重要。@john_ryan你做到了吗?@jonathanwiesel不完全是这样,我在electron应用程序中启动了一个http服务器,然后才发布到本地server@john_ryan您是否曾经启动本地web服务器?如果没有,您是否有一些示例代码愿意分享您是如何让本地服务器运行在electron中以与Chrome扩展通信的?