content_20250812164221.ts 4.7 KB

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