谷歌插件的运行是在一个封闭的沙盒中的,所以要想从本地读出相应的数据无法达到。这时,我们可以考虑SOCKET通信。
原理:本地运行一个SOCKET监听某个端口,插件向该服务端口发送信息,服务处理后,再返回相应的数据。
服务中使用的是SOCKET,实现代码如下
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Web;//需要引用System.Web程序集 namespace ChromeService { public class ListenService { private TcpListener listener; private int listenPort = 20000; private Encoding encoding_ = Encoding.UTF8; public ListenService() { try { listener = new TcpListener(IPAddress.Parse("127.0.0.1"), listenPort); listener.Start(); Thread listenThread = new Thread(StartListen); listenThread.Start(); } catch (Exception ex) { Console.WriteLine("ListenService Exception :" + ex.Message); } } public void StartListen() { while (true) { try { Socket newSocket = listener.AcceptSocket(); if (!newSocket.Connected) { continue; } Byte[] receive = new Byte[10240]; newSocket.Receive(receive, receive.Length, 0); byte[] backData = handle(receive); newSocket.Send(backData, backData.Length, 0); newSocket.Close(); continue; } catch (Exception ex) { Console.WriteLine(ex.Message); } } } public byte[] handle(byte[] cmdPack) { string bufferStr = encoding_.GetString(cmdPack); byte[] backData = new byte[2] { 0, 0 }; //只处理"get"请求类型 if (bufferStr.Substring(0, 3) != "GET") { return backData; } int dataBegin = bufferStr.IndexOf(@"/") + @"/".Length; int dataEnd = bufferStr.IndexOf("HTTP"); string cmdPackStr = bufferStr.Substring(dataBegin, dataEnd - dataBegin); string cmdData = ""; if (cmdPackStr.Length > 2) { cmdData = cmdPackStr.Substring(2); cmdData = HttpUtility.UrlDecode(cmdData);//如果插件发送的数据有中文时,需要进行解码 } string cmd = cmdPackStr.Substring(0, 2).Trim();//从插件发送来的命令占两个字符00-FF switch (cmd) { case "0F": { backData = encoding_.GetBytes("OK"); break; } default: { backData = encoding_.GetBytes("DEFAULT"); break; } } return backData; } } }
在插件中使用的是XHR或者JQUERY的AJAX,实现代码如下
/// <reference path="../libs/jquery-1.7.2.min.js"/> var Communicate = {}; Communicate.send = function (cmd, data, callback) {//jquery的ajax实现 var sendCmdData = ""; if (cmd === undefined || cmd === null) { return; } sendCmdData += cmd; if (data !== undefined || data !== null) { sendCmdData += data; } var url = 'http://localhost:20000/' + sendCmdData $.ajax({ url: url, async: false, success: callback }); } Communicate.send = function (cmd, data, callback) {//xhr实现 var xhr = new XMLHttpRequest(); var sendCmdData = ""; if (cmd === undefined || cmd === null) { return; } sendCmdData += cmd; if (data !== undefined || data !== null) { sendCmdData += data; } xhr.onreadystatechange = function () { if (xhr.readyState == 4) { if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) //200:Success.304:Tell browser to read cache. { //alert(xhr.responseText); if (callback === undefined || callback === null) { return; } callback(xhr.responseText); } } } xhr.open("GET", 'http://localhost:20000/' + sendCmdData, true); xhr.send(null); }
转载请注明出处 http://blog.csdn.net/xxdddail/article/details/13507379