Chrome NativeClient建立 (转)

Chrome NativeClient建立

demo目标是让chrome扩展启动本地exehtml

 

1建立一个名叫nativeMsgDemo的控制台程序

#include <Windows.h>ios

 

#include <iostream>chrome

#include <string>json

#include <fstream>app

using namespace std;post

 

bool RecieveMsgFromChrome();chrome-extension

 

int main(int argc, char* argv[])spa

{3d

         while(1)htm

         {

                   Sleep(300);

                   if (!RecieveMsgFromChrome())

                            break;

         }

         return 1;

}

生成的exe是nativeMsgDemo.exe

2. 扩展要写入的内容

在扩展的background.js中写入内容,这样chrome扩展支持nativeclient消息响应

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注册表文件

com.wudi.chrome.namsg.yunzhou写入注册表

HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\com.wudi.chrome.namsg.yunzhou

经过这个注册表找到.json文件的路径

D:\projects\NativeMsgDEmo\output\com.wudi.chrome.namsg.yz-win.json,

在这个文件中保存了chrome.runtime.connectNative真正关联通信的exe,文件内容以下:

{

  "name": "com.wudi.chrome.namsg.yunzhou",

  "description": "Chrome Native Messaging For yuzhou",

  "path": "nativeMsgDemo.exe",

  "type": "stdio",

  "allowed_origins": [

    "chrome-extension://hohonaplgfolmdaaafoddgbiakognoal/"

  ]

}

通常会将.json文件和.exe文件放在同一个目录下,方便找到,以下图:

 

而后让chrome加载咱们的扩展,backgroud.html会加载backgroud.js,background.js中的chrome.runtime.connectNative会获取到和chrome通信的exe,这种进程间通信方式是标准的输入输出。

4. nativeClient .exe寻找流程

1. 经过background.js 中的var port =

chrome.runtime.connectNative('com.wudi.chrome.namsg.yunzhou'); 

查找注册表项HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts中的

com.wudi.chrome.namsg.yunzhou

2. 经过上述注册表项找到com.wudi.chrome.namsg.yz-win.json文件

3. 在com.wudi.chrome.namsg.yz-win.json文件中指定了链接输入输出的exe

相关文章
相关标签/搜索