content_20250812164305.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. export default defineContentScript({
  2. matches: ["<all_urls>"],
  3. main() {
  4. // 辅助函数:提取背景图片URL
  5. const extractBackgroundUrl = (element: Element): string | null => {
  6. const computedStyle = window.getComputedStyle(element);
  7. const backgroundImage = computedStyle.backgroundImage;
  8. if (backgroundImage && backgroundImage !== 'none' && backgroundImage.includes('url')) {
  9. // 处理包含转义字符的URL
  10. const urlMatch = backgroundImage.match(/url\(["']?(.*?)["']?\)/);
  11. if (urlMatch && urlMatch[1]) {
  12. // 解码HTML实体
  13. let url = urlMatch[1];
  14. const textarea = document.createElement('textarea');
  15. textarea.innerHTML = url;
  16. return textarea.value;
  17. }
  18. }
  19. return null;
  20. };
  21. // 监听来自popup的消息
  22. browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
  23. console.log("rrrrrr")
  24. if (request.action === "getTbodyData") {
  25. console.log(document.querySelectorAll('table'),"dddddddds")
  26. // 获取所有tbody中的数据,包括背景图片
  27. const tables = Array.from(document.querySelectorAll('table')).map((table, tableIndex) => {
  28. const tbodies = Array.from(table.querySelectorAll('tbody'));
  29. return tbodies.map(tbody => {
  30. const rows = Array.from(tbody.querySelectorAll('tr'));
  31. return rows.map((row, rowIndex) => {
  32. const cells = Array.from(row.querySelectorAll('td, th'));
  33. return cells.map((cell, cellIndex) => {
  34. // 获取单元格文本内容
  35. const textContent = cell.textContent?.trim() || '';
  36. // 查找单元格内所有带有背景图片的div元素
  37. const divsWithBackground = Array.from(cell.querySelectorAll('div[style*="background-image"]'))
  38. .map((div, divIndex) => {
  39. const backgroundUrl = extractBackgroundUrl(div);
  40. if (backgroundUrl) {
  41. return {
  42. url: backgroundUrl,
  43. className: div.className || '',
  44. style: div.getAttribute('style') || ''
  45. };
  46. }
  47. return null;
  48. })
  49. .filter(item => item !== null);
  50. // 如果找到带背景图片的div,返回包含这些信息的对象
  51. if (divsWithBackground.length > 0) {
  52. return {
  53. text: textContent,
  54. backgroundImages: divsWithBackground
  55. };
  56. }
  57. // 检查单元格本身是否有背景图片
  58. const cellBackgroundUrl = extractBackgroundUrl(cell);
  59. if (cellBackgroundUrl) {
  60. return {
  61. text: textContent,
  62. cellBackgroundUrl: cellBackgroundUrl
  63. };
  64. }
  65. // 否则返回文本内容
  66. return textContent;
  67. });
  68. });
  69. }).flat();
  70. }).flat();
  71. // 过滤掉空表格
  72. const nonEmptyTables = tables.filter(table => table.length > 0);
  73. sendResponse({ tables: nonEmptyTables });
  74. }
  75. if (request.action === "getBackgroundImages") {
  76. // 获取所有带有background-image的元素
  77. const allElements = Array.from(document.querySelectorAll('*'));
  78. const backgroundImages: Array<{url: string, element: string, class: string, id: string}> = [];
  79. allElements.forEach(element => {
  80. const computedStyle = window.getComputedStyle(element);
  81. const backgroundImage = computedStyle.backgroundImage;
  82. // 检查是否有background-image
  83. if (backgroundImage && backgroundImage !== 'none' && backgroundImage.includes('url')) {
  84. // 提取URL
  85. const urlMatch = backgroundImage.match(/url\(["']?(.*?)["']?\)/);
  86. if (urlMatch && urlMatch[1]) {
  87. // 解码HTML实体
  88. const textarea = document.createElement('textarea');
  89. textarea.innerHTML = urlMatch[1];
  90. const decodedUrl = textarea.value;
  91. backgroundImages.push({
  92. url: decodedUrl,
  93. element: element.tagName.toLowerCase(),
  94. class: element.className || '',
  95. id: element.id || ''
  96. });
  97. }
  98. }
  99. });
  100. sendResponse({ backgroundImages });
  101. }
  102. });
  103. }
  104. });