page.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. let nowFileName = "";
  2. let jsonInfo = {};
  3. let tabId = "";
  4. function InitTable() {
  5. window.TabInstance = $("#filterTable")
  6. .dxDataGrid({
  7. dataSource: [],
  8. keyExpr: "resource_detail_id",
  9. remoteOperations: false,
  10. searchPanel: {
  11. visible: true,
  12. highlightCaseSensitive: true,
  13. },
  14. groupPanel: { visible: true },
  15. grouping: {
  16. allowCollapsing: true,
  17. autoExpandAll: true,
  18. expandMode: "rowClick",
  19. },
  20. selection: {
  21. mode: "single",
  22. },
  23. allowColumnReordering: true,
  24. rowAlternationEnabled: true,
  25. showBorders: true,
  26. height: "100%",
  27. columns: [
  28. {
  29. dataField: "JS文件",
  30. dataType: "JS文件",
  31. groupIndex: 0,
  32. },
  33. {
  34. dataField: "过滤器",
  35. caption: "过滤器",
  36. },
  37. {
  38. dataField: "选择器类型",
  39. dataType: "选择器类型",
  40. },
  41. {
  42. dataField: "调用方式",
  43. dataType: "调用方式",
  44. },
  45. {
  46. dataField: "所在位置",
  47. dataType: "所在位置",
  48. },
  49. {
  50. dataField: "DOM操作分类",
  51. dataType: "DOM操作分类",
  52. },
  53. ],
  54. onContentReady(e) {},
  55. onSelectionChanged: function (selectedItems) {
  56. let data = selectedItems.selectedRowsData[0];
  57. if (data) {
  58. let ast = data.astJSON;
  59. if (data.JS文件 !== nowFileName) {
  60. nowFileName = data.JS文件;
  61. let code = window.fileList.find((o) => o.name === data.JS文件).code;
  62. //更改JS代码
  63. jsCode.SetCode(code);
  64. //更改ast
  65. //window.ShowASTTree(ast, document.getElementById('astJson'))
  66. }
  67. //jscode定位
  68. let {
  69. defineNode: {
  70. iid,
  71. loc: { end, start },
  72. },
  73. } = data.source_data;
  74. {
  75. //移出所有高亮
  76. jsCode.RemoveAllHighlight();
  77. //设置高亮
  78. jsCode.CreateHighlight({
  79. startLine: start?.line,
  80. startColumn: start?.column + 1,
  81. endLine: end?.line,
  82. endColumn: end?.column + 1,
  83. });
  84. }
  85. {
  86. // //ast定位
  87. // let jsonNode = document.getElementById(iid);
  88. // if (jsonNode) {
  89. // let textNode = Array.from(jsonNode.parentNode.childNodes).find(item => item.classList.contains("node-text"));
  90. // $("#astJson").find(".monaco-highlight").removeClass("monaco-highlight");
  91. // textNode?.classList?.add("monaco-highlight");
  92. // scrollTo(jsonNode)
  93. // }
  94. }
  95. }
  96. },
  97. })
  98. .dxDataGrid("instance");
  99. window.CSSTabInstance = $("#cssJson")
  100. .dxDataGrid({
  101. dataSource: [],
  102. keyExpr: "id",
  103. remoteOperations: false,
  104. searchPanel: {
  105. visible: true,
  106. highlightCaseSensitive: true,
  107. },
  108. selection: {
  109. mode: "single",
  110. },
  111. allowColumnReordering: true,
  112. rowAlternationEnabled: true,
  113. showBorders: true,
  114. height: "100%",
  115. paging: {
  116. enabled: false,
  117. pageSize: 0,
  118. },
  119. columns: [
  120. {
  121. dataField: "key",
  122. caption: "名称",
  123. },
  124. {
  125. dataField: "value",
  126. caption: "生效值",
  127. },
  128. ],
  129. onSelectionChanged: function (selectedItems) {
  130. let data = selectedItems.selectedRowsData[0];
  131. if (data) {
  132. CSSBelongInstance.option({
  133. dataSource: findPropRef(data.key, jsonInfo).map((s, i) => ({
  134. ...s,
  135. id: i,
  136. })),
  137. });
  138. }
  139. },
  140. })
  141. .dxDataGrid("instance");
  142. window.CSSBelongInstance = $("#cssBelong")
  143. .dxDataGrid({
  144. dataSource: [],
  145. keyExpr: "id",
  146. remoteOperations: false,
  147. searchPanel: {
  148. visible: true,
  149. highlightCaseSensitive: true,
  150. },
  151. selection: {
  152. mode: "single",
  153. },
  154. allowColumnReordering: true,
  155. rowAlternationEnabled: true,
  156. showBorders: true,
  157. paging: {
  158. enabled: false,
  159. pageSize: 0,
  160. },
  161. height: "100%",
  162. columns: [
  163. {
  164. dataField: "来源",
  165. caption: "来源",
  166. width: 100,
  167. },
  168. {
  169. dataField: "选择器",
  170. caption: "选择器",
  171. },
  172. {
  173. dataField: "属性值",
  174. caption: "属性值",
  175. },
  176. ],
  177. })
  178. .dxDataGrid("instance");
  179. }
  180. chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
  181. if (request.msgToPopup === "发送页面") {
  182. //初始化JS代码编辑器
  183. window.jsCode = new CodeEditor();
  184. jsCode.InitEditor($("#jscode")[0], false, false, [], false);
  185. jsCode.dataType = 3;
  186. InitTable();
  187. let ret = request.data;
  188. console.log(ret);
  189. let { jslist, targe, csslist, tabId: _tabId, path } = ret;
  190. tabId = _tabId;
  191. parseCodeToAST(
  192. jslist.map((item) => {
  193. return {
  194. code: item.data,
  195. name: item.fileName,
  196. };
  197. })
  198. );
  199. let data = window.findSelector();
  200. let id = $(targe).attr("id");
  201. let classList = $(targe).attr("class").split(" ");
  202. data = data.filter((m) => {
  203. let v = m.过滤器;
  204. if (v.includes(id)) return true;
  205. if (classList.length > 0) {
  206. for (const classitem of classList) {
  207. if (v.includes(classitem)) return true;
  208. }
  209. }
  210. });
  211. await attach();
  212. await getPathNodeInfo(path);
  213. TabInstance.option({
  214. dataSource: data,
  215. });
  216. CSSTabInstance.option({
  217. dataSource: Object.entries(csslist).map((a, index) => ({
  218. id: index,
  219. key: a[0],
  220. value: a[1],
  221. })),
  222. });
  223. }
  224. });
  225. function findPropRef(key, json) {
  226. let matchedCSSRules = json.matchedCSSRules;
  227. let retArr = [];
  228. for (const matchedCSSRule of matchedCSSRules) {
  229. let cssProperties = matchedCSSRule.rule.style.cssProperties;
  230. let cssProp = cssProperties.find((m) => m.name == key);
  231. if (!cssProp) continue;
  232. let ret = {
  233. 来源: "CSS",
  234. 选择器: matchedCSSRule.rule.selectorList.text,
  235. 选中选择器: matchedCSSRule.matchingSelectors[0],
  236. 属性值: cssProp.value,
  237. };
  238. retArr.push(ret);
  239. }
  240. return retArr;
  241. }
  242. async function getPathNodeInfo(path) {
  243. const doc = await getDocument();
  244. const htmlInfo = doc.children.find((m) => m.localName === "html");
  245. let info = htmlInfo;
  246. for (const index of path) {
  247. info = getpath(info, index);
  248. }
  249. let nodeId = info.nodeId;
  250. let cssInfo = await getMatchedStylesForNode(nodeId);
  251. jsonInfo = cssInfo;
  252. function getpath(info, index) {
  253. let children = info.children;
  254. return children[index];
  255. }
  256. }
  257. async function getMatchedStylesForNode(nodeId) {
  258. let ret = await chrome.debugger.sendCommand(
  259. { tabId },
  260. "CSS.getMatchedStylesForNode",
  261. {
  262. nodeId,
  263. }
  264. );
  265. return ret;
  266. }
  267. async function getDocument() {
  268. const doc = await chrome.debugger.sendCommand({ tabId }, "DOM.getDocument", {
  269. pierce: true,
  270. depth: -1,
  271. });
  272. return doc.root;
  273. }
  274. async function attach() {
  275. const targets = await chrome.debugger.getTargets();
  276. debugger;
  277. let target = targets.find((m) => m.tabId == tabId);
  278. if (target?.attached === true) {
  279. } else {
  280. await chrome.debugger.attach({ tabId }, "1.3");
  281. }
  282. await chrome.debugger.sendCommand({ tabId }, "DOM.enable");
  283. await chrome.debugger.sendCommand({ tabId }, "CSS.enable");
  284. }