Chrome Native Messaging技术示例

本文介绍了如何使用Chrome Native Messaging技术实现web页面、浏览器扩展与本地消息主机之间的JSON通讯。重点强调manifest.json配置以及主机名称的大小写规范。
摘要由CSDN通过智能技术生成
Title:Chrome Native Messaging技术示例
Author: kagula
Revison: 1
First publish Date:2015-10-23
Last modify date:2015-10-23


环境:
 [1]Visaul Stuio 2013 Update4
 [2]Google chrome 45.0.2454.101 
 [3]Windows7 SP1 64bits
 
正文
    在Chrome浏览器中,Native Messaging是唯一能让web page调用Native API的技术,这里记录要达到这个目的所要涉及到的知识。

    web page、extension和native messaging host三者之间用UTF-8编码的json字符串通讯。


    web page同extension之间进行message exchange,extension同native messaging host之间message exchange.
    最后由native messaging host access native OS API.
    本文主体由native messaging host、extension、web page三部份组成。
    这里基于google echo示例代码做了大量修改,用于实现web page同native application的通讯。
    
第一部份 native messaging host
    native messaging host从stdin读取消息从stdout中返回消息,通过stderr返回错误消息给chrome。
    一次最多能向native messaging host发送4GB字节的请求,native messaging host一次最多能返回1M字节的消息。
    这部份由C++源文件和manifest文件两部份组成:
    
在VisualStudio中新建chrome_native_messaging_host win32 console project,把下面的文件加进去并编译。
下面是native messaing host源文件清单   
    
#include "stdafx.h"

/*
Title: native messaging c++ program
Author: kagula
Date: 2015-10-22
*/

#include <stdio.h>
#include <fcntl.h>
#include <io.h>   

#include "SimpleLog.h"

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

void sendMessage(const string &strMsg)
{
	// We need to send the 4 bytes of length information
	unsigned int len = strMsg.length();
	std::cout << char(((len >> 0) & 0xFF))
		<< char(((len >> 8) & 0xFF))
		<< char(((len >> 16) & 0xFF))
		<< char(((len >> 24) & 0xFF));
	//output integer value directly will lead byte order problem.

	// Now we can output our message
	cout << strMsg;
	cout.flush();
}


simpleLogClass logger;

int _tmain(int argc, _TCHAR* argv[])
{
	logger.fileMaxSize = 64*1024;
	logger.fileName = "e:\\a.log";
	logger.fileOldName = "e:\\a.old.log";

	_setmode(_fileno(stdin), O_BINARY);
	_setmode(_fileno(stdout), O_BINARY);
	_setmode(_fileno(stderr), O_BINARY);

	int bufSize;
	do 
	{
		bufSize = 0;

		//unlike >> operator and scanf function, 
		//read function will wait until read all 4 bytes!
		cin.read((char*)&bufSize, 4);

		stringstream ss;
		ss << "bufSize = " << bufSize << endl;
		logger.info(ss.str());

		if (bufSize> 0)
		{
			char *pData = new char[bufSize+1];
			memset(pData, 0, bufSize + 1);
			cin.read(pData, bufSize);

			string response = "{\"echo\":";
			response.append(pData);
			response.append("}");

			sendMessage(response);
			logger.info(response);

			delete pData;
		} else
		{
			break; 
		}
	} while (true);
	
	
	return 0;
}


下面是com.google.chrome.example.echo-win.json源文清单

{
  "name": "com.google.chrome.example.echo",
  "description": "Chrome Native Messaging API Example Host",
  "path": "chrome_native_messaging_host.exe",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
  ]
}


    manifest文件和C++源
  • 5
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值