Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using chrome native messaging with electron #8692

Closed
johnryan opened this issue Feb 15, 2017 · 29 comments
Closed

Using chrome native messaging with electron #8692

johnryan opened this issue Feb 15, 2017 · 29 comments

Comments

@johnryan
Copy link

  • Electron version: 1.4.15
  • Operating system: OSX 10.12.3

https://developer.chrome.com/extensions/nativeMessaging

I'm trying to communicate between a chrome extension and the electron process. I saw this issue #7681 but it seems to be trying do something different (loading the extension from the electron app).

I think the extension is set up properly but i'm not sure where the "path" should be pointed to. Is this something that is even possible in electron to listen via stdin and pass that information to the renderer process?

@groundwater
Copy link
Contributor

Hi @johnryan, I think we're going to need more information to help. Can you link to the example code/report you're trying to get working?

I am not quite sure if this would qualify as a bug, a feature request, or something else.

@groundwater groundwater added the blocked/need-info ❌ Cannot proceed without more information label Feb 17, 2017
@johnryan
Copy link
Author

@groundwater Thanks for responding, it's more just a general support request (i tried the electron slack but didn't get a response). All i'm trying to do is find a way to communicate between a chrome extension and my electron app.

I ended up trying a totally different route and set up a web server running inside the main process that the chrome extension just posts to like any other service. If that seems more like a reasonable solution i'm happy to close this issue (and hopefully anyone else trying to do the same thing stumbles upon this solution).

@groundwater
Copy link
Contributor

Sounds good @johnryan. You can also try the Electron section of discuss.atom.io for support. We're still figuring out what the best approach for community support it.

For what it's worth, I use the exact same method with my electron apps.

@groundwater groundwater added triage/offtopic and removed blocked/need-info ❌ Cannot proceed without more information labels Feb 20, 2017
@mi-g
Copy link

mi-g commented Mar 4, 2017

For the records, i successfully use an electron app with a WebExtensions add-on through native messaging using the following code.

Sending message from app to add-on:

function Send(message) {
    let msgStr = JSON.stringify(message);
    let lengthStr = String.fromCharCode(
        msgStr.length & 0x000000ff,
        (msgStr.length >> 8) & 0x000000ff,
        (msgStr.length >> 16) & 0x000000ff,
        (msgStr.length >> 24) & 0x000000ff
    );
    process.stdout.write(lengthStr+msgStr);
}

Receiving message from add-on:

let msgBacklog = "";
process.stdin.setEncoding('utf8');
process.stdin.on('data', (chunk) => {
    AppendInputString(chunk);
});
function AppendInputString(chunk) {
    msgBacklog += chunk;
    while (true) {
        if (msgBacklog.length < 4)
            return;
        let msgLength = msgBacklog.charCodeAt(0) + (msgBacklog.charCodeAt(1) << 8) +
            (msgBacklog.charCodeAt(2) << 16) + (msgBacklog.charCodeAt(3) << 24);
        if (msgBacklog.length < msgLength + 4)
            return;
        try {
            let msgObject = JSON.parse(msgBacklog.substring(4, 4 + msgLength));
            // handle received message
        } catch (e) {}
        msgBacklog = msgBacklog.substring(4 + msgLength);
    }
}

@johnryan
Copy link
Author

johnryan commented Mar 4, 2017

interesting @mi-g Where does :

process.stdin.on('data', (chunk) => {
    AppendInputString(chunk);
});

live in your electron application? all the examples i've seen just showed this approach in a separate JS file which still left the question of how to get that data into the running application.

@mi-g
Copy link

mi-g commented Mar 4, 2017

You can put that code wherever you want in the main thread, but not into a window renderer.
In my case, this native messaging code is in a native-messaging.js module, which is invoked by the main.js file through a require('native-messaging.js'), main.js being the value of field main in package.json

@johnryan
Copy link
Author

johnryan commented Mar 4, 2017

@mi-g Got it, one more question i meant to ask. What do you set as the "path" of your script? in the chrome extension json (https://developer.chrome.com/extensions/nativeMessaging) ? i.e.

{
  "name": "com.my_company.my_application",
  "description": "My Application",
  "path": "C:\\Program Files\\My Application\\chrome_native_messaging_host.exe",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
  ]
}

As far as I can tell all the scripts are compressed inside the app.asar file

@mi-g
Copy link

mi-g commented Mar 4, 2017

I do not use asar archiving, so far, i didn't see any true benefit for that but a few inconveniences.

But even in asar format there must be an executable somewhere to be run to launch your application. If you use electron-builder to create an installer and execute this setup program, you should use the same resulting path as the one in the installed app shortcut.

@arjun-g
Copy link

arjun-g commented Jul 11, 2017

@mi-g I was trying to do the same. i am was able to launch the application with connectNative. I tried your method for receiving message. But on port.postMessage i am not receiving the data in electron main process. Is there any configuration I need to do before for this to work ?

@mi-g
Copy link

mi-g commented Jul 18, 2017

If the extension is able to launch the native application, then the data sent by the add-on through port.postMessage should end up in the application. Make sure your application has a proper logging mechanism that writes to a file and not to the console (that would break the native messaging protocol), then try to log any byte received by the application.

@jackmallers
Copy link

