最近项目用到浏览插件的开发,IE用到的是BHO,chrome打算做成扩展。

但是和ie有一点不同,chrome扩展是基于html+js+css开发的,那么就会有二个问题

1. 代码和算法等容易被别人复制和盗用。

2. 有部份功能需要用native code来实现。

所以仅仅使用chrome扩展是不够的。后来想到了native client,但是这个需要在chrome://flags/中开启相应的设置。而且也不是很安全。

最后选择了chrome的native messaging,原理我就不多说了,这里只介绍如何实现。

chrome扩展中,manifest.json有几点要注意

"background": {
"scripts": [
"res/js/background.js"
]
},

上面个background.js,是必须要加的,后面会介绍原因

"content_scripts": [
{
"all_frames": false,
"css": [
],
"js": [
"res/js/contentscript.js"
],
"matches": [
"http://*/*",
"https://*/*"
],
"run_at": "document_end"
}
],

这个表示在页面加载结束时调用contentscript.js

"permissions": [
"nativeMessaging"
],

权限别忘了加上。

contentscript.js中,只有一段代码

$(function () {

chrome.extension.sendRequest("this is a request");

});

重点在background.js中了,不多说,贴代码

var port;

function onDisconnected() {

port = null;

}

function onNativeMessage(message) {

alert(JSON.stringify(message));

}

function connect() {

port = chrome.runtime.connectNative('com.beeper2g.demo.nativemessage');

port.onMessage.addListener(onNativeMessage);

port.onDisconnect.addListener(onDisconnected);

}

chrome.extension.onRequest.addListener(function(data, sender) {

if (data.length > 0) {

connect();

port.postMessage(data);

}

});

注意方法 chrome.runtime.connectNative 是属于content script,这个不能在页面中调用,所以只能放在backgroud.js中,上面提到过。

扩展中的东西就这么些,讲一下注册以及调用方式

注册和解注册就是在注册表中添加相应的值就OK了,发二个bat,一个install,一个uninstall

install.bat内容为

:: %~dp0 is the directory containing this bat script and ends with a backslash.

REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\com.beeper2g.demo.nativemessage" /ve /t REG_SZ /d "%~dp0com.beeper2g.demo.nativemessage.json" /f

uninstall.bat内容为

:: Deletes the entry created by install_host.bat

REG DELETE "HKCU\Software\Google\Chrome\NativeMessagingHosts\com.beeper2g.demo.nativemessage" /f

其中的com.beeper2g.demo.nativemessage.json中内容十分简单

{

"name": "com.beeper2g.demo.nativemessage",

"description": "Demo程序",

"path": "X:\XXX\chromeService.exe",

"type": "stdio",

"allowed_origins": [

"chrome-extension://fkdejdpefhhjdjdlmalkegclifdofipm/"

]

}

字段自己去对应,path为绝对路径。chrome-extension://fkdejdpefhhjdjdlmalkegclifdofipm/ 中的id别忘了与扩展一致

c++部份

// chromeHost.cpp : Defines the entry point for the console application.

//

using namespace std;

void output(const string& str)

{

unsigned int len = str.length();

cout<< char(((len>>0) & 0xFF))

<< char(((len>>8) & 0xFF))

<< char(((len>>16) & 0xFF))

<< char(((len>>24) & 0xFF));

cout << str << endl;

}

string getInput()

{

string input;

unsigned int reqLen = 0;

cin.read(reinterpret_cast<char*>(&reqLen) ,4);

for (int i = 0; i < reqLen; i++) {

input += getchar();

}

return input;

}

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

string input = getInput();

MessageBoxA(NULL, input.c_str(), NULL, NULL);

output("this is response");

return 0;

}

重中之重就是注意通讯中都有四个字节的包头,表示包的大小,这个不能丢,否则不会成功。

