Chrome nativeclient Create
The demo goal is to have the Chrome extension launch the local exe
1 Creating a console program called Nativemsgdemo
#include <Windows.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace Std;
BOOL Recievemsgfromchrome ();
int main (int argc, char* argv[])
{
while (1)
{
Sleep (300);
if (! Recievemsgfromchrome ())
Break
}
return 1;
}
The generated exe is nativeMsgDemo.exe
2. Extending the content to be written
Write content in the extended background.js so that the Chrome extension supports nativeclient message response
var port = chrome.runtime.connectNative (' Com.wudi.chrome.namsg.yunzhou ');
Port.onMessage.addListener (function (msg) {
Console.log ("Received" + msg);
});
Port.onDisconnect.addListener (function () {
Console.log ("Disconnected");
});
Port.postmessage ({text: "Hello, My_application"});
3. Chrome nativemessaginghosts Registry File
Com.wudi.chrome.namsg.yunzhou writing to the registry
Hkey_local_machine\software\google\chrome\nativemessaginghosts\com.wudi.chrome.namsg.yunzhou
Find the path to the. json file through this registry
D:\projects\NativeMsgDEmo\output\com.wudi.chrome.namsg.yz-win.json,
In this file , the EXE that chrome.runtime.connectNative True Association communication is saved , the file content is as follows:
{
"Name": "Com.wudi.chrome.namsg.yunzhou",
"description": "Chrome Native Messaging for Yuzhou",
"Path": "NativeMsgDemo.exe",
"Type": "Stdio",
"Allowed_origins": [
"Chrome-extension://hohonaplgfolmdaaafoddgbiakognoal/"
]
}
The . json file and the. EXE file are typically placed in the same directory for easy locating, such as:
Then let chrome load our extension,backgroud.html will load backgroud.js,background.js in the Chrome.runtime.connectNative will get the EXE that communicates with Chrome , which is the standard input/output.
4. nativeclient. exe search process
1. Via var in background.js port =
Chrome.runtime.connectNative (' Com.wudi.chrome.namsg.yunzhou ');
Locate the registry key in the hkey_local_machine\software\google\chrome\nativemessaginghosts
Com.wudi.chrome.namsg.yunzhou
2. Locate the Com.wudi.chrome.namsg.yz-win.json file by using the above registry key
3. exe with connection input and output specified in the Com.wudi.chrome.namsg.yz-win.json file
Chrome nativeclient Create (GO)