Is there a open source example located somewhere? I'm not sure I understand completely @mi-g

@vapour
Copy link

vapour commented Dec 15, 2017

I use this method to do this。
parcel [chrome native messaging] to my electron project, for the chrome extension stdio message
https://github.com/jdiamond/chrome-native-messaging

@deepakjha14
Copy link

@mi-g can you share your codes for the reference? I somehow cant make it work even after I follow your advice as given above?

@mi-g
Copy link

mi-g commented Jan 16, 2018

@deepakjha14: you can check code from the Video DownloadHelper companion app.

@deepakjha14
Copy link

@mi-g I have copied your code, but unfortunately from extension when I send message I am unable to receive it in the host program, the host does not do anything on message. Would you be kind enough to suggest me where I am making mistake if i upload my codes on github and give you the link?

@mi-g
Copy link

mi-g commented Jan 21, 2018

I presume you are sending the message from your extension using the native extension API right ? Make sure to check errors when calling runtime.sendNativeMessage
Setting up a callable native app requires installing manifest files at some specific places (depending on the OS) and on Windows some keys in the registry. Do not worry if it does not work at the first trial (it never did for me !), just keep searching.

@riskers
Copy link

riskers commented Jun 13, 2018

@johnryan Have you solved the problem?

@johnryan
Copy link
Author

@riskers as mentioned above I just ended up running a web server rather than using the native message protocol

@Nantris
Copy link
Contributor

Nantris commented Sep 3, 2018

@mi-g the Video DownloadHelper companion app is a plain Node app though as best I can tell. I'm confused. You've managed this in Electron?

I've gotten a Node application communicating via Native Messaging, but can't find a way to get an Electron application working. Do you have any ideas about doing this with Electron?


@vapour can you share any details about how to do this? I'm put my code in the main process for Electron, but I get disconnected immediately with the error from Chrome's side: Native host has exited

I've tried disabling console.log, but no luck so far. I think it's an issue with getting the Electron main process to accept stdio.

If anyone has any pointers, I'd really appreciate it. I am soooo close to having this working after two solid days of tinkering.

@mi-g
Copy link

mi-g commented Sep 3, 2018

@slapbox yes, the Video DownloadHelper companion is a plain node app, but before this app becomes what it is now, i had in mind to make it an Electron one. As far as i remember from the tests i did, it was basically the same stdin/stdout code running from an Electron app and being runnable from a Firefox/Chrome extension.

@Nantris
Copy link
Contributor

Nantris commented Sep 3, 2018

I thought that it was related to Electron attaching stdio to the console, but setting ELECTRON_NO_ATTACH_CONSOLE to true didn't help at all.

@mi-g do you have any ideas about what I should be researching to find a solution to this? I know it's a stupidly vague question, but I've exhausted pretty much every line of inquiry I can find.

@mi-g
Copy link

mi-g commented Sep 3, 2018

I'm afraid i don't remember doing anything special. It was 18 months ago, maybe since then, Electron has changed so that it now hijacks the standard streams making it impossible to use for native messaging :/

@Nantris
Copy link
Contributor

Nantris commented Sep 3, 2018

@mi-g thanks very much for your replies. As best as I can tell they haven't changed how it's handled for a couple of years, but perhaps I'm mistaken. This is my first real dive into streams and stdio, so perhaps I'm just overlooking something seemingly obvious. Thanks again, I really appreciate it.

@jml674
Copy link

jml674 commented Nov 30, 2018

Hey guys, I am trying to do the exact same thing on Windows7:

  • I'm able to implement chrome native messaging when I launch the native app using nodejs
  • switching to electron with a main app that encapsulates the exact same js files, extension does not receive any message any more but no error whatsoever is seen in the extension.
    Notes:
  • I only change the json file to switch from "node app.js" to the packaged electron app
  • I have validated that the electron app is waiting on the stdio handler.
  • I'm using mi-g code to send/receive messages
    sounds to me like electron is playing with stdio since the app does not receive any packet.

Did you progress in any way ?

@Nantris
Copy link
Contributor

Nantris commented Dec 2, 2018

@jml674 I eventually concluded it is impossible on Windows 7. I could be wrong, but that was my conclusion.

@jml674
Copy link

jml674 commented Dec 2, 2018

@slapbox thank you very much for the update. I have worked-around the issue keeping the native app but using websockets. This is a pity since it makes the whole solution way less elegant. I saw you put a comment in #4218

@BitPhoenix
Copy link

@jml674 Would you recommend the websockets workaround? I've been googling trying to figure out how to get clean chrome extension <-> electron communication working, but based on all the threads I've scoured through so far it seems like it can't be done at the moment. I was considering implementing your workaround and had a few quick questions:

  • Do you still have a native messaging host? Is the socket connection electron app <--> chrome native messaging host? Or did you forgo the native messaging host altogether and have the socket connection be electron app <--> extension?

@jml674
Copy link

jml674 commented Jan 25, 2019

@kodezen I kept the native messaging app because I wanted the addon to be in control of the "compression service" I wanted to implement. I wrote a post about it on my blog

@tangYinPeng
Copy link

Extension load can succeed, but using chrome. Runtime. ConnectNative connection not only on local applications, but use the Google browser is ok. Is there a reason

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests