content.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. let overDiv;
  2. chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  3. if (request.action === "highlight") {
  4. if (request.message && !isOpen) {
  5. overDiv = document.createElement("div");
  6. overDiv.classList.add("bg-overDiv");
  7. // CSS样式内容
  8. var css = `
  9. .bg-overDiv{
  10. position: fixed;
  11. opacity: 0.5;
  12. background: rgb(20 122 218 / 85%);
  13. backdrop-filter: blur(5px);
  14. cursor: pointer;
  15. z-index: 99999999999;
  16. display: none;
  17. }`;
  18. // 创建一个style元素
  19. var style = document.createElement("style");
  20. style.classList.add("bg-style");
  21. // 为style元素添加CSS内容
  22. if (style.styleSheet) {
  23. style.styleSheet.cssText = css;
  24. } else {
  25. style.appendChild(document.createTextNode(css));
  26. }
  27. // 将style元素添加到body的末尾
  28. document.body.appendChild(style); //把这个样式加入body的最后
  29. document.body.appendChild(overDiv);
  30. isOpen = true;
  31. window.addEventListener("click", clickHandler);
  32. window.addEventListener("mousemove", mouseMoveHandler);
  33. }
  34. if (!request.message && isOpen) {
  35. document.querySelector(".bg-overDiv")?.remove();
  36. document.querySelector(".bg-style")?.remove();
  37. window.removeEventListener("click", clickHandler);
  38. window.removeEventListener("mousemove", mouseMoveHandler);
  39. isOpen = false;
  40. }
  41. }
  42. });
  43. const jsRequests = []; // 使用 Set 以避免重复的 URL
  44. // 观察 DOM 的变化
  45. const observer = new MutationObserver((mutations) => {
  46. for (const mutation of mutations) {
  47. if (mutation.type === "childList") {
  48. mutation.addedNodes.forEach((node) => {
  49. if (node.nodeName === "SCRIPT" && node.src) {
  50. const url = new URL(node.src);
  51. if (url.pathname.startsWith("/lib")) return;
  52. fetch(node.src)
  53. .then((res) => res.text())
  54. .then((data) => {
  55. jsRequests.push({
  56. fileName: getFileName(node.src),
  57. url: node.src,
  58. data,
  59. });
  60. });
  61. //jsRequests.add(node.src);
  62. }
  63. });
  64. }
  65. }
  66. });
  67. function getFileName(url) {
  68. // 创建一个URL对象
  69. const urlObj = new URL(url);
  70. // 获取路径部分
  71. const pathname = urlObj.pathname;
  72. // 提取文件名
  73. const fileName = pathname.substring(pathname.lastIndexOf("/") + 1);
  74. return fileName;
  75. }
  76. // 观察整个文档
  77. observer.observe(document, { childList: true, subtree: true });
  78. // 获取当前加载的脚本
  79. const scripts = Array.from(document.getElementsByTagName("script"));
  80. scripts.forEach((script) => {
  81. if (script.src) {
  82. const url = new URL(script.src);
  83. if (url.pathname.startsWith("/lib")) return;
  84. fetch(script.src)
  85. .then((res) => res.text())
  86. .then((data) => {
  87. jsRequests.push({
  88. fileName: getFileName(script.src),
  89. url: script.src,
  90. data,
  91. });
  92. });
  93. // jsRequests.add(script.src);
  94. }
  95. });
  96. let isOpen = false;
  97. function clickHandler(e) {
  98. // console.log(jsRequests);
  99. // console.log(oldTarget);
  100. chrome.runtime.sendMessage({
  101. action: "show_popup",
  102. jslist: jsRequests,
  103. targe: oldTarget.outerHTML,
  104. csslist: Array.from(document.styleSheets).map(({ href, cssRules, ownerNode }, index) => {
  105. return {
  106. href: href === null ? "自定义样式" + index : href,
  107. selectorTexts: Array.from(cssRules).map(({ selectorText }) => selectorText),
  108. styleCode: ownerNode instanceof HTMLStyleElement ? ownerNode.innerHTML : "" // 自定义的样式,需要提前存储
  109. }
  110. }),
  111. path: getNodePosition(oldTarget),
  112. });
  113. }
  114. function getNodePosition(element) {
  115. let id = [];
  116. if (element === document) {
  117. return [];
  118. } else {
  119. let parentNode = element.parentNode;
  120. let index = 0;
  121. for (let i = 0; parentNode.childNodes.length; i++) {
  122. let child = parentNode.childNodes[i];
  123. if (child.nodeName === "#text") continue;
  124. if (child === element) {
  125. id = [index];
  126. break;
  127. }
  128. index++;
  129. }
  130. return getNodePosition(element.parentNode).concat(id);
  131. }
  132. }
  133. // window.addEventListener("click", function (e) {
  134. // // e.preventDefault();
  135. // // e.stopPropagation();
  136. // console.log("点击");
  137. // });
  138. let oldTarget;
  139. function mouseMoveHandler(e) {
  140. if (!isOpen) return;
  141. let target =
  142. findTarget(document.elementsFromPoint(e.clientX, e.clientY)) || e.target;
  143. if (oldTarget !== target) {
  144. overDiv.style.cssText = "";
  145. oldTarget = target;
  146. let rect = target.getClientRects();
  147. if (rect.length > 0) {
  148. rect = rect[0];
  149. overDiv.style.cssText =
  150. "display: block; top:" +
  151. rect.top +
  152. "px;left: " +
  153. rect.left +
  154. "px; width: " +
  155. rect.width +
  156. "px; height: " +
  157. rect.height +
  158. "px;";
  159. }
  160. }
  161. oldTarget = target;
  162. let oldEle = document.querySelector(".bg-overlay");
  163. if (oldEle) {
  164. oldEle.classList.remove("bg-overlay");
  165. }
  166. target.classList.add("bg-overlay");
  167. }
  168. function findTarget(eles) {
  169. for (let i = 0; i < eles.length; i++) {
  170. if (eles[i].classList.contains("bg-overDiv") === false) return eles[i];
  171. }
  172. }
  173. function findChildren(ele, x, y) {
  174. let clds = ele.children;
  175. let findEle = ele;
  176. if (clds.length > 0) {
  177. let len = clds.length;
  178. let cur = null;
  179. for (let i = 0; i < len; i++) {
  180. let rect = clds[i].getClientRects();
  181. if (!rect[0]) continue;
  182. if (
  183. rect[0].x + rect[0].width >= x &&
  184. rect[0].x <= x &&
  185. rect[0].y + rect[0].height >= y &&
  186. rect[0].y <= y
  187. ) {
  188. findEle = findChildren(clds[i], x, y);
  189. break;
  190. }
  191. }
  192. }
  193. return findEle;
  194. }