连续复制
一键复制
一键打包
function extractTableData() {
    const table = document.querySelector('table');
    const rows = table.querySelectorAll('tbody tr');
    const jsonData = [];
    const stack = [];
    const levelCounts = {};  // Track the count of items at each level

    rows.forEach(row => {
        const cells = row.querySelectorAll('td');
        const level = parseInt(row.getAttribute('level') || 0, 10);

        // Update the count for this level
        if (!levelCounts[level]) {
            levelCounts[level] = 0;
        }
        levelCounts[level]++;

        // Retain "必填" initially and then clean up
        let fieldText = cells[0].textContent.trim();
        let hasRequired = false;

        if (fieldText.includes('必填')) {
            fieldText = fieldText.replace('必填', '').trim(); // Remove "必填" and trim spaces
            hasRequired = true;
        }
        
        const rowData = {
            field: fieldText,
            type: cells[1].textContent.trim(),
            description: cells[2].textContent.trim() + ' ### ' + cells[3].textContent.trim(),
            hasRequired: hasRequired,
            children: []
        };

        // Determine where to place the current row data based on the level
        while (stack.length > 0 && stack[stack.length - 1].level >= level) {
            stack.pop();
        }

        if (stack.length > 0) {
            stack[stack.length - 1].data.children.push(rowData);
        } else {
            jsonData.push(rowData);
        }

        // Push the current row data onto the stack
        stack.push({ data: rowData, level: level });
    });

    console.log(`Number of items at level 1: ${levelCounts[1] || 0}`);  // Print number of items at level 1
    return jsonData;
}

function generateKeyValuePairs(data, level = 0) {
    let result = '';
    
    data.forEach((item, index) => {
        // Indent according to the level to maintain hierarchy
        const indent = '  '.repeat(level);

        // Add the field and its empty value
        result += `${indent}"${item.field}":`;

        // Format the comment based on whether "必填" is present
        const commentPrefix = item.hasRequired ? ' // !必填 ' : ' // ';
        const description = item.description;

        // If there are children, format as an object
        if (item.children.length > 0) {
            result += ` {\n`;
            // Add the formatted comment
            result += `${indent}  ${commentPrefix}${description}\n`;
            // Recursively process children
            result += generateKeyValuePairs(item.children, level + 1);
            result += `${indent}},`; // Add closing brace with comma
        } else {
            result += ` ""`; // If no children, just an empty string
            // Add the formatted comment
            result += `, ${commentPrefix}${description}`;
        }

        // Add a newline after each item
        result += `\n`;
    });

    return result;
}

// Extract table data and generate key-value pairs with comments
const jsonData = extractTableData();
const formattedOutput = generateKeyValuePairs(jsonData);

// Format the final JSON object
console.log(`{\n${formattedOutput.trim().replace(/,\n$/, '\n')}\n}`);