Google chrome浏览器中通过扩展调用本地应用程序以及和程序相互通讯(C++)的更多相关文章

  1. Google Chrome浏览器中如何使用命令

    Google Chrome浏览器中如何使用命令 | 浏览:2974 | 更新:2014-02-23 23:12 | 标签:chrome 1 2 3 分步阅读 Google Chrome浏览器有很多的特 ...

  2. 将Chrome浏览器中的扩展程序导出为crx插件文件

    将Chrome浏览器中安装的插件程序导出为crx插件文件 以360急速浏览器为例进行导出crx插件程序 1.在Chrom商店中找到需要的插件,安装到浏览器的扩展程序里面()IDM Integratio ...

  3. Google Chrome浏览器插件入门开发

    --1. 在html文件中引用js 文件 --2.在Google Chrome中开发简单插件 1.首先,简单说明一下在html 中引用js 文件: 将kittenbook.html 和 kittenb ...

  4. 哇塞,原来自己写 Google Chrome 浏览器扩展(插件)这么容易!

    1. 首先新建一个记事本,命名为 manifest.json,这是写 Google Chrome 浏览器扩展必须的文件 { "manifest_version": 2, " ...

  5. 360chrome,google chrome浏览器使用jquery.ajax加载本地html文件

    使用360chrome和google chrome浏览器加载本地html文件时,会报错,提示: XMLHttpRequest cannot load file:///Y:/jswg/code/html ...

  6. CEF中文教程(google chrome浏览器控件) -- Windows下编译Chromium

    CEF中文教程(google chrome浏览器控件) -- CEF简介 2013-04-10 16:48 42928人阅读 评论(4) 收藏 举报  分类: CEF(2)    目录(?)[+]   ...

  7. 详解Google Chrome浏览器(操作篇)(下)

    开篇概述 由于最近忙于公司产品的架构与研发,已经三个多月没有写博客了,收到有些朋友的来信,问为什么不及时更新博客内容呢,他们说他们正期待着某些内容.对此,非常抱歉,那么我在此也给各位朋友一些承诺,从即 ...

  8. 【ASP.NET MVC系列】浅谈Google Chrome浏览器(操作篇)(下)

    ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...

  9. 详解Google Chrome浏览器(操作篇)(上)

    开篇概述 在上篇博客中详解Google Chrome浏览器(理论篇)一文中,主要讲解了Chrome 搜索引擎使用.Chrome安装和基本操作.Chrome 基本架构.多线程等原理性问题,这篇将重点讲解 ...

随机推荐

  1. 分布式监控系统--zabbix

    1Zabbix简介 Zabbix 是一个企业级的分布式开源监控方案. 2.监控系统架构 C/S架构 客户端/服务器端,这种架构适合规模较小,处于同一地域的环境 C/P/S 客户端/代理端/服务器端/, ...

  2. linux下python2升级python3,python2和python3并存

    wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz 解压:tar -xzvf Python-3.6.4.tgz cd Pytho ...

  3. Linux系统上安装JDK和Tomcat服务器

    一.安装JDK 1.查看当前Linux系统是否已经安装java  输入命令: rpm -qa | grep java 2.卸载两个openJDK  输入命令:rpm -e --nodeps 3.上传j ...

  4. PageRank_网页排名_MapReduceJava代码实现思路

    PageRank 1.    概念 2.    原理   3.    java代码实现思路   1.定义收敛标准     每次算出新的pr-oldpr=差值 ,所有页面的差值累加 ,除以pagecou ...

  5. 怎样实现给DEDE的栏目增加栏目图片(2)

    2.3 打开dede/templets/catalog_edit.htm页面,查找 栏目名称:

  6. 邓_ecshop

    =========================================== 版本错误: error_reporting(0); ============================== ...

  7. 【编程技巧】NSDate,NSDateFormatter,NSTimeInterval

    //获取日期 todaysDate=[NSDate date]; //显示日期和时间 dateFormat = [[NSDateFormatter alloc] init];//NSDate没有自己的 ...

  8. HTML <td> 标签的 rowspan 属性

    rowspan 属性规定单元格可横跨的行数. colspan  属性规定单元格可横跨的列数.

  9. spring问题:java.lang.NoClassDefFoundError: org/aspectj/weaver/tools/PointcutPrimitive

    问题描述: spring的:java.lang.NoClassDefFoundError: org/aspectj/weaver/tools/PointcutPrimitive 解决方案: 缺少jar ...

  10. Linux - ubuntu 设置固定ip和设置dns

    ubuntu 设置固定ip和设置dns 1.ifconfig 查看网卡名称 root@jiqing-virtual-machine:~# ifconfig ens32 Link encap:以太网 硬 ...