| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import { OptionScript } from "./content_script/OptionScript";
- export default defineContentScript({
- matches: ["<all_urls>"],
- main() {
- // 辅助函数:提取背景图片URL
- const extractBackgroundUrl = (element: Element): string | null => {
- const computedStyle = window.getComputedStyle(element);
- const backgroundImage = computedStyle.backgroundImage;
- if (backgroundImage && backgroundImage !== 'none' && backgroundImage.includes('url')) {
- // 处理包含转义字符的URL
- const urlMatch = backgroundImage.match(/url\(["']?(.*?)["']?\)/);
- if (urlMatch && urlMatch[1]) {
- // 解码HTML实体
- let url = urlMatch[1];
- const textarea = document.createElement('textarea');
- textarea.innerHTML = url;
- return textarea.value;
- }
- }
- return null;
- };
- // 监听来自popup的消息
- browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
- if (request.action === "getTbodyData") {
- // 获取所有tbody中的数据,包括背景图片
- const tables = Array.from(document.querySelectorAll('table')).map((table, tableIndex) => {
- const tbodies = Array.from(table.querySelectorAll('tbody'));
- return tbodies.map(tbody => {
- const rows = Array.from(tbody.querySelectorAll('tr'));
- return rows.map((row, rowIndex) => {
- const cells = Array.from(row.querySelectorAll('td, th'));
- return cells.map((cell, cellIndex) => {
- // 获取单元格文本内容
- let textContent = cell.textContent?.trim() || '';
- // 将文本中带有WB和件的判定为单号进行特殊处理,单号位数不确定
- if (textContent.includes('WB') && textContent.includes('件')) {
- // 去除带"件"的span标签
- const spans = cell.getElementsByTagName('span');
- let spanValues: string[] = [];
- // 遍历所有 span 元素并提取文本值
- for (let i = 0; i < spans.length; i++) {
- const spanText = spans[i].textContent?.trim() || '';
- spanValues.push(spanText);
- }
- const firstTwoSpanValues = spanValues.slice(0, 2);
- // console.log("前两个 Span 标签的值:", firstTwoSpanValues);
- textContent = firstTwoSpanValues[0] || ''
- console.log("textContent", textContent)
- }
- // 查找单元格内所有带有背景图片的div元素
- const divsWithBackground = Array.from(cell.querySelectorAll('div[style*="background-image"]'))
- .map((div, divIndex) => {
- const backgroundUrl = extractBackgroundUrl(div);
- if (backgroundUrl) {
- return {
- url: backgroundUrl,
- className: div.className || '',
- style: div.getAttribute('style') || ''
- };
- }
- return null;
- })
- .filter(item => item !== null);
- // 如果找到带背景图片的div,返回包含这些信息的对象
- if (divsWithBackground.length > 0) {
- return {
- text: textContent,
- backgroundImages: divsWithBackground
- };
- }
- // 检查单元格本身是否有背景图片
- const cellBackgroundUrl = extractBackgroundUrl(cell);
- if (cellBackgroundUrl) {
- return {
- text: textContent,
- cellBackgroundUrl: cellBackgroundUrl
- };
- }
- // 否则返回文本内容
- return textContent;
- });
- });
- }).flat();
- }).flat();
- // 过滤掉空表格
- const nonEmptyTables = tables.filter(table => table.length > 0);
- sendResponse({ tables: nonEmptyTables });
- }
- // 模拟用户重复性操作
- if (request.action === "autoSendMessages") {
- OptionScript(request, sender, sendResponse)
- }
- });
- }
- });
|