messageInteraction.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //实现与后台和流程图部分的交互
  2. if (window.location.href.indexOf("backEndAddressServiceWrapper") >= 0) {
  3. throw "serviceGrid"; //如果是服务器网页页面,则不执行工具
  4. }
  5. startMsg = { "type": 0, msg: "" };
  6. chrome.runtime.sendMessage(startMsg, function(response) {
  7. console.log(response.msg);
  8. }); //每次打开新页面的时候需要告诉后台
  9. console.log("test");
  10. chrome.extension.onMessage.addListener(
  11. function(request, sender, sendResponse) {
  12. if (request["type"] == 1);
  13. sendResponse("回答处理结果");
  14. }
  15. );
  16. function input(value) {
  17. let message = {
  18. "type": "InputText",
  19. "history": history.length, //记录history的长度
  20. "tabIndex": -1,
  21. "xpath": readXPath(nodeList[0]["node"], 0),
  22. "value": value,
  23. };
  24. let msg = { "type": 3, msg: message };
  25. chrome.runtime.sendMessage(msg);
  26. msg = { "type": 2, msg: value };
  27. chrome.runtime.sendMessage(msg);
  28. }
  29. //点击元素操作
  30. function sendSingleClick() {
  31. let message = {
  32. "type": "singleClick",
  33. "history": history.length, //记录history的长度
  34. "tabIndex": -1,
  35. "useLoop": false, //是否使用循环内元素
  36. "xpath": readXPath(nodeList[0]["node"], 0),
  37. };
  38. let msg = { "type": 3, msg: message };
  39. chrome.runtime.sendMessage(msg);
  40. }
  41. //采集单个元素
  42. function collectSingle() {
  43. let message = {
  44. "type": "singleCollect",
  45. "history": history.length, //记录history的长度
  46. "tabIndex": -1,
  47. "parameters": outputParameters,
  48. };
  49. let msg = { "type": 3, msg: message };
  50. chrome.runtime.sendMessage(msg);
  51. }
  52. //采集无规律多元素
  53. function collectMultiNoPattern() {
  54. let message = {
  55. "type": "multiCollectNoPattern",
  56. "history": history.length, //记录history的长度
  57. "tabIndex": -1,
  58. "parameters": outputParameters,
  59. };
  60. let msg = { "type": 3, msg: message };
  61. chrome.runtime.sendMessage(msg);
  62. }
  63. //采集有规律多元素
  64. function collectMultiWithPattern() {
  65. //先点击选择全部然后再
  66. let message = {
  67. "type": "multiCollectWithPattern",
  68. "history": history.length, //记录history的长度
  69. "tabIndex": -1,
  70. "loopType": 1,
  71. "xpath": "", //默认值设置为空
  72. "isDescendents": app._data.selectedDescendents, //标记是否采集的是子元素
  73. "parameters": outputParameters,
  74. };
  75. if (!detectAllSelected()) //如果不是全部选中的话
  76. {
  77. message.loopType = 2; //固定元素列表
  78. }
  79. if (message.loopType == 1) {
  80. message["xpath"] = app._data.nowPath;
  81. } else { //固定元素列表
  82. message["pathList"] = [];
  83. for (let i = 0; i < nodeList.length; i++) {
  84. message["pathList"].push(readXPath(nodeList[i]["node"], 0));
  85. }
  86. }
  87. let msg = { "type": 3, msg: message };
  88. chrome.runtime.sendMessage(msg);
  89. }
  90. //循环点击单个元素
  91. function sendLoopClickSingle(name) {
  92. let message = {
  93. "type": "loopClickSingle",
  94. "history": history.length, //记录history的长度
  95. "tabIndex": -1,
  96. "useLoop": true, //是否使用循环内元素
  97. "xpath": readXPath(nodeList[0]["node"], 0),
  98. "loopType": 0, //循环类型,0为单个元素
  99. "nextPage": false, //是否循环点击下一页
  100. };
  101. if (name == "下一页元素") {
  102. message.nextPage = true;
  103. }
  104. let msg = { "type": 3, msg: message };
  105. chrome.runtime.sendMessage(msg);
  106. }
  107. //循环点击每个元素
  108. function sendLoopClickEvery() {
  109. let message = {
  110. "type": "loopClickEvery",
  111. "history": history.length, //记录history的长度
  112. "tabIndex": -1,
  113. "xpath": "", //默认值设置为空
  114. "useLoop": true, //是否使用循环内元素
  115. "loopType": 1, //循环类型,1为不固定元素列表
  116. };
  117. if (!detectAllSelected()) //如果不是全部选中的话
  118. {
  119. message.loopType = 2; //固定元素列表
  120. }
  121. if (message.loopType == 1) {
  122. message["xpath"] = app._data.nowPath;
  123. } else { //固定元素列表
  124. //有的网站像淘宝,每个元素都有一个独一无二的ID号,这时候就不适用用id进行xpath定位了,这个问题暂时搁置
  125. message["pathList"] = [];
  126. for (let i = 0; i < nodeList.length; i++) {
  127. message["pathList"].push(readXPath(nodeList[i]["node"], 0));
  128. }
  129. }
  130. let msg = { "type": 3, msg: message };
  131. chrome.runtime.sendMessage(msg);
  132. }
  133. //检测是否xpath对应的元素被全选了,个数判断即可
  134. function detectAllSelected() {
  135. if (app._data.nowPath == "") {
  136. return false;
  137. } else {
  138. let num = 0;
  139. let result = document.evaluate(app._data.nowPath, document, null, XPathResult.ANY_TYPE, null);
  140. var node = result.iterateNext(); //枚举第一个元素
  141. while (node) {
  142. // console.log(node.innerHTML);
  143. num++;
  144. node = result.iterateNext();
  145. }
  146. if (num == nodeList.length) {
  147. return true;
  148. } else {
  149. return false;
  150. }
  151. }
  152. }