本插件是基于 nativeMessaging
crx 打包内容:
1. manifest.json
- {
- "manifest_version": 2,
- "name": "demo",
- "version": "1.0",
- "description": "simple demo",
- "browser_action": {
- "default_icon": "13.png",
- "default_title": "simple demo",
- "default_popup": "popup.html"
- },
- "app": {
- "launch": {
- "local_path": "popup.html"
- }
- },
- "permissions": ["nativeMessaging"]
- }
2. popup.html
- <div style="width:200px;">
- <p id="label">
- This is a simple Demo
- </p>
- <button id="yes-button">
- Yes
- </button>
- </div>
- <script type="text/javascript" src="script.js">
- </script>
3. script.js
- document.getElementById('yes - button').addEventListener('click',
- function() {
- var port = chrome.runtime.connectNative('com.google.chrome.demo');
- port.onDisconnect.addListener(function() {
- console.log("Disconnected");
- });
- port.postMessage({
- text: "Hello, my_application"
- });
- port.onMessage.addListener(function(message, sender, sendResponse) {
- var strs = sendResponse;
- console.log("Received" + strs + " " + message.text + " " + JSON.stringify(message));
- })
- });
将以上 3 个文件和一个图片放到一个文件夹中,进入 chrome://extensions/ ,点选开发者模式,选择,打包扩展程序,即可 将文件夹中的文件打包成 crx 插件,在将插件拖入,即可完成安装,只是现在 google 要求插件必须是由应用商店下载的,所以重新打开 google 浏览器后,这个插件就自动停用,且无法再启用。
与 exe 的通信:
1. host.json
- {
- "name": "com.google.chrome.demo",
- "description": "Native messaging API host",
- "path": "D:\\Documents\\Visual Studio 2008\\ProjectsGoogle\\googleDemo\\Release\\googleDemo.exe",
- "type": "stdio",
- "allowed_origins": [
- "chrome-extension://agajdodbfodoocphplebagbgakgaodjj/"
- ]
- }
path:就是你想调用的 exe 完整路径
chrome-extension:是你刚才打包的 crx 安装后,显示在界面上的 ID
name:链接名
2. host.reg
- Windows Registry Editor Version 5.00
- [HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\com.google.chrome.demo]
- @="D:\\360安全浏览器下载\\host.json"
创建注册表项,com.google.chrome.demo 就是刚才的 host.json 文件中的 name,项数据是 host.json 的路径
点击 YES 按键后,就将 exe 启动了,以上即是 chrome 插件启动本地 exe 程序内容
下面写一下 exe 中的内容:C++
在 OnInitDialog 函数中创建线程
- HANDLE hThreadcom=CreateThread(NULL,
- 0,
- (LPTHREAD_START_ROUTINE)commandThreadFunc,
- NULL,
- 0,
- NULL);
HANDLE hThreaddata=CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)dataThreadFunc,
NULL,
0,
NULL);
线程处理函数
- void commandThreadFunc()
- {
- char temp[128];
- stringstream strs;
- while(1)
- {
- int ch;
- if((ch = getchar())!=EOF)
- {
- sprintf(temp,"%c",ch);
- strs<<temp;
- OutputDebugString(strs.str().c_str());
- }
- }
- }
- void dataThreadFunc()
- {
- while(1)
- {
- //send to stdout
- // Define our message
- std::string message = "{\"text\": \"This is a response message\"}";
- // Collect the length of the message
- unsigned int len = message.length();
- // We need to send the 4 bytes of length information
- std::cout << char(((len>>0) & 0xFF))
- << char(((len>>8) & 0xFF))
- << char(((len>>16) & 0xFF))
- << char(((len>>24) & 0xFF));
- // Now we can output our message
- std::cout << message;
- }
- }
nativeMessaging 通信是基于 io 流的,使用 getchar()函数即可接收发送到 exe 的消息
现在的问题是接收 exe 发送到 chrome 插件的有问题,接收不到,在 console 里看不到,不知道为什么?还有就是 nativeMessaging 不能进行实时通信,我的需求是本地 exe 会一直向 google 浏览器发送数据,基本上是 100-200 包每秒,在浏览器上绘制图形,现在还有待研究中..........
来源: