-->

Communication between Chrome javascript and native

2019-04-16 04:08发布

问题:

I need to have a communication channel between my web application that runs on Chrome, and a native code on Windows. I need to run a native code when JS requests and pass the results back from native code. The environment is totally managed so I can set trusts and group policies, etc.

I can think of preparing a small web service that runs locally (and allows CORS) and call this service from javascript, but in this case i need to run this code forever.

Any advices will be very helpful. If it is possible i can try Windows registry write/read, pipes, shared memory, MMF or any other way to do it.

Thanks

回答1:

There's a way to communicate with local processes without using an extension. Websockets aren't restricted to communicating with the same domain as the web page, they can communicate with a WebSocket server on localhost. You have to wrap your native code in a WebSocket server, libraries are available for that though.

Another method is Native Messaging, but it requires a browser extension:

Native messaging enables a WebExtension to exchange messages with a native application installed on the user's computer.

https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Native_messaging https://developer.chrome.com/extensions/nativeMessaging