Chrome Native Messaging 与本地程序之间的通信
最近项目上出现了web打印不稳定的问题,师父决定web调用本地打印程序,在查阅了相关资料和加了几个相关群咨询后得知新版的chrome不支持NNAPI了,最好用Native Messaging来处理,经过一段时间的学习和试错,终于完成了,所以把步骤与总结发出来。过程参考http://blog.csdn.net/talking12391239/article/details/38498557
首先,我们需要写一个chrome插件。学习相关知识可以去《Chrome扩展及应用开发》。这个插件包含了三个文件:manifest.json(这个是插件配置信息文件),background.js(文件名可改, 后台文件),content.js(与页面交互,传递消息给后台)。首先我们来看manifest.json这个文件,
{
"name" : "myApp",
"version" : "1.0.1",
"description" : "Launch APP ",
"background" : { "scripts": ["background.js"] },
"permissions" : [
"nativeMessaging",
"tabs",
"http://*/*"
],
"content_scripts": [
{
"matches": ["http://*/*"],
"js": ["content.js"]
}
],
"minimum_chrome_version" : "6.0.0.0",
"manifest_version": 2
}
上面可以看出,这个文件在哪里引用了background.js和content.js,这两个文件的名字是可以更改的,permissions里的"http://*/*"与 content_scripts里的"matches"的意思是允许所有的http请求的url调用。"nativeMessaging" 代表要在这个插件中允许调用这种方法。
接下来,看一下后台文件background.js:
var port = null;
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.type == "launch"){
connectToNativeHost(request.message);
}
return true;
}); //onNativeDisconnect
function onDisconnected()
{
console.log(chrome.runtime.lastError);
console.log('disconnected from native app.');
port = null;
} function onNativeMessage(message) {
console.log('recieved message from native app: ' + JSON.stringify(message));
} //connect to native host and get the communicatetion port
function connectToNativeHost(msg)
{
var nativeHostName = "com.dhcc.lisprint";
port = chrome.runtime.connectNative(nativeHostName);
console.log(port)
port.onMessage.addListener(onNativeMessage);
port.onDisconnect.addListener(onDisconnected);
port.postMessage({message: msg});
}
这个js文件里有两个十分重要的方法chrome.runtime.onMessage.addListener和connectToNativeHost。chrome.runtime.onMessage.addListener里可以看出,这个响应事件的方式为"launch"时,调用connectToNativeHost。
connectToNativeHost链接com.dhcc.lisprint这个服务。同时postMessage。
接下来,我们来看content.js。
var launch_message;
document.addEventListener('myCustomEvent', function(evt) {
console.log(evt);
chrome.runtime.sendMessage({type: "launch", message: evt.detail}, function(response) {
});
}, false);
这个文件的意图很明显,当界面传来myCustomEvent这个事件,向后台文件background.js发送type为launch的信息。
以上就是chrome插件相关部分,我们可以看出并未写如何调用下面介绍native messaging调用部分。首先我们新建一个文件,manifest.json(名称可以修改)。
{
"name": "com.dhcc.lisprint",
"description": "Dhcc imedical lis print config app",
"path": "C:\\Test.exe",
"type": "stdio",
"allowed_origins": [
"chrome-extension://acpcejomihdkopjnnijfmnpdgfkmfhkj/"
]
}
可以很明显的看出,name即我们background.js里面要调用的host,path即我们要调用的本的应用程序。allowed_origins是什么呢?即我们安装完chrome插件的ID,如图:

