-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtbodyExtr.html
More file actions
181 lines (156 loc) · 8.3 KB
/
tbodyExtr.html
File metadata and controls
181 lines (156 loc) · 8.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!-- v2.3 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>tBodyExtractor</title>
<link href="https://minisoft.it/icons/fix.png" rel="shortcut icon" type="image/x-icon" />
<link href="https://minisoft.it/icons/fix.png" rel="apple-touch-icon" />
<script src="https://cdn.tailwindcss.com"></script>
<script>
tailwind.config = {
theme: {
extend: {
colors: {
primary: '#006E81',
'primary-dark': '#006E81',
secondary: '#814000',
}
}
}
}
</script>
</head>
<body class="bg-gray-100 flex flex-col min-h-screen">
<header class="bg-white border-b border-gray-200">
<div class="container mx-auto px-4 py-6 max-w-4xl">
<h1 class="text-3xl font-bold text-center text-gray-800">tBody_Extractor</h1>
<p class="text-gray-600 text-center mt-1">HTML Table Data Extractor</p>
</div>
</header>
<main class="container mx-auto px-4 py-8 max-w-4xl flex-grow">
<!-- Instructions Section -->
<div class="bg-white rounded-lg shadow-md p-6 mb-8">
<h2 class="text-xl font-semibold text-gray-800 mb-4">Instructions</h2>
<div class="bg-slate-50 border-l-4 border-primary p-4 rounded-r">
<ol class="list-decimal pl-5 space-y-1">
<li>Paste your <tbody> HTML content in the input area below.</li>
<li>Specify the column indices you want to extract, separated by commas (e.g., 1,2).</li>
<li>Click "Extract Data" to process the table and view the extracted content.</li>
</ol>
</div>
</div>
<div class="bg-white rounded-lg shadow-md p-6">
<form id="extraction-form" class="space-y-4">
<div>
<label for="html" class="block text-sm font-medium text-gray-700 mb-2">Paste your <tbody> here</label>
<textarea id="htmlInput" class="w-full h-40 p-3 border border-gray-300 rounded-lg resize-y"
required></textarea>
</div>
<div>
<label for="columns" class="block text-sm font-medium text-gray-700 mb-2">Column indices to extract</label>
<input type="text" id="columnsInput" class="w-full p-3 border border-gray-300 rounded-lg"
placeholder="1,2" required />
</div>
<button type="button" id="extractBtn" class="bg-primary text-white px-4 py-2 rounded-lg hover:bg-primary-dark transition duration-200">
Extract Data
</button>
</form>
<div id="result" class="mt-8 hidden">
<h2 class="text-xl font-semibold text-primary mb-4 text-left">Extracted Data</h2>
<div class="overflow-x-auto">
<table id="resultTable" class="min-w-full divide-y divide-gray-200">
<thead class="bg-primary">
<tr id="tableHeader">
<!-- Headers will be added here by JavaScript -->
</tr>
</thead>
<tbody id="tableBody" class="bg-white divide-y divide-gray-200">
<!-- Table data will be added here by JavaScript -->
</tbody>
</table>
</div>
</div>
</div>
</main>
<footer class="bg-white border-t border-gray-200 mt-auto">
<div class="container mx-auto px-4 py-6 max-w-4xl">
<p class="text-gray-600 text-center">© <a href="https://minisoft.it/" class="text-primary hover:text-primary-dark">Minisoft</a> — All rights reserved</p>
</div>
</footer>
<script>
document.addEventListener('DOMContentLoaded', function() {
const extractBtn = document.getElementById('extractBtn');
const htmlInput = document.getElementById('htmlInput');
const columnsInput = document.getElementById('columnsInput');
const resultDiv = document.getElementById('result');
const tableHeader = document.getElementById('tableHeader');
const tableBody = document.getElementById('tableBody');
extractBtn.addEventListener('click', function() {
// Validate inputs
if (!htmlInput.value.trim() || !columnsInput.value.trim()) {
alert('Please fill in all fields.');
return;
}
// Get selected columns
const selectedColumns = columnsInput.value.split(',').map(col => {
return parseInt(col.trim()) - 1; // Convert to 0-based index
}).filter(index => !isNaN(index) && index >= 0);
if (selectedColumns.length === 0) {
alert('Please enter valid column indices.');
return;
}
try {
// Parse HTML content
const parser = new DOMParser();
// Wrap input in a proper HTML structure if needed
let htmlContent = htmlInput.value.trim();
if (!htmlContent.includes('<table')) {
htmlContent = '<table>' + htmlContent + '</table>';
}
const doc = parser.parseFromString(htmlContent, 'text/html');
const rows = doc.querySelectorAll('tbody tr');
if (rows.length === 0) {
alert('No table rows found. Make sure your HTML contains valid <tbody> and <tr> elements.');
return;
}
// Clear previous results
tableHeader.innerHTML = '';
tableBody.innerHTML = '';
// Add headers
selectedColumns.forEach(index => {
const th = document.createElement('th');
th.className = 'px-6 py-3 text-left text-xs font-medium text-white uppercase tracking-wider';
th.textContent = `Column ${index + 1}`;
tableHeader.appendChild(th);
});
// Add data rows
rows.forEach(row => {
const cells = row.querySelectorAll('td');
if (cells.length > 0) {
const tr = document.createElement('tr');
selectedColumns.forEach(index => {
const td = document.createElement('td');
td.className = 'px-6 py-4 whitespace-nowrap text-sm text-gray-900';
if (index >= 0 && index < cells.length) {
td.textContent = cells[index].textContent.trim();
} else {
td.textContent = 'N/A';
}
tr.appendChild(td);
});
tableBody.appendChild(tr);
}
});
// Show results
resultDiv.classList.remove('hidden');
} catch (error) {
console.error('Error parsing HTML:', error);
alert('Failed to parse HTML content. Please check your input and try again.');
}
});
});
</script>
</body>
</html>