開發說明
1、瀏覽器插件實現
manifest.json文件內容。每個chrome的插件都需要該文件,該文件記錄插件的關鍵信息
{ "name" : "callapp", "version" : "1.0.1", "description" : "call local application", "background" : { "scripts": ["background.js"] }, "icons": { "16": "16.png", "128": "128.png" }, "permissions" : [ "nativeMessaging", "contextMenus", "tabs" ], "minimum_chrome_version" : "6.0.0.0", "manifest_version": 2 }
其中由於需要在右鍵菜單中增加選項,在permissions中增加"nativeMessaging"、"contextMenus"兩項;需要和本地程序進行通訊,增加"nativeMessaging"項
maniffest.json所需要的background.js
//Author: jyx //Date: 2014.10.11 //Description: This is a javaScript file use for handle contextMenus action //When click the contextMenus, it will sent the infomation to native app //connect to native app var port = null; var nativeHostName = "com.ctrip.ops.mysql.callapp";//chrome與本地程序通信的橋梁,根據該名稱進行配置項的尋找。windows下在注冊表HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts內尋找,linux下在目錄/etc/opt/chrome/native-messaging-hosts/尋找該名稱的json文件(本例子為com.ctrip.ops.mysql.callapp.json) //onNativeDisconnect function onDisconnected() { //alert("連接到FastDownload服務失敗: " + chrome.runtime.lastError.message); port = null; } //connect to native host and get the communicatetion port function connectToNativeHost() { port = chrome.runtime.connectNative(nativeHostName);//根據配置文件連接到本地程序 port.onDisconnect.addListener(onDisconnected); } //調用connectToNativeHost函數連接到本地程序,完成后使用port.postMessage函數將纖細傳遞給應用程序 //將信息寫入I/O流與本地程序通信 function getClickHandler() { return function(info, tab) { connectToNativeHost(); port.postMessage(info.linkUrl); }; }; //在瀏覽器啟動時即創建右鍵菜單,在頁面鏈接上右擊鼠標會顯示該菜單,當點擊"start program"的時候就會調用getClickHandler()函數處理 chrome.contextMenus.create({ "title" : "start program", "type" : "normal", "id": "callapp", "contexts" : ["link"], "enabled": true, "onclick" : getClickHandler() });
程序中nativeHostName需要特殊說明,正是通過該變量,chrome的插件找到調用本地程序的配置
本文的實驗環境為windows7,在注冊表的HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts中創建對應的項,注冊表導出內容如下Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\com.ctrip.ops.mysql.callapp] @="D:\\temp\\chromeExtension\\callapp\\callapp.json"
linux下參考http://match-yang.blog.163.com/blog/static/2109902542014319103739996/
2、瀏覽器調用配置
callapp.json
該文件的路徑保存在注冊表的HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\com.ctrip.ops.mysql.callapp項內
{ "name": "com.ctrip.ops.mysql.callapp", "description": "Chrome call native app and sent message to app.", "path": "C:\\MyApp.exe", "type": "stdio", "allowed_origins": [ "chrome-extension://imcfacgnnkhdheiajocckejfmeiokgol/" ] }
3、本地應用程序例子
使用C#進行開發,C++參考http://match-yang.blog.163.com/blog/static/2109902542014319103739996/
using System; using System.IO; namespace ConsoleApplication8 { class Program { static void Main(string[] args) { log2file("--------------------program start at " + DateTime.Now.ToString() + "--------------------"); if (args.Length != 0) { for (int i = 0; i < args.Length; i++) log2file("arg " + i.ToString() + args[i]); string chromeMessage = OpenStandardStreamIn(); log2file("--------------------My application starts with Chrome Extension message: " + chromeMessage + "---------------------------------"); } log2file("--------------------program end at " + DateTime.Now.ToString() + "--------------------"); } static void log2file(string s) { FileStream fs = new FileStream(@"c:\test.log", FileMode.Append); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(s); sw.Close(); fs.Close(); } private static string OpenStandardStreamIn() { //// We need to read first 4 bytes for length information Stream stdin = Console.OpenStandardInput(); int length = 0; byte[] bytes = new byte[4]; stdin.Read(bytes, 0, 4); length = System.BitConverter.ToInt32(bytes, 0); string input = ""; for (int i = 0; i < length; i++) { input += (char)stdin.ReadByte(); } return input; } } }
說明:
background.js代碼根據如下鏈接,進行了一些非框架性的修改,主要是注釋,不過還是修改了一下author
http://match-yang.blog.163.com/blog/static/2109902542014319103739996/
參考:
講解如何在瀏覽器的右鍵菜單中增加
http://my.oschina.net/ruben/blog/92813
講解調用本地的應用程序
http://match-yang.blog.163.com/blog/static/2109902542014319103739996/
http://blog.csdn.net/talking12391239/article/details/38498557#