谷歌浏览器插件开发教程8
利用workerman和 谷歌插件通信相结合
1 首先获取groupid 保证插件和inject 同个房间 这样还可以用workerman 相互发送
先插件获得groupid 传递到inject
var groupid; $('.chongxinlianjie').on('click', function() { var wsUri = "ws://111.229.30.162:8282"; ws = new WebSocket(wsUri); //请求服务端后 成功后触发 ws.onopen = function() { groupid = Date.parse(new Date()); var str2 = { "from": "mypanel", "tag": 'begin', "groupid": groupid, }; var str2 = JSON.stringify(str2); ws.send(str2); chrome.tabs.query({ active: true, currentWindow: true }, function(tab) { //获取当前tab //向tab发送请求 chrome.tabs.sendMessage(tab[0].id, { action: "begin", groupid: groupid, }, function(response) { //test.js的数据 }); }); }; ws.onmessage = function(evt) { var data = JSON.parse(evt.data); if (data.code == 100) { var list = data.data; var tag = list.tag; var from = list.from; } else { alert(data.msg); } }; //我关闭服务的时候 就会触发 ws.onclose = function(evt) { console.log('WebSocketClosed!'); }; ws.onerror = function(evt) { console.log('WebSocketError!'); }; })
contentscript只是中间的过渡
function injectCustomJs(jsPath) { jsPath = jsPath || 'inject.js'; var temp = document.createElement('script'); temp.setAttribute('type', 'text/javascript'); // 获得的地址类似:chrome-extension://ihcokhadfjfchaeagdoclpnjdiokfakg/js/inject.js temp.src = chrome.extension.getURL(jsPath); temp.onload = function() { // 放在页面不好看,执行完后移除掉 this.parentNode.removeChild(this); }; document.head.appendChild(temp); } injectCustomJs(); // chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { window.postMessage({ cmd: 'sendinject', data: request }, '*'); });
var groupid; window.addEventListener("message", function(e) { var info = e.data; var cmd = info.cmd; if (cmd == "sendinject") { var data = info.data; var action = data.action; if (action == "begin") { console.log('inject开始初始化'); groupid = data.groupid; var wsUri = "ws://111.229.30.162:8282"; ws = new WebSocket(wsUri); //请求服务端后 成功后触发 ws.onopen = function() { var str2 = { "from": "inject", "tag": 'begin', "groupid": groupid, }; var str2 = JSON.stringify(str2); ws.send(str2); }; ws.onmessage = function(evt) { var data = JSON.parse(evt.data); if (data.code == 100) { var list = data.data; var from = list.from; var tag = list.tag; var title = list.title; if (from == "mypanel") { console.log('injectjs收到的数据'); console.log(list); } } else { alert(data.msg); } }; //我关闭服务的时候 就会触发 ws.onclose = function(evt) { console.log('WebSocketClosed!'); }; ws.onerror = function(evt) { console.log('WebSocketError!'); }; } } }, false);
上面是injectjs
最后发现workerman代码
public static function onMessage($client_id, $message) { $data = json_decode($message, 'true'); $tag = $data['tag']; $from = $data['from']; $groupid = $data['groupid']; $fid = $client_id; if ($tag == 'begin') { $time = time(); $insertdata = array('laizi' => $from, 'fid' => $fid, 'groupid' => $groupid, 'time' => $time); commoninsertinfo($insertdata, 'cocoschajian'); } else { $sql = 'select fid from cocoschajian where groupid=? and laizi=? limit 1'; if ($from == 'mypanel') { $to = 'inject'; } else { $to = 'mypanel'; } $fid = commongetinfo($sql, array($groupid, $to))[0]['fid']; if (empty($fid)) { $data = json_encode(array('code' => 323, 'msg' => '服务器错误 请重新链接')); } else { $data = json_encode(array('code' => 100, 'msg' => '发送成功', 'data' => $data)); } Gateway::sendToClient($fid, $data); } // print_r($data); // 向所有人发送 }
这个后台代码只是个桥梁
如果遇到什么不懂的地方直接关注公众号留言(本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。)
作者:newmiracle
出处:https://www.cnblogs.com/newmiracle/