background.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //此变量用于监听是否加载了新的页面(包括新窗口打开),如果是,增加变量值,用于传回后台。
  2. // var tabList = []; //用来记录打开的新的tab的id
  3. // var nowTabId = null;
  4. // var nowTabIndex = 0; //重要变量!!
  5. // var parameterNum = 1; //默认参数索引值
  6. //
  7. // chrome.storage.local.set({ "parameterNum": 1 }); //修改默认的参数索引值
  8. // // chrome.tabs.update(6,{"active":true}) //一行就可以切换标签页
  9. // chrome.tabs.onActivated.addListener(function(activeInfo) {
  10. // nowTabId = activeInfo.tabId; //记录现在活动的tabid
  11. // if (tabList.indexOf(nowTabId) != -1) {
  12. // nowTabIndex = tabList.indexOf(nowTabId);
  13. // }
  14. // });
  15. // // 监听来自content-script的消息
  16. // chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  17. // if (request.type == 0) {
  18. // if (tabList.indexOf(sender["tab"]["id"]) < 0) { //元素不存在加入数组
  19. // tabList.push(sender["tab"]["id"]);
  20. // }
  21. // nowTabIndex = tabList.indexOf(nowTabId);
  22. // sendResponse({ type: 0, "msg": "Get!" }); //回传一个消息
  23. // } else if (request.type == 1) { //前台询问参数索引值
  24. // sendResponse({ type: 1, "value": parameterNum }); //回传一个消息
  25. // } else if (request.type == 2) {
  26. // let message = {
  27. // type: 2, //消息类型,2代表键盘输入
  28. // message: { "keyboardStr": request.value, "xpath": request.xpath, "id": request.id } // {}全选{BS}退格
  29. // };
  30. // ws.send(JSON.stringify(message));
  31. // chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  32. // // 获取当前选项卡的 ID
  33. // const tabId = tabs[0].id;
  34. // const url = tabs[0].url;
  35. //
  36. // // 停止当前页面的加载
  37. // // chrome.tabs.executeScript(tabId, {code: 'window.stop();'});
  38. // });
  39. // } else if (request.type == 3) {
  40. // let tmsg = request.msg;
  41. // tmsg.tabIndex = nowTabIndex; //赋值当前tab的id
  42. // let message = {
  43. // type: 3, //消息类型,3代表元素增加事件
  44. // from: 0, //0代表从浏览器到流程图,1代表从流程图到浏览器
  45. // message: {"pipe": JSON.stringify(request.msg)}
  46. // };
  47. // console.log(message);
  48. // ws.send(JSON.stringify(message));
  49. // }
  50. // });
  51. // 打开一个 web socket
  52. let ws = new WebSocket("ws://localhost:8084");
  53. ws.onopen = function () {
  54. // Web Socket 已连接上,使用 send() 方法发送数据
  55. console.log("已连接");
  56. let message = {
  57. type: 0,
  58. message: {
  59. id: 0, //socket id
  60. }
  61. };
  62. this.send(JSON.stringify(message));
  63. };
  64. ws.onmessage = function (evt) {
  65. evt = JSON.parse(evt.data);
  66. if (evt["type"] == "0") { //0代表更新参数添加索引值
  67. chrome.storage.local.set({ "parameterNum": parseInt(evt["value"]) }); //修改值
  68. }
  69. };
  70. ws.onclose = function () {
  71. // 关闭 websocket
  72. console.log("连接已关闭...");
  73. };