那如何让chrome知道这个json就是com.dhcc.print的配置文件呢?这里我们还需要进行下一步,修改注册表:
运行-> Regedit -> HKEY_Current_User->Software->Google->Chrome->新建一个叫NativeMessagingHosts的项->新建一个叫com.my_company.my_application的项, 同时在这个项里默认值设置为我们Native Messaging 的 位置 即这个json文件的位置,如C:\\Native\\manifest.json。这样便完成了native masseging的设置。
接下来我们看界面是如何调用的
<html>
<head>
<script> function startApp() {
var evt = document.createEvent("CustomEvent");
evt.initCustomEvent('myCustomEvent', true, false, "哈哈哈哈");
// fire the event
document.dispatchEvent(evt);
} </script>
</head>
<body>
<button type="button" onClick="startApp()" id="startApp">startApp</button>
</body>
</html>
里面有一个简单的按钮, 这个按钮会启动方法, 新建一个名叫"myCustomEvent"的事件, 同时附带有我们要传的信息, 并发布这个事件。 这样我们插件中的Content.js 就可以接收并响应这个事件了!
下面,我们可以看下如何接收页面传递给exe的信息。
private void Form1_Load(object sender, EventArgs e)
{
string chromeMessage = OpenStandardStreamIn(); MessageBox.Show(chromeMessage); } private static string OpenStandardStreamIn()
{
//// We need to read first 4 bytes for length information
Stream stdin = Console.OpenStandardInput();
int length = ;
byte[] bytes = new byte[];
stdin.Read(bytes, , );
length = System.BitConverter.ToInt32(bytes, ); string input = "";
for (int i = ; i < length; i++)
{
input += (char)stdin.ReadByte();
} return input;
}
以上就是全部内容啦~
Chrome Native Messaging 与本地程序之间的通信的更多相关文章
- 一个实现浏览器网页与本地程序之间进行双向调用的轻量级、强兼容、可扩展的插件开发平台—PluginOK中间件
通过PluginOK中间件插件平台(原名本网通WebRunLocal)可实现在网页中的JavaScript脚本无障碍访问本地电脑的硬件.调用本地系统的API及相关组件,同时可彻底解决ActiveX组件 ...
- Google Chrome Native Messaging开发实录(二)Chrome Extension扩展
接上一篇<Google Chrome Native Messaging开发实录(一)背景介绍>的项目背景,话不多说,有关Chrome Extension介绍和文档就不展开了,直接上代码. ...
- Google Chrome Native Messaging开发实录(一)背景介绍
最近接手了一个针对Google Chrome的需求,最终是使用Native Messaging来实现的.通过这个连载,将把本次开发从方案选定到编码的全部过程进行完整的回顾,并记录开发过程中踩过的各种坑 ...
- 通过私有协议Chrome浏览器页面打开本地程序
近期方有这样的要求:这两个系统,根据一组Chrome开展,根据一组IE开展,需要Chrome添加一个链接,然后进入IE该系统的开发.这,需要Chrome跳转到创建一个链接IE浏览器指定的页面.同时也实 ...
- Chrome浏览器扩展开发系列之十四:本地消息机制Native messaging
通过将浏览器所在客户端的本地应用注册为Chrome浏览器扩展的“本地消息主机(native messaging host)”,Chrome浏览器扩展还可以与客户端本地应用之间收发消息. 客户端的本地应 ...
- Delphi 两个应用程序(进程)之间的通信
两个应用程序之间的通信实际上是两个进程之间的通信.由于本人知识有限,决定应用消息来实现.需要用到的知识: 1.RegisterWindowMessage(); //参数类型:pchar:返回值:Lon ...
- Chrome 插件: 起动本地应用 (Native messaging)
Chrome 插件: 起动本地应用 (Native messaging) www.MyException.Cn 网友分享于:2014-08-01 浏览:3次 Chrome 插件: 启动本地应用 ...
- Chrome 小工具: 启动本地应用 (Native messaging)
最近遇到一个新的问题.需要使用Chrome 插件, 从我们对我们当地的一个网站之一启动C#应用,同时通过本申请值执行不同的操作. 在这里记录下解决的过程.以便以后查找 首先我们须要新建一个google ...
- 如何使用Native Messaging API 打开window程序
问 如何使用Native Messaging API 打开window程序 cmd javascript terminal chrome Tychio 2013年03月26日提问 关注 1 关注 收藏 ...
随机推荐
- oracle分页sql模板
select t2.* from (select t1.*,rownum rn from (select * from mytest) t1 where rownum<=860010) t2 w ...
- Commenting and uncommenting XML via Python
转载: http://stackoverflow.com/questions/8764017/commenting-and-uncommenting-xml-via-python from xml.d ...
- HTML5 Canvas 画钟表
画钟表是2D画图的老生常谈,我也不能免俗弄了一个.代码如下: <!DOCTYPE html> <html lang="utf-8"> <meta ht ...
- SQL JOB 调用 SSIS package 权限问题
来自: http://www.cnblogs.com/sodacc/archive/2012/11/26/2789135.html 第一次用SQL给SSIS包排JOB的时候,都会遇到这样一个问题:单独 ...
- 标准库Queue的实现
跟上篇实现stack的思路一致,我增加了一些成员函数模板,支持不同类型的Queue之间的复制和赋值. 同时提供一个异常类. 代码如下: #ifndef QUEUE_HPP #define QUEUE_ ...
- Font Awesome入门教程
Font Awesome 图标 Font Awesome 是一套绝佳的图标字体库和CSS框架. Font Awesome 字体为您提供可缩放矢量图标,它可以被定制大小.颜色.阴影以及任何可以用CSS的 ...
- Unix环境部署
http://www.cnblogs.com/chuyuhuashi/p/4423699.html 分布式javahttp://www.cnblogs.com/tangyanbo/p/4499485. ...
- Windows web服务器搭建---阿里云
前提步骤: 1)申请域名---- 阿里云.花生壳.万维网等等. 2)云主机购买-----阿里云.腾讯云.京东云等等. 3)网站备案,此步骤最长. 4)建立网站 5)部署网站 下面主要介绍如何部署网 ...
- Java和C++ 比較
总体差别 1. C/C++是直接执行在机器上(编译后为机器码),而java编译后产生*.class文件(字节码)是执行在java虚拟机上在(JVM),经过JVM解译(机器码)再放到真实机器上执行. J ...
- 在spring mvc中利用ajax批量删除数据
1.前台代码: $("#batchdelete").click(function(){ $.ajax({ type: "post", url: url, dat ...