File size: 8,428 Bytes
743aa53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9f0009
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Data Extractor</title>
<style>
  :root { --p: #00f0ff; --bg: #07070c; --panel: #12121c; --line: rgba(255,255,255,.12); }
  * { box-sizing: border-box; margin: 0; padding: 0; }
  body { background: var(--bg); color: #eee; font-family: 'Segoe UI', Arial, sans-serif; padding: 24px; }
  .wrap { max-width: 860px; margin: 0 auto; }
  h1 { font-size: 26px; margin-bottom: 6px; }
  h1 span { color: var(--p); }
  p.sub { color: #9aa; margin-bottom: 20px; }
  .card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 18px; margin-bottom: 16px; }
  label { display: block; font-size: 13px; color: #aaa; margin: 12px 0 4px; }
  label:first-child { margin-top: 0; }
  select, textarea { width: 100%; padding: 10px; border-radius: 8px; border: 1px solid var(--line); background: #0a0a12; color: #fff; font-size: 14px; }
  textarea { min-height: 160px; font-family: Consolas, monospace; font-size: 13px; resize: vertical; }
  .row { display: flex; gap: 12px; }
  .row > div { flex: 1; }
  pre { background: #0a0a12; border: 1px solid var(--line); border-radius: 10px; padding: 14px; overflow-x: auto; font-size: 13px; line-height: 1.5; white-space: pre; margin-top: 8px; }
  h2 { font-size: 16px; margin-bottom: 4px; }
  .btns { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
  button { padding: 9px 16px; border-radius: 8px; border: 1px solid var(--line); background: #1c1c2a; color: #fff; cursor: pointer; font-size: 14px; }
  button.primary { background: var(--p); color: #000; border-color: var(--p); font-weight: 700; }
  button:hover { opacity: .85; }
  .note { color: #888; font-size: 13px; margin-top: 14px; }
  .status { color: #9aa; font-size: 13px; margin-top: 8px; }
  table.preview { width: 100%; border-collapse: collapse; margin-top: 10px; font-size: 13px; }
  table.preview th, table.preview td { border: 1px solid var(--line); padding: 6px 8px; text-align: left; }
  table.preview th { background: #1c1c2a; }
</style>
</head>
<body>
<div class="wrap">
  <h1>HTML Data <span>Extractor</span></h1>
  <p class="sub">Paste HTML, get a table back as CSV or JSON. No fetching, no server — everything runs in your browser, on HTML you paste yourself.</p>

  <div class="card">
    <div class="row">
      <div>
        <label for="mode">Extract</label>
        <select id="mode">
          <option value="tables">Tables (&lt;table&gt;)</option>
          <option value="links">Links (&lt;a href&gt;)</option>
        </select>
      </div>
      <div>
        <label for="tablePick">Table</label>
        <select id="tablePick"></select>
      </div>
    </div>
    <label for="input">Your HTML</label>
    <textarea id="input" placeholder="&lt;table&gt;&lt;tr&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Price&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Widget&lt;/td&gt;&lt;td&gt;9.99&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;"></textarea>
    <div class="btns">
      <button class="primary" onclick="extract()">Extract</button>
      <button onclick="loadSample()">Load sample</button>
      <button onclick="clearAll()">Clear</button>
    </div>
    <p class="status" id="status"></p>
  </div>

  <div class="card" id="result-card" style="display:none">
    <h2>Preview</h2>
    <div id="preview"></div>
    <h2 style="margin-top:16px">CSV</h2>
    <pre id="out-csv"></pre>
    <h2 style="margin-top:16px">JSON</h2>
    <pre id="out-json"></pre>
    <div class="btns">
      <button class="primary" onclick="copyText('out-csv')">Copy CSV</button>
      <button onclick="downloadText('out-csv', 'extracted.csv')">Download CSV</button>
      <button onclick="copyText('out-json')">Copy JSON</button>
      <button onclick="downloadText('out-json', 'extracted.json')">Download JSON</button>
    </div>
  </div>

  <p class="note">Tip: paste only HTML you are allowed to use — your own pages, or pages made for practice such as quotes.toscrape.com. This tool never fetches a URL by itself.</p>
</div>
<script>
function escapeCsv(v) {
  v = String(v == null ? '' : v);
  return /[",\n]/.test(v) ? '"' + v.replace(/"/g, '""') + '"' : v;
}
function rowsToCSV(rows) {
  return rows.map(function (r) { return r.map(escapeCsv).join(','); }).join('\n');
}
function rowsToJSON(rows) {
  if (!rows.length) return '[]';
  var head = rows[0];
  return JSON.stringify(rows.slice(1).map(function (r) {
    var o = {};
    head.forEach(function (h, i) { o[h || ('col' + (i + 1))] = r[i] == null ? '' : r[i]; });
    return o;
  }), null, 2);
}
function tableRows(table) {
  var out = [];
  var trs = table.querySelectorAll('tr');
  trs.forEach(function (tr) {
    var cells = tr.querySelectorAll('th, td');
    if (cells.length) out.push(Array.prototype.map.call(cells, function (c) { return c.textContent.trim(); }));
  });
  return out;
}
function loadSample() {
  document.getElementById('input').value =
    '<table>\n  <tr><th>Name</th><th>Price</th><th>In stock</th></tr>\n' +
    '  <tr><td>Widget A</td><td>9.99</td><td>yes</td></tr>\n' +
    '  <tr><td>Widget B</td><td>14.50</td><td>no</td></tr>\n</table>';
  extract();
}
function clearAll() {
  document.getElementById('input').value = '';
  document.getElementById('result-card').style.display = 'none';
  document.getElementById('status').textContent = '';
}
function showRows(rows, label) {
  var card = document.getElementById('result-card');
  if (!rows.length) {
    card.style.display = 'none';
    document.getElementById('status').textContent = label;
    return;
  }
  card.style.display = 'block';
  document.getElementById('status').textContent = rows.length + ' rows found.';
  var html = '<table class="preview">';
  rows.forEach(function (r, i) {
    html += '<tr>' + r.map(function (c) {
      var tag = i === 0 ? 'th' : 'td';
      return '<' + tag + '>' + String(c).replace(/&/g, '&amp;').replace(/</g, '&lt;') + '</' + tag + '>';
    }).join('') + '</tr>';
  });
  document.getElementById('preview').innerHTML = html + '</table>';
  document.getElementById('out-csv').textContent = rowsToCSV(rows);
  document.getElementById('out-json').textContent = rowsToJSON(rows);
}
var lastDoc = null;
function extract() {
  var html = document.getElementById('input').value;
  var mode = document.getElementById('mode').value;
  var pick = document.getElementById('tablePick');
  if (!html.trim()) {
    document.getElementById('status').textContent = 'Paste some HTML first — or press "Load sample".';
    return;
  }
  lastDoc = new DOMParser().parseFromString(html, 'text/html');
  if (mode === 'links') {
    var links = Array.prototype.map.call(lastDoc.querySelectorAll('a[href]'), function (a) {
      return [a.textContent.trim(), a.getAttribute('href')];
    });
    showRows([['text', 'href']].concat(links), 'No links found in this HTML.');
    pick.innerHTML = '';
    return;
  }
  var tables = lastDoc.querySelectorAll('table');
  if (!tables.length) {
    showRows([], 'No <table> found in this HTML. Try "Links" mode, or paste HTML that contains a table.');
    pick.innerHTML = '';
    return;
  }
  pick.innerHTML = '';
  tables.forEach(function (t, i) {
    var opt = document.createElement('option');
    opt.value = i;
    opt.textContent = 'Table ' + (i + 1) + ' — ' + t.querySelectorAll('tr').length + ' rows';
    pick.appendChild(opt);
  });
  showRows(tableRows(tables[Number(pick.value) || 0]), '');
}
function copyText(id) {
  var t = document.getElementById(id).textContent;
  if (navigator.clipboard) navigator.clipboard.writeText(t);
}
function downloadText(id, name) {
  var t = document.getElementById(id).textContent;
  var a = document.createElement('a');
  a.href = URL.createObjectURL(new Blob([t], { type: 'text/plain' }));
  a.download = name; a.click();
}
document.getElementById('mode').addEventListener('change', function () {
  document.getElementById('tablePick').disabled = this.value === 'links';
  if (document.getElementById('input').value.trim()) extract();
});
document.getElementById('tablePick').addEventListener('change', function () {
  if (!lastDoc) return;
  var tables = lastDoc.querySelectorAll('table');
  showRows(tableRows(tables[Number(this.value) || 0]), '');
});
if (typeof module !== 'undefined') { module.exports = { escapeCsv: escapeCsv, rowsToCSV: rowsToCSV, rowsToJSON: rowsToJSON }; }
</script>
</body>
</html>