global.js 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. import config from './config.json';
  2. export var global = {
  3. nodeList: [], //已被选中的节点列表
  4. readyList: [], //预备选中的list
  5. outputParameters: [], //输出参数列表
  6. outputParameterNodes: [], //输出参数节点列表
  7. NowNode: null,
  8. xnode: null,
  9. step: 0, //记录这是第几次点击操作
  10. style: "", //记录上个元素的颜色
  11. oe: null, //记录上个元素
  12. app: null,
  13. div: null,
  14. tdiv: null,
  15. selectedColor: "rgba(151,255,255, 0.6)",
  16. defaultbgColor: 'rgba(221,221,255,0.8)',
  17. boxShadowColor: "blue 0px 0px 5px",
  18. lang: config.language,
  19. id: "C" + Math.floor(Math.random() * (99999999)).toString(), //处理不同标签页的handles,生成的id
  20. ws: null,
  21. iframe: false,
  22. markElements: [],
  23. justSend: false,
  24. };
  25. export function isInIframe() {
  26. try {
  27. return window.self !== window.parent;
  28. } catch (e) {
  29. return true;
  30. }
  31. }
  32. export function getOS() {
  33. if (navigator.userAgent.indexOf('Window') > 0) {
  34. return 'Windows'
  35. } else if (navigator.userAgent.indexOf('Mac') > 0) {
  36. return 'Mac'
  37. } else if (navigator.userAgent.indexOf('Linux') > 0) {
  38. return 'Linux'
  39. } else {
  40. return 'NULL'
  41. }
  42. }
  43. export function getElementXPaths(element, parentElement = document.body) {
  44. let paths = [];
  45. let pre_xpath = "";
  46. // if(global.iframe){
  47. // pre_xpath = "//iframe";
  48. // }
  49. paths.push(readXPath(element, 1, parentElement));
  50. paths.push(pre_xpath + "//" + element.tagName.toLowerCase() + "[contains(., '" + element.textContent.slice(0, 10).trim() + "')]");
  51. if (element.id) {
  52. paths.push(pre_xpath + `id("${element.id}")`);
  53. }
  54. if (element.className) {
  55. paths.push(pre_xpath + "//" + element.tagName + "[@class='" + element.className + "']");
  56. }
  57. if (element.name) {
  58. paths.push(pre_xpath + "//" + element.tagName + "[@name='" + element.name + "']");
  59. }
  60. if (element.alt) {
  61. paths.push(pre_xpath + "//" + element.tagName + "[@alt='" + element.alt + "']");
  62. }
  63. paths.push(getAbsoluteXPathWithReverseIndex(element));
  64. console.log("ALL PATHS: " + paths);
  65. return paths;
  66. }
  67. function getAbsoluteXPathWithReverseIndex(element) {
  68. let pre_xpath = "";
  69. // if(global.iframe){
  70. // pre_xpath = "//iframe";
  71. // }
  72. let path = [];
  73. while (element && element.nodeType == Node.ELEMENT_NODE) {
  74. let index = 0;
  75. for (let sibling = element.nextSibling; sibling; sibling = sibling.nextSibling) {
  76. // Ignore document type declaration.
  77. if (sibling.nodeType == Node.DOCUMENT_TYPE_NODE)
  78. continue;
  79. if (sibling.nodeName == element.nodeName)
  80. ++index;
  81. }
  82. let tagName = element.nodeName.toLowerCase();
  83. let pathIndex = (index ? "[last()-" + index + "]" : "");
  84. path.unshift(tagName + pathIndex);
  85. element = element.parentNode;
  86. }
  87. return pre_xpath + "/" + path.join("/");
  88. }
  89. //返回element相对node节点的xpath,默认的node节点是: /
  90. export function readXPath(element, type = 1, node = document.body) {
  91. let pre_xpath = "";
  92. // if(global.iframe){
  93. // pre_xpath = "//iframe";
  94. // }
  95. try {
  96. if (type == 0) //type=0代表默认可通过id生成xpath type=1代表只能从根节点生成xpath, nodeList里必须使用绝对xpath!
  97. {
  98. if (element.id !== "") { //判断id属性,如果这个元素有id,则显示//*[@id="xPath"] 形式内容
  99. return pre_xpath + '//*[@id=\"' + element.id + '\"]';
  100. }
  101. if (element.className != "") { //如果有class且某个class name只有一个元素,则使用class name生成xpath
  102. console.log("class name: " + element.className);
  103. let names = element.className.split(" ");
  104. for (let i = 0; i < names.length; i++) {
  105. if (names[i] != "") {
  106. // return '//*[@class=\"' + names[i] + '\"]';
  107. // console.log('//*[@contains(@class, \"' + names[i] + '\")]');
  108. let elements_of_class = node.getElementsByClassName(names[i]);
  109. // console.log("Length of elements_of_class: " + elements_of_class.length);
  110. if (elements_of_class.length == 1) {
  111. return pre_xpath + '//*[contains(@class, \"' + names[i] + '\")]'
  112. }
  113. }
  114. }
  115. }
  116. }
  117. //这里需要需要主要字符串转译问题,可参考js 动态生成html时字符串和变量转译(注意引号的作用)
  118. if (element == node) { //递归到body处,结束递归
  119. if (node == document.body) {
  120. return pre_xpath + '/html/' + element.tagName.toLowerCase();
  121. } else {
  122. return "";
  123. }
  124. }
  125. let ix = 1, //在nodelist中的位置,且每次点击初始化
  126. siblings = element.parentNode.childNodes; //同级的子元素
  127. for (let i = 0, l = siblings.length; i < l; i++) {
  128. let sibling = siblings[i];
  129. //如果这个元素是siblings数组中的元素,则执行递归操作;arguments.callee代表当前函数的名称
  130. if (sibling == element) {
  131. return readXPath(element.parentNode, type, node) + '/' + element.tagName.toLowerCase() + '[' + (ix) + ']';
  132. //如果不符合,判断是否是element元素,并且是否是相同元素,如果是相同的就开始累加
  133. } else if (sibling.nodeType == 1 && sibling.tagName == element.tagName) {
  134. //注意此处,为了防止多计算了插入的操作台的3个div元素导致定位错误,这里需要屏蔽掉三个元素的索引号
  135. if (sibling.id != "wrapperDiv" && sibling.id != "wrapperTdiv" && sibling.id != "wrapperToolkit") {
  136. ix++;
  137. }
  138. }
  139. }
  140. } catch {
  141. return pre_xpath + "/"
  142. }
  143. }
  144. //选中元素到列表中
  145. export function addEl(e, node = global.NowNode, handle = true) {
  146. // if (tooltips) {
  147. // return;
  148. // }
  149. //如果元素不是鼠标选中的,记录该元素的style,不然按照鼠标选中的元素的style
  150. let style = node == global.NowNode ? global.style : node.style.backgroundColor;
  151. global.NowNode = node;
  152. let exist = false;
  153. for (let o of global.nodeList) {
  154. if (o["node"] == global.oe) {
  155. exist = true;
  156. break;
  157. }
  158. }
  159. //元素没有被添加过才去添加
  160. if (!exist) {
  161. global.step++;
  162. exist = false; //判断刚加入的元素是否在readyList中,如果在,则将所有readylist中的元素全部放入list中
  163. for (let o of global.readyList) {
  164. if (o["node"] == global.oe) {
  165. exist = true;
  166. break;
  167. }
  168. }
  169. if (exist) { //存在在readylist就全选中
  170. readyToList(global.step, false);
  171. if (global.app._data.selectedDescendents) {
  172. handleDescendents(); //如果之前有选中子元素,新加入的节点又则这里也需要重新选择子元素
  173. }
  174. } else //不然只添加一个元素
  175. {
  176. clearReady(); //readylist清零重新算
  177. global.nodeList.push({
  178. node: global.NowNode,
  179. "step": global.step,
  180. bgColor: style,
  181. "boxShadow": global.NowNode.style.boxShadow == "" || global.boxShadowColor ? "none" : global.NowNode.style.boxShadow,
  182. xpath: readXPath(global.NowNode, 1),
  183. "allXPaths": getElementXPaths(global.NowNode)
  184. });
  185. global.NowNode.style.backgroundColor = global.selectedColor;
  186. }
  187. if(handle){
  188. handleElement(); //处理新状态
  189. }
  190. //将虚线框显示在元素上方但屏蔽其鼠标操作
  191. let pos = global.NowNode.getBoundingClientRect();
  192. global.div.style.display = "block";
  193. global.div.style.height = global.NowNode.offsetHeight + "px";
  194. global.div.style.width = global.NowNode.offsetWidth + "px";
  195. global.div.style.left = pos.left + "px";
  196. global.div.style.top = pos.top + "px";
  197. global.div.style.zIndex = 2147483645;
  198. global.div.style.pointerEvents = "none";
  199. }
  200. // console.log("------");
  201. // for (i = 0; i < global.nodeList.length; i++) {
  202. // console.log(global.nodeList[i]["xpath"]);
  203. // }
  204. //对于可点击元素,屏蔽a标签默认点击事件
  205. event.stopImmediatePropagation();
  206. event.stopPropagation();
  207. event.preventDefault ? event.preventDefault() : event.returnValue = false;
  208. }
  209. //清除选择项
  210. export function clearEl(trail=false) {
  211. //如果最后停留的元素被选中,则调整此元素的style为原始style,否则不进行调整
  212. for (let node of global.nodeList) {
  213. node["node"].style.backgroundColor = node["bgColor"];
  214. node["node"].style.boxShadow = node["boxShadow"];
  215. if (global.NowNode == node["node"] && !trail) {
  216. // global.style = node["bgColor"];
  217. }
  218. }
  219. for (let node of global.markElements) {
  220. let element = node.element;
  221. element.style.boxShadow = "none";
  222. }
  223. global.markElements = [];
  224. global.step = 0;
  225. clearReady();
  226. clearParameters();
  227. global.nodeList.splice(0, global.nodeList.length); //清空数组
  228. global.app._data.option = 0; //选项重置
  229. global.app._data.page = 0; //恢复原始页面
  230. // global.app._data.nextPage = 0; //不出现翻页操作提示
  231. }
  232. //清除预备数组
  233. export function clearReady() {
  234. for (let node of global.readyList) //节点列表状态恢复原状
  235. {
  236. node["node"].style.boxShadow = node["boxShadow"];
  237. }
  238. global.readyList.splice(0, global.readyList.length); //清空数组
  239. }
  240. //每次对元素进行增删之后需要执行的操作
  241. export function handleElement() {
  242. clearReady(); //预备元素每次处理都先处理掉
  243. if (global.nodeList.length > 1) { //选中了许多元素的情况
  244. global.app._data.option = relatedTest();
  245. if (global.app._data.option == 100) {
  246. generateMultiParameters();
  247. } else {
  248. generateParameters(0);
  249. }
  250. } else if (global.nodeList.length == 1) {
  251. findRelated(); //寻找和元素相关的元素
  252. }
  253. }
  254. export function clearParameters(deal = true) //清空参数列表
  255. {
  256. if (deal) //是否取消对选中的子元素进行处理
  257. {
  258. global.app._data.selectedDescendents = false;
  259. global.app._data.selectStatus = false;
  260. }
  261. for (let o of global.outputParameterNodes) {
  262. o["node"].style.boxShadow = o["boxShadow"];
  263. }
  264. global.outputParameterNodes.splice(0);
  265. global.outputParameters.splice(0); //清空原来的参数列表
  266. global.app._data.valTable = []; //清空展现数组
  267. }
  268. export function LANG(zh, en) {
  269. if (global.lang == "zh") {
  270. return zh;
  271. } else {
  272. return en;
  273. }
  274. }
  275. function parameterName(value) {
  276. if (global.lang == 'zh') {
  277. return value;
  278. } else {
  279. switch (value) {
  280. case "文本":
  281. return "text";
  282. case "链接":
  283. return "link";
  284. case "_链接":
  285. return "_link";
  286. case "_文本":
  287. return "_text";
  288. case "链接文本":
  289. return "link_text";
  290. case "_链接文本":
  291. return "_link_text";
  292. case "链接地址":
  293. return "link_address";
  294. case "_链接地址":
  295. return "_link_address";
  296. case "按钮":
  297. return "button";
  298. case "输入文本框":
  299. return "input_text";
  300. case "单选框":
  301. return "radio";
  302. case "复选框":
  303. return "checkbox";
  304. case "下拉框":
  305. return "select";
  306. case "下拉框选项":
  307. return "select_option";
  308. case "地址":
  309. return "address";
  310. case "参数":
  311. return "para";
  312. case "_图片":
  313. return "_image";
  314. case "_图片地址":
  315. return "_image_address";
  316. case "背景图片地址":
  317. return "background_image_address";
  318. case "_背景图片":
  319. return "_background_image";
  320. case "页面网址":
  321. return "page_url";
  322. case "_页面网址":
  323. return "_page_url";
  324. case "页面标题":
  325. return "page_title";
  326. case "_页面标题":
  327. return "_page_title";
  328. case "选择的选项文本":
  329. return "selected_option_text";
  330. case "_选择的选项文本":
  331. return "_selected_option_text";
  332. case "选择的选项值":
  333. return "selected_option_value";
  334. case "_选择的选项值":
  335. return "_selected_option_value";
  336. default:
  337. return "";
  338. }
  339. }
  340. }
  341. //根据nodelist列表内的元素生成参数列表
  342. //适合:nodelist中的元素为同类型元素
  343. //type:0为全部文本 1为节点内直接的文字 2为innerhtml 3为outerhtml 4为backgroundImg 5为当前页面网址 6为当前页面标题 7为元素截图 8为OCR识别 9为JavaScript返回值 10为选择框选择的值 11为选择框选择的文本
  344. //nodetype:0,对应全type0123
  345. //nodetype:1 链接,对应type0123
  346. //nodetype:2 链接地址 对应type0
  347. //nodetype:3 按钮和输入文本框 对应type
  348. //nodetype:4 按钮和输入文本框 对应type
  349. export function generateParameters(type, linktext = true, linkhref = true) {
  350. clearParameters(false);
  351. let n = 1;
  352. chrome.storage.local.get('parameterNum', function (items) {
  353. // let at = parseInt(new Date().getTime());
  354. n = items.parameterNum;
  355. let ndPath = "";
  356. let ndAllXPaths = [];
  357. clearParameters(false);
  358. for (let num = 0; num < global.nodeList.length; num++) {
  359. let nd = global.nodeList[num]["node"];
  360. ndPath = global.nodeList[num]["xpath"];
  361. ndAllXPaths = global.nodeList[num]["allXPaths"];
  362. let unique_index = Math.random().toString(36).substring(2) + Date.now().toString(36);
  363. ; //唯一标识符
  364. global.outputParameterNodes.push({
  365. "node": nd,
  366. "unique_index": unique_index,
  367. "boxShadow": nd.style.boxShadow == "" || global.boxShadowColor ? "none" : nd.style.boxShadow
  368. });
  369. nd.style.boxShadow = global.boxShadowColor;
  370. let pname = parameterName("文本");
  371. let ndText = "";
  372. if (type == 0) {
  373. // ndText = $(nd).text();
  374. ndText = nd.textContent;
  375. pname = parameterName("文本");
  376. if (nd.tagName == "IMG") {
  377. ndText = nd.getAttribute("src") == null ? "" : nd.getAttribute("src");
  378. pname = parameterName("地址");
  379. } else if (nd.tagName == "INPUT") {
  380. ndText = nd.getAttribute("value") == null ? "" : nd.getAttribute("value");
  381. }
  382. } else if (type == 1) {
  383. // ndText = $(nd).contents().filter(function() { return this.nodeType === 3; }).text().replace(/\s+/g, '');
  384. ndText = "";
  385. let ndContents = nd.childNodes;
  386. for (let i = 0; i < ndContents.length; i++) {
  387. if (ndContents[i].nodeType === 3) { // if it's a text node
  388. ndText += ndContents[i].textContent.trim(); // add its content to the string
  389. }
  390. }
  391. ndText = ndText.replace(/\s+/g, ''); // remove any whitespace characters
  392. pname = parameterName("文本");
  393. if (nd.tagName == "IMG") {
  394. ndText = nd.getAttribute("src") == null ? "" : nd.getAttribute("src");
  395. pname = parameterName("地址");
  396. } else if (nd.tagName == "INPUT") {
  397. ndText = nd.getAttribute("value") == null ? "" : nd.getAttribute("value");
  398. }
  399. } else if (type == 2) {
  400. // ndText = $(nd).html();
  401. ndText = nd.innerHTML;
  402. pname = "Innerhtml";
  403. } else if (type == 3) {
  404. // ndText = $(nd).prop("outerHTML");
  405. ndText = nd.outerHTML;
  406. pname = "outerHTML";
  407. } else if (type == 4) {
  408. ndText = nd.style.backgroundImage.replace(/url\((['"])?(.*?)\1\)/gi, '$2').split(',')[0];
  409. pname = parameterName("背景图片地址");
  410. } else if (type == 5) {
  411. ndText = window.location.href;
  412. pname = parameterName("页面网址");
  413. } else if (type == 6) {
  414. ndText = document.title;
  415. pname = parameterName("页面标题");
  416. } else if (type == 10) {
  417. ndText = nd.value;
  418. pname = parameterName("选择的选项值");
  419. } else if (type == 11) {
  420. ndText = nd.options[nd.selectedIndex].text;
  421. pname = parameterName("选择的选项文本");
  422. }
  423. if (num == 0) { //第一个节点新建,后面的增加即可
  424. if (nd.tagName == "IMG") { //如果元素是图片
  425. global.outputParameters.push({
  426. "nodeType": 4, //节点类型
  427. "contentType": type, // 内容类型
  428. "relative": global.nodeList.length > 1 ? true : false, //是否为相对xpath路径
  429. "name": parameterName("参数") + (n++) + parameterName("_图片") + pname,
  430. "desc": "", //参数描述
  431. "extractType": 0, //提取方式 0 普通 1 OCR
  432. "relativeXPath": global.nodeList.length > 1 ? "" : ndPath,
  433. "allXPaths": global.nodeList.length > 1 ? "" : ndAllXPaths,
  434. "exampleValues": [{"num": num, "value": ndText}],
  435. "unique_index": unique_index,
  436. "iframe": global.iframe,
  437. });
  438. } else if (nd.tagName == "A") { //如果元素是超链接
  439. if (linktext) {
  440. global.outputParameters.push({
  441. "nodeType": 1,
  442. "contentType": type, // 内容类型
  443. "relative": global.nodeList.length > 1 ? true : false, //是否为相对xpath路径
  444. "name": parameterName("参数") + (n++) + parameterName("_链接") + pname,
  445. "desc": "", //参数描述
  446. "extractType": 0, //提取方式 0 普通 1 OCR
  447. "relativeXPath": global.nodeList.length > 1 ? "" : ndPath,
  448. "allXPaths": global.nodeList.length > 1 ? "" : ndAllXPaths,
  449. "exampleValues": [{"num": num, "value": ndText}],
  450. "unique_index": unique_index,
  451. "iframe": global.iframe,
  452. });
  453. }
  454. if (linkhref) {
  455. global.outputParameters.push({
  456. "nodeType": 2,
  457. "contentType": type, // 内容类型
  458. "relative": global.nodeList.length > 1 ? true : false, //是否为相对xpath路径
  459. "name": parameterName("参数") + (n++) + parameterName("_链接地址"),
  460. "desc": "", //参数描述
  461. "relativeXPath": global.nodeList.length > 1 ? "" : ndPath,
  462. "allXPaths": global.nodeList.length > 1 ? "" : ndAllXPaths,
  463. "exampleValues": [{
  464. "num": num,
  465. "value": nd.getAttribute("href") == null ? "" : nd.getAttribute("href")
  466. }],
  467. "unique_index": unique_index,
  468. "iframe": global.iframe,
  469. });
  470. }
  471. } else if (nd.tagName == "INPUT") { //如果元素是输入项
  472. global.outputParameters.push({
  473. "nodeType": 3,
  474. "contentType": type, // 内容类型
  475. "relative": global.nodeList.length > 1 ? true : false, //是否为相对xpath路径
  476. "name": parameterName("参数") + (n++) + "_" + pname,
  477. "desc": "", //参数描述
  478. "extractType": 0, //提取方式 0 普通 1 OCR
  479. "relativeXPath": global.nodeList.length > 1 ? "" : ndPath,
  480. "allXPaths": global.nodeList.length > 1 ? "" : ndAllXPaths,
  481. "exampleValues": [{"num": num, "value": ndText}],
  482. "unique_index": unique_index,
  483. "iframe": global.iframe,
  484. });
  485. } else { //其他所有情况
  486. global.outputParameters.push({
  487. "nodeType": 0,
  488. "contentType": type, // 内容类型
  489. "relative": global.nodeList.length > 1 ? true : false, //是否为相对xpath路径
  490. "name": parameterName("参数") + (n++) + "_" + pname,
  491. "desc": "", //参数描述
  492. "extractType": 0, //提取方式 0 普通 1 OCR
  493. "relativeXPath": global.nodeList.length > 1 ? "" : ndPath,
  494. "allXPaths": global.nodeList.length > 1 ? "" : ndAllXPaths,
  495. "exampleValues": [{"num": num, "value": ndText}],
  496. "unique_index": unique_index,
  497. "iframe": global.iframe,
  498. });
  499. }
  500. } else { //如果元素节点已经存在,则只需要插入值就可以了
  501. if (nd.tagName == "IMG") { //如果元素是图片
  502. global.outputParameters[0]["exampleValues"].push({"num": num, "value": ndText});
  503. } else if (nd.tagName == "A") { //如果元素是超链接
  504. global.outputParameters[0]["exampleValues"].push({"num": num, "value": ndText});
  505. global.outputParameters[1]["exampleValues"].push({
  506. "num": num,
  507. "value": nd.getAttribute("href") == null ? "" : nd.getAttribute("href")
  508. });
  509. } else if (nd.tagName == "INPUT") { //如果元素是输入项
  510. global.outputParameters[0]["exampleValues"].push({"num": num, "value": ndText});
  511. } else { //其他所有情况
  512. global.outputParameters[0]["exampleValues"].push({"num": num, "value": ndText});
  513. }
  514. }
  515. }
  516. // let at2 = parseInt(new Date().getTime());
  517. // console.log("generateParameters:", at2, at, at2 - at);
  518. generateValTable();
  519. console.log(global.outputParameters);
  520. });
  521. console.log("Generate Parameters");
  522. }
  523. //根据nodelist列表内的元素生成参数列表
  524. //适合:nodelist中的元素为不同类型元素
  525. export function generateMultiParameters() {
  526. clearParameters(false);
  527. let n = 1;
  528. chrome.storage.local.get({parameterNum: 1}, function (items) {
  529. // let at = parseInt(new Date().getTime());
  530. n = items.parameterNum;
  531. let nd, ndText, ndPath, pname, ndAllXPaths;
  532. for (let num = 0; num < global.nodeList.length; num++) {
  533. let nd = global.nodeList[num]["node"];
  534. ndPath = global.nodeList[num]["xpath"];
  535. ndAllXPaths = global.nodeList[num]["allXPaths"];
  536. let unique_index = Math.random().toString(36).substring(2) + Date.now().toString(36);
  537. ;
  538. global.outputParameterNodes.push({
  539. "node": nd,
  540. "unique_index": unique_index,
  541. "boxShadow": nd.style.boxShadow == "" || global.boxShadowColor ? "none" : nd.style.boxShadow
  542. });
  543. nd.style.boxShadow = global.boxShadowColor;
  544. // ndText = $(nd).text();
  545. ndText = nd.textContent;
  546. if (nd.tagName == "IMG") { //如果元素是图片
  547. global.outputParameters.push({
  548. "nodeType": 4, //节点类型
  549. "contentType": 0, // 内容类型
  550. "relative": false, //是否为相对xpath路径
  551. "name": parameterName("参数") + (n++) + parameterName("_图片地址"),
  552. "desc": "", //参数描述
  553. "relativeXPath": ndPath,
  554. "allXPaths": ndAllXPaths,
  555. "exampleValues": [{
  556. "num": 0,
  557. "value": nd.getAttribute("src") == null ? "" : nd.getAttribute("src")
  558. }],
  559. "unique_index": unique_index,
  560. "iframe": global.iframe,
  561. });
  562. } else if (nd.tagName == "A") { //如果元素是超链接
  563. global.outputParameters.push({
  564. "nodeType": 1,
  565. "contentType": 0, // 内容类型
  566. "relative": false, //是否为相对xpath路径
  567. "name": parameterName("参数") + (n++) + parameterName("_链接文本"),
  568. "desc": "", //参数描述
  569. "relativeXPath": ndPath,
  570. "allXPaths": ndAllXPaths,
  571. "exampleValues": [{"num": 0, "value": ndText}],
  572. "unique_index": unique_index,
  573. "iframe": global.iframe,
  574. });
  575. global.outputParameters.push({
  576. "nodeType": 2,
  577. "contentType": 0, // 内容类型
  578. "relative": false, //是否为相对xpath路径
  579. "name": parameterName("参数") + (n++) + parameterName("_链接地址"),
  580. "desc": "", //参数描述
  581. "relativeXPath": ndPath,
  582. "allXPaths": ndAllXPaths,
  583. "exampleValues": [{
  584. "num": 0,
  585. "value": nd.getAttribute("href") == null ? "" : nd.getAttribute("href")
  586. }],
  587. "unique_index": unique_index,
  588. "iframe": global.iframe,
  589. });
  590. } else if (nd.tagName == "INPUT") { //如果元素是输入项
  591. global.outputParameters.push({
  592. "nodeType": 3,
  593. "contentType": 0, // 内容类型
  594. "relative": false, //是否为相对xpath路径
  595. "name": parameterName("参数") + (n++) + parameterName("_文本"),
  596. "desc": "", //参数描述
  597. "relativeXPath": ndPath,
  598. "allXPaths": ndAllXPaths,
  599. "exampleValues": [{
  600. "num": 0,
  601. "value": nd.getAttribute("value") == null ? "" : nd.getAttribute("value")
  602. }],
  603. "unique_index": unique_index,
  604. "iframe": global.iframe,
  605. });
  606. } else { //其他所有情况
  607. global.outputParameters.push({
  608. "nodeType": 0,
  609. "contentType": 0, // 内容类型
  610. "relative": false, //是否为相对xpath路径
  611. "name": parameterName("参数") + (n++) + parameterName("_文本"),
  612. "desc": "", //参数描述
  613. "relativeXPath": ndPath,
  614. "allXPaths": ndAllXPaths,
  615. "exampleValues": [{"num": 0, "value": ndText}],
  616. "unique_index": unique_index,
  617. "iframe": global.iframe,
  618. });
  619. }
  620. }
  621. // console.log(global.outputParameters);
  622. // let at2 = parseInt(new Date().getTime());
  623. // console.log("generateMultiParameters", at2, at, at2 - at);
  624. generateValTable(false);
  625. });
  626. }
  627. //处理子元素,对于每个块中多出的特殊元素,需要特殊处理
  628. export function handleDescendents(mode = 0) {
  629. let n = 1;
  630. chrome.storage.local.get({parameterNum: 1}, function (items) {
  631. // let at = parseInt(new Date().getTime());
  632. n = items.parameterNum;
  633. clearParameters(); //清除原来的参数列表
  634. global.app._data.selectedDescendents = true;
  635. let nd, ndText, ndPath, pname, ndAllPaths, tmode;
  636. tmode = mode;
  637. if (mode == 2) {
  638. let xpath_list = [];
  639. // mode == 1; //如果是选中全部块的共有子元素,则先选中和第一个块相同的子元素,然后最后再删除第一个块中和其他块不同的子元素
  640. for (let num = 0; num < global.nodeList.length; num++) {
  641. let node_xpaths = [];
  642. let tnode = global.nodeList[num]["node"];
  643. let stack = new Array(); //深度优先搜索遍历元素
  644. stack.push(tnode); //从此节点开始
  645. while (stack.length > 0) {
  646. let nd = stack.pop(); // 挨个取出元素
  647. if (nd.parentNode.tagName == "A" && nd.tagName == "SPAN") {
  648. continue; //对A标签内的SPAN元素不进行处理,剪枝,此时子元素根本不加入stack,即实现了此功能
  649. }
  650. ndPath = readXPath(nd, 1, tnode);
  651. node_xpaths.push(ndPath);
  652. for (let i = nd.children.length - 1; i >= 0; i--) {
  653. stack.push(nd.children[i]);
  654. }
  655. }
  656. xpath_list.push(node_xpaths);
  657. }
  658. // 取第一个子数组作为初始的共有元素集合
  659. let commonXPaths = new Set(xpath_list[0]);
  660. // 遍历剩余的子数组
  661. for (let i = 1; i < xpath_list.length; i++) {
  662. // 使用过滤函数来筛选出与共有元素集合中的元素相同的元素
  663. commonXPaths = new Set(xpath_list[i].filter(element => commonXPaths.has(element)));
  664. }
  665. // 将共有元素集合转换为数组
  666. let commonXPathList = Array.from(commonXPaths);
  667. // console.log(commonXPathList);
  668. let hash = {}; //记录index和数组位置的对应关系
  669. for (let num = 0; num < global.nodeList.length; num++) {
  670. let tnode = global.nodeList[num]["node"];
  671. let stack = new Array(); //深度优先搜索遍历元素
  672. stack.push(tnode); //从此节点开始
  673. while (stack.length > 0) {
  674. let nd = stack.pop(); // 挨个取出元素
  675. if (nd.parentNode.tagName == "A" && nd.tagName == "SPAN") {
  676. continue; //对A标签内的SPAN元素不进行处理,剪枝,此时子元素根本不加入stack,即实现了此功能
  677. }
  678. ndPath = readXPath(nd, 1, tnode);
  679. ndAllPaths = getElementXPaths(nd, tnode);
  680. let index = -1;
  681. for (let i = 0; i < commonXPathList.length; i++) {
  682. if (commonXPathList[i] == ndPath) {
  683. index = i;
  684. break;
  685. }
  686. }
  687. if (index != -1) { //如果是共有元素
  688. let unique_index = ndPath;
  689. ndText = "";
  690. let ndContents = nd.childNodes;
  691. for (let i = 0; i < ndContents.length; i++) {
  692. if (ndContents[i].nodeType === 3) { // if it's a text node
  693. ndText += ndContents[i].textContent.trim(); // add its content to the string
  694. }
  695. }
  696. ndText = ndText.replace(/\s+/g, ''); // remove any whitespace characters
  697. if (ndText != "" || nd == tnode) {
  698. global.outputParameterNodes.push({
  699. "node": nd,
  700. "unique_index": unique_index,
  701. "boxShadow": nd.style.boxShadow == "" || global.boxShadowColor ? "none" : nd.style.boxShadow
  702. });
  703. nd.style.boxShadow = global.boxShadowColor;
  704. }
  705. // console.log("Hash", hash);
  706. if (num == 0) { //从第二个节点开始,只插入那些不在参数列表中的元素,根据xpath进行寻址
  707. //如果当前节点除了子元素外仍然有其他文字或者该元素是图片/表单项,加入子元素节点
  708. if (ndText != "" || nd.tagName == "IMG" || nd.tagName == "INPUT" || nd.tagName == "A") {
  709. hash[index] = global.outputParameters.length;
  710. if (nd.tagName == "IMG") { //如果元素是图片
  711. global.outputParameters.push({
  712. "nodeType": 4, //节点类型
  713. "contentType": 1, // 内容类型
  714. "relative": global.nodeList.length > 1 ? true : false, //是否为相对xpath路径,注意当只选择了子元素没有选中全部的时候,需要判断
  715. "name": parameterName("参数") + (n++) + parameterName("_图片地址"),
  716. "desc": "", //参数描述
  717. "relativeXPath": global.nodeList.length > 1 ? ndPath : readXPath(nd), //同理需要判断
  718. "allXPaths": global.nodeList.length > 1 ? ndAllPaths : getElementXPaths(nd),
  719. "exampleValues": [{
  720. "num": num,
  721. "value": nd.getAttribute("src") == null ? "" : nd.getAttribute("src")
  722. }],
  723. "unique_index": unique_index,
  724. "iframe": global.iframe,
  725. });
  726. } else if (nd.tagName == "A") { //如果元素是超链接
  727. global.outputParameters.push({
  728. "nodeType": 1,
  729. "contentType": 0, // 内容类型
  730. "relative": global.nodeList.length > 1 ? true : false, //是否为相对xpath路径
  731. "name": parameterName("参数") + (n++) + parameterName("_链接文本"),
  732. "desc": "", //参数描述
  733. "relativeXPath": global.nodeList.length > 1 ? ndPath : readXPath(nd),
  734. "allXPaths": global.nodeList.length > 1 ? ndAllPaths : getElementXPaths(nd),
  735. "exampleValues": [{"num": num, "value": nd.textContent}], //注意这里的ndtext是整个a的文字!!!
  736. "unique_index": unique_index,
  737. "iframe": global.iframe,
  738. });
  739. global.outputParameters.push({
  740. "nodeType": 2,
  741. "contentType": 0, // 内容类型
  742. "relative": global.nodeList.length > 1 ? true : false, //是否为相对xpath路径
  743. "name": parameterName("参数") + (n++) + parameterName("_链接地址"),
  744. "desc": "", //参数描述
  745. "relativeXPath": global.nodeList.length > 1 ? ndPath : readXPath(nd),
  746. "allXPaths": global.nodeList.length > 1 ? ndAllPaths : getElementXPaths(nd),
  747. "exampleValues": [{
  748. "num": num,
  749. "value": nd.getAttribute("href") == null ? "" : nd.getAttribute("href")
  750. }],
  751. "unique_index": unique_index,
  752. "iframe": global.iframe,
  753. });
  754. } else if (nd.tagName == "INPUT") { //如果元素是输入项
  755. global.outputParameters.push({
  756. "nodeType": 3,
  757. "contentType": 1, // 内容类型
  758. "relative": global.nodeList.length > 1 ? true : false, //是否为相对xpath路径
  759. "name": parameterName("参数") + (n++) + parameterName("_文本"),
  760. "desc": "", //参数描述
  761. "relativeXPath": global.nodeList.length > 1 ? ndPath : readXPath(nd),
  762. "allXPaths": global.nodeList.length > 1 ? ndAllPaths : getElementXPaths(nd),
  763. "exampleValues": [{
  764. "num": num,
  765. "value": nd.getAttribute("value") == null ? "" : nd.getAttribute("value")
  766. }],
  767. "unique_index": unique_index,
  768. "iframe": global.iframe,
  769. });
  770. } else { //其他所有情况
  771. global.outputParameters.push({
  772. "nodeType": 0,
  773. "contentType": 1, // 内容类型
  774. "relative": global.nodeList.length > 1 ? true : false, //是否为相对xpath路径
  775. "name": parameterName("参数") + (n++) + parameterName("_文本"),
  776. "desc": "", //参数描述
  777. "relativeXPath": global.nodeList.length > 1 ? ndPath : readXPath(nd),
  778. "allXPaths": global.nodeList.length > 1 ? ndAllPaths : getElementXPaths(nd),
  779. "exampleValues": [{"num": num, "value": ndText}],
  780. "unique_index": unique_index,
  781. "iframe": global.iframe,
  782. });
  783. }
  784. }
  785. } else //如果元素节点已经存在,则只需要插入值就可以了
  786. {
  787. try {
  788. if (ndText != "" || nd.tagName == "IMG" || nd.tagName == "INPUT" || nd.tagName == "A") {
  789. if (nd.tagName == "IMG") { //如果元素是图片
  790. global.outputParameters[hash[index]]["exampleValues"].push({
  791. "num": num,
  792. "value": nd.getAttribute("src") == null ? "" : nd.getAttribute("src")
  793. });
  794. } else if (nd.tagName == "A") { //如果元素是超链接
  795. global.outputParameters[hash[index]]["exampleValues"].push({
  796. "num": num,
  797. "value": nd.textContent
  798. });
  799. global.outputParameters[hash[index] + 1]["exampleValues"].push({
  800. "num": num,
  801. "value": nd.getAttribute("href") == null ? "" : nd.getAttribute("href")
  802. });
  803. } else if (nd.tagName == "INPUT") { //如果元素是输入项
  804. global.outputParameters[hash[index]]["exampleValues"].push({
  805. "num": num,
  806. "value": nd.getAttribute("value") == null ? "" : nd.getAttribute("value")
  807. });
  808. } else { //其他所有情况
  809. global.outputParameters[hash[index]]["exampleValues"].push({
  810. "num": num,
  811. "value": ndText
  812. });
  813. }
  814. }
  815. } catch (e) {
  816. console.log("Error: -----------------------\n\n", e);
  817. }
  818. }
  819. }
  820. for (let i = nd.children.length - 1; i >= 0; i--) {
  821. stack.push(nd.children[i]);
  822. }
  823. }
  824. }
  825. } else {
  826. for (let num = 0; num < global.nodeList.length; num++) {
  827. let tnode = global.nodeList[num]["node"];
  828. let stack = new Array(); //深度优先搜索遍历元素
  829. stack.push(tnode); //从此节点开始
  830. while (stack.length > 0) {
  831. let nd = stack.pop(); // 挨个取出元素
  832. if (nd.parentNode.tagName == "A" && nd.tagName == "SPAN") {
  833. continue; //对A标签内的SPAN元素不进行处理,剪枝,此时子元素根本不加入stack,即实现了此功能
  834. }
  835. ndPath = readXPath(nd, 1, tnode);
  836. ndAllPaths = getElementXPaths(nd, tnode);
  837. let index = -1;
  838. for (let i = 0; i < global.outputParameters.length; i++) {
  839. if (global.outputParameters[i]["relativeXPath"] == ndPath) {
  840. index = i;
  841. break;
  842. }
  843. }
  844. ndText = "";
  845. let ndContents = nd.childNodes;
  846. for (let i = 0; i < ndContents.length; i++) {
  847. if (ndContents[i].nodeType === 3) { // if it's a text node
  848. ndText += ndContents[i].textContent.trim(); // add its content to the string
  849. }
  850. }
  851. ndText = ndText.replace(/\s+/g, ''); // remove any whitespace characters
  852. let unique_index = ndPath;
  853. if (ndText != "" || nd == tnode) {
  854. if (mode == 0 || (mode == 1 && (index != -1 || num == 0))) { //如果不是应选尽选,则只添加和第一个元素相同类型的子元素
  855. global.outputParameterNodes.push({
  856. "node": nd,
  857. "unique_index": unique_index,
  858. "boxShadow": nd.style.boxShadow == "" || global.boxShadowColor ? "none" : nd.style.boxShadow
  859. });
  860. nd.style.boxShadow = global.boxShadowColor;
  861. } else if (mode == 1 && nd == tnode) { //最外层元素标记一下
  862. nd.style.boxShadow = global.boxShadowColor;
  863. }
  864. }
  865. if (index == -1) { //从第二个节点开始,只插入那些不在参数列表中的元素,根据xpath进行寻址
  866. //如果当前节点除了子元素外仍然有其他文字或者该元素是图片/表单项,加入子元素节点
  867. if (!(mode == 1 && num > 0)) { //如果不是应选尽选,则只添加和第一个元素相同类型的子元素
  868. if (ndText != "" || nd.tagName == "IMG" || nd.tagName == "INPUT" || nd.tagName == "A") {
  869. if (nd.tagName == "IMG") { //如果元素是图片
  870. global.outputParameters.push({
  871. "nodeType": 4, //节点类型
  872. "contentType": 1, // 内容类型
  873. "relative": global.nodeList.length > 1 ? true : false, //是否为相对xpath路径,注意当只选择了子元素没有选中全部的时候,需要判断
  874. "name": parameterName("参数") + (n++) + parameterName("_图片地址"),
  875. "desc": "", //参数描述
  876. "relativeXPath": global.nodeList.length > 1 ? ndPath : readXPath(nd), //同理需要判断
  877. "allXPaths": global.nodeList.length > 1 ? ndAllPaths : getElementXPaths(nd),
  878. "exampleValues": [{
  879. "num": num,
  880. "value": nd.getAttribute("src") == null ? "" : nd.getAttribute("src")
  881. }],
  882. "unique_index": unique_index,
  883. "iframe": global.iframe,
  884. });
  885. } else if (nd.tagName == "A") { //如果元素是超链接
  886. global.outputParameters.push({
  887. "nodeType": 1,
  888. "contentType": 0, // 内容类型
  889. "relative": global.nodeList.length > 1 ? true : false, //是否为相对xpath路径
  890. "name": parameterName("参数") + (n++) + parameterName("_链接文本"),
  891. "desc": "", //参数描述
  892. "relativeXPath": global.nodeList.length > 1 ? ndPath : readXPath(nd),
  893. "allXPaths": global.nodeList.length > 1 ? ndAllPaths : getElementXPaths(nd),
  894. "exampleValues": [{"num": num, "value": nd.textContent}], //注意这里的ndtext是整个a的文字!!!
  895. "unique_index": unique_index,
  896. "iframe": global.iframe,
  897. });
  898. global.outputParameters.push({
  899. "nodeType": 2,
  900. "contentType": 0, // 内容类型
  901. "relative": global.nodeList.length > 1 ? true : false, //是否为相对xpath路径
  902. "name": parameterName("参数") + (n++) + parameterName("_链接地址"),
  903. "desc": "", //参数描述
  904. "relativeXPath": global.nodeList.length > 1 ? ndPath : readXPath(nd),
  905. "allXPaths": global.nodeList.length > 1 ? ndAllPaths : getElementXPaths(nd),
  906. "exampleValues": [{
  907. "num": num,
  908. "value": nd.getAttribute("href") == null ? "" : nd.getAttribute("href")
  909. }],
  910. "unique_index": unique_index,
  911. "iframe": global.iframe,
  912. });
  913. } else if (nd.tagName == "INPUT") { //如果元素是输入项
  914. global.outputParameters.push({
  915. "nodeType": 3,
  916. "contentType": 1, // 内容类型
  917. "relative": global.nodeList.length > 1 ? true : false, //是否为相对xpath路径
  918. "name": parameterName("参数") + (n++) + parameterName("_文本"),
  919. "desc": "", //参数描述
  920. "relativeXPath": global.nodeList.length > 1 ? ndPath : readXPath(nd),
  921. "allXPaths": global.nodeList.length > 1 ? ndAllPaths : getElementXPaths(nd),
  922. "exampleValues": [{
  923. "num": num,
  924. "value": nd.getAttribute("value") == null ? "" : nd.getAttribute("value")
  925. }],
  926. "unique_index": unique_index,
  927. "iframe": global.iframe,
  928. });
  929. } else { //其他所有情况
  930. global.outputParameters.push({
  931. "nodeType": 0,
  932. "contentType": 1, // 内容类型
  933. "relative": global.nodeList.length > 1 ? true : false, //是否为相对xpath路径
  934. "name": parameterName("参数") + (n++) + parameterName("_文本"),
  935. "desc": "", //参数描述
  936. "relativeXPath": global.nodeList.length > 1 ? ndPath : readXPath(nd),
  937. "allXPaths": global.nodeList.length > 1 ? ndAllPaths : getElementXPaths(nd),
  938. "exampleValues": [{"num": num, "value": ndText}],
  939. "unique_index": unique_index,
  940. "iframe": global.iframe,
  941. });
  942. }
  943. }
  944. }
  945. } else //如果元素节点已经存在,则只需要插入值就可以了
  946. {
  947. if (nd.tagName == "IMG") { //如果元素是图片
  948. global.outputParameters[index]["exampleValues"].push({
  949. "num": num,
  950. "value": nd.getAttribute("src") == null ? "" : nd.getAttribute("src")
  951. });
  952. } else if (nd.tagName == "A") { //如果元素是超链接
  953. global.outputParameters[index]["exampleValues"].push({"num": num, "value": nd.textContent});
  954. global.outputParameters[index + 1]["exampleValues"].push({
  955. "num": num,
  956. "value": nd.getAttribute("href") == null ? "" : nd.getAttribute("href")
  957. });
  958. } else if (nd.tagName == "INPUT") { //如果元素是输入项
  959. global.outputParameters[index]["exampleValues"].push({
  960. "num": num,
  961. "value": nd.getAttribute("value") == null ? "" : nd.getAttribute("value")
  962. });
  963. } else { //其他所有情况
  964. global.outputParameters[index]["exampleValues"].push({"num": num, "value": ndText});
  965. }
  966. }
  967. for (let i = nd.children.length - 1; i >= 0; i--) {
  968. stack.push(nd.children[i]);
  969. }
  970. }
  971. }
  972. }
  973. // let at2 = parseInt(new Date().getTime());
  974. // console.log("选中子元素", at2, at, at2 - at);
  975. generateValTable();
  976. });
  977. }
  978. //根据参数列表生成可视化参数界面
  979. export function generateValTable(multiline = true) {
  980. let paraValues = [];
  981. for (let i = 0; i < global.outputParameters.length; i++) {
  982. let tValues = [];
  983. let tindex = 0;
  984. let l = multiline ? global.nodeList.length : 1;
  985. for (let j = 0; j < l; j++) {
  986. //注意第一个循环条件,index超出界限了就不需要再寻找了,其他的全是空
  987. if (tindex < global.outputParameters[i]["exampleValues"].length && global.outputParameters[i]["exampleValues"][tindex]["num"] == j) {
  988. tValues.push(global.outputParameters[i]["exampleValues"][tindex]["value"]);
  989. tindex++;
  990. } else {
  991. tValues.push(" ");
  992. }
  993. }
  994. paraValues.push(tValues);
  995. }
  996. global.app._data.valTable = paraValues;
  997. // console.log("生成参数表格", paraValues);
  998. }
  999. // 选中第一个节点,自动寻找同类节点
  1000. // 方法:/div[1]/div[2]/div[2]/a[1]
  1001. // 从倒数第一个节点开始找,看去掉方括号之后是否元素数目变多,如上面的变成/div[1]/div[2]/div[2]/a
  1002. // 如果没有,则恢复原状,然后试试倒数第二个:/div[1]/div[2]/div/a[1]
  1003. // 直到找到第一个变多的节点或者追溯到根节点为止
  1004. export function findRelated() {
  1005. // let at = parseInt(new Date().getTime());
  1006. let testPath = global.nodeList[0]["xpath"].replace("//iframe", "").split("/").splice(1); //分离xpath成 ["html","body","div[0]"]这样子
  1007. let nodeNameList = [];
  1008. let nodeIndexList = [];
  1009. for (let i = 0; i < testPath.length; i++) {
  1010. nodeNameList.push(testPath[i].split("[")[0]);
  1011. if (testPath[i].indexOf("[") >= 0) { //如果存在索引值
  1012. nodeIndexList.push(parseInt(testPath[i].split("[")[1].replace("]", ""))); //只留下数字
  1013. } else {
  1014. nodeIndexList.push(-1);
  1015. }
  1016. }
  1017. let tempPath = "";
  1018. for (let i = nodeIndexList.length - 1; i >= 0; i--) {
  1019. if (nodeIndexList[i] == -1) { //没有索引值直接跳过
  1020. continue;
  1021. }
  1022. let tempIndexList = [...nodeIndexList]; //复刻一个index数组
  1023. tempIndexList[i] = -1; //删除索引值
  1024. tempPath = combineXpath(nodeNameList, tempIndexList); //生成新的xpath
  1025. let result = document.evaluate(tempPath, document, null, XPathResult.ANY_TYPE, null);
  1026. result.iterateNext(); //枚举第一个元素
  1027. if (result.iterateNext() != null) { //如果能枚举到第二个元素,说明存在同类元素,选中同类元素,结束循环
  1028. global.app.$data.nowPath = tempPath; //标记此元素xpath
  1029. let element = document.evaluate(tempPath, document, null, XPathResult.ANY_TYPE, null).iterateNext();
  1030. console.log("tempPath:", tempPath, "element:", element);
  1031. global.app.$data.nowAllPaths = getElementXPaths(element); //标记此元素xpath
  1032. pushToReadyList(tempPath);
  1033. break;
  1034. }
  1035. }
  1036. // let at2 = parseInt(new Date().getTime());
  1037. // console.log("findRelated:", at2, at, at2 - at);
  1038. }
  1039. //根据path将元素放入readylist中
  1040. export function pushToReadyList(path) {
  1041. let result = document.evaluate(path, document, null, XPathResult.ANY_TYPE, null);
  1042. let node = result.iterateNext(); //枚举第一个元素
  1043. while (node) { //只添加不在已选中列表内的元素
  1044. let exist = false;
  1045. for (let o of global.nodeList) {
  1046. if (o["node"] == node) {
  1047. exist = true;
  1048. break;
  1049. }
  1050. }
  1051. if (!exist) {
  1052. global.readyList.push({
  1053. "node": node,
  1054. "bgColor": node.style.backgroundColor,
  1055. "boxShadow": node.style.boxShadow == "" || global.boxShadowColor ? "none" : node.style.boxShadow
  1056. });
  1057. }
  1058. node.style.boxShadow = global.boxShadowColor;
  1059. node = result.iterateNext(); //枚举下一个元素
  1060. }
  1061. }
  1062. export function selectAllElements(context=null) {
  1063. global.step++;
  1064. readyToList(global.step, false);
  1065. handleElement();
  1066. if (context.selectedDescendents) {
  1067. handleDescendents(); //如果之前有选中子元素,新加入的节点又则这里也需要重新选择子元素
  1068. }
  1069. }
  1070. //将readyList中的元素放入选中节点中
  1071. export function readyToList(step, dealparameters = true) {
  1072. for (let o of global.readyList) {
  1073. global.nodeList.push({
  1074. node: o["node"],
  1075. "step": global.step,
  1076. bgColor: o["bgColor"],
  1077. "boxShadow": o["boxShadow"],
  1078. xpath: readXPath(o["node"], 1),
  1079. "allXPaths": getElementXPaths(o["node"])
  1080. });
  1081. o["node"].style.backgroundColor = global.selectedColor;
  1082. }
  1083. clearReady();
  1084. if (dealparameters) { //防止出现先选中子元素再选中全部失效的问题
  1085. generateParameters(0); //根据nodelist列表内的元素生成参数列表,0代表纯文本
  1086. }
  1087. }
  1088. //根据节点列表和索引列表生成XPATH
  1089. // 如:["html","body","div"],[-1,-1,2],生成/html/body/div[2]
  1090. export function combineXpath(nameList, indexList) {
  1091. let finalPath = "";
  1092. for (let i = 0; i < nameList.length; i++) {
  1093. finalPath = finalPath + "/" + nameList[i];
  1094. if (indexList[i] != -1) {
  1095. finalPath = finalPath + "[" + indexList[i] + "]";
  1096. }
  1097. }
  1098. return finalPath;
  1099. }
  1100. //专门测试已经选中的这些元素之间有没有相关性
  1101. // 举例:
  1102. // /html/body/div[3]/div[1]/div[1]/div[1]/div[3]/div[1]/div[3]/a[22]
  1103. // /html/body/div[3]/div[1]/div[1]/div[1]/div[3]/div[2]/div[3]/a[25]
  1104. // 最终转换为:
  1105. // /html/body/div[3]/div[1]/div[1]/div[1]/div[3]/div/div[3]/a
  1106. export function relatedTest() {
  1107. // let at = new Date().getTime()
  1108. let testList = [];
  1109. let testpath = "";
  1110. for (let i = 0; i < global.nodeList.length; i++) {
  1111. let testnumList = []; //用于比较节点索引号不同
  1112. let tpath = global.nodeList[i]["xpath"].replace("//iframe", "").split("/").splice(1); //清理第一个空元素
  1113. for (let j = 0; j < tpath.length; j++) {
  1114. if (tpath[j].indexOf("[") >= 0) { //如果存在索引值
  1115. testnumList.push(parseInt(tpath[j].split("[")[1].replace("]", ""))); //只留下数字
  1116. } else {
  1117. testnumList.push(-1);
  1118. }
  1119. tpath[j] = tpath[j].split("[")[0];
  1120. }
  1121. let tp = tpath.join("/");
  1122. if (i > 0 && testpath != tp) { //如果去除括号后元素内存在不一致情况,直接返回默认情况代码100
  1123. global.app.$data.nowPath = ""; //标记此元素xpath
  1124. return 100;
  1125. }
  1126. testpath = tp;
  1127. testList.push(testnumList);
  1128. }
  1129. testpath = testpath.split("/"); //清理第一个空元素
  1130. let indexList = []; //记录新生成的xpath
  1131. //如果选中的元素属于同样的序列,则计算出序列的最佳xpath表达式
  1132. for (let j = 0; j < testList[0].length; j++) {
  1133. indexList.push(testList[0][j]);
  1134. for (let i = 1; i < testList.length; i++) {
  1135. if (testList[i][j] != testList[i - 1][j]) {
  1136. indexList[j] = -1; //不一致就记录成-1
  1137. break;
  1138. }
  1139. }
  1140. }
  1141. let finalPath = combineXpath(testpath, indexList);
  1142. let element = document.evaluate(finalPath, document, null, XPathResult.ANY_TYPE, null).iterateNext();
  1143. global.app.$data.nowAllPaths = getElementXPaths(element); //标记此元素xpath
  1144. console.log("finalPath:", finalPath, "element:", element);
  1145. global.app.$data.nowPath = finalPath; //标记此元素xpath
  1146. pushToReadyList(finalPath);
  1147. // let at2 = parseInt(new Date().getTime());
  1148. // console.log("手动:", at2, at, at2 - at);
  1149. return 50; //先返回给默认码
  1150. }