content.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { OptionScript } from "./content_script/OptionScript";
  2. export default defineContentScript({
  3. matches: ["<all_urls>"],
  4. main() {
  5. // 辅助函数:提取背景图片URL
  6. const extractBackgroundUrl = (element: Element): string | null => {
  7. const computedStyle = window.getComputedStyle(element);
  8. const backgroundImage = computedStyle.backgroundImage;
  9. if (backgroundImage && backgroundImage !== 'none' && backgroundImage.includes('url')) {
  10. // 处理包含转义字符的URL
  11. const urlMatch = backgroundImage.match(/url\(["']?(.*?)["']?\)/);
  12. if (urlMatch && urlMatch[1]) {
  13. // 解码HTML实体
  14. let url = urlMatch[1];
  15. const textarea = document.createElement('textarea');
  16. textarea.innerHTML = url;
  17. return textarea.value;
  18. }
  19. }
  20. return null;
  21. };
  22. // 监听来自popup的消息
  23. browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
  24. if (request.action === "getTbodyData") {
  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. let textContent = cell.textContent?.trim() || '';
  35. // 将文本中带有WB和件的判定为单号进行特殊处理,单号位数不确定
  36. if (textContent.includes('WB') && textContent.includes('件')) {
  37. // 去除带"件"的span标签
  38. const spans = cell.getElementsByTagName('span');
  39. let spanValues: string[] = [];
  40. // 遍历所有 span 元素并提取文本值
  41. for (let i = 0; i < spans.length; i++) {
  42. const spanText = spans[i].textContent?.trim() || '';
  43. spanValues.push(spanText);
  44. }
  45. const firstTwoSpanValues = spanValues.slice(0, 2);
  46. // console.log("前两个 Span 标签的值:", firstTwoSpanValues);
  47. textContent = firstTwoSpanValues[0] || ''
  48. console.log("textContent", textContent)
  49. }
  50. // 查找单元格内所有带有背景图片的div元素
  51. const divsWithBackground = Array.from(cell.querySelectorAll('div[style*="background-image"]'))
  52. .map((div, divIndex) => {
  53. const backgroundUrl = extractBackgroundUrl(div);
  54. if (backgroundUrl) {
  55. return {
  56. url: backgroundUrl,
  57. className: div.className || '',
  58. style: div.getAttribute('style') || ''
  59. };
  60. }
  61. return null;
  62. })
  63. .filter(item => item !== null);
  64. // 如果找到带背景图片的div,返回包含这些信息的对象
  65. if (divsWithBackground.length > 0) {
  66. return {
  67. text: textContent,
  68. backgroundImages: divsWithBackground
  69. };
  70. }
  71. // 检查单元格本身是否有背景图片
  72. const cellBackgroundUrl = extractBackgroundUrl(cell);
  73. if (cellBackgroundUrl) {
  74. return {
  75. text: textContent,
  76. cellBackgroundUrl: cellBackgroundUrl
  77. };
  78. }
  79. // 否则返回文本内容
  80. return textContent;
  81. });
  82. });
  83. }).flat();
  84. }).flat();
  85. // 过滤掉空表格
  86. const nonEmptyTables = tables.filter(table => table.length > 0);
  87. sendResponse({ tables: nonEmptyTables });
  88. }
  89. // 模拟用户重复性操作
  90. if (request.action === "autoSendMessages") {
  91. OptionScript(request, sender, sendResponse)
  92. }
  93. });
  94. }
  95. });