-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
182 lines (151 loc) · 6.57 KB
/
app.js
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
document.addEventListener('DOMContentLoaded', function() {
const form = document.getElementById('scrapeForm');
const loadingElement = document.createElement('div');
loadingElement.id = 'loading';
loadingElement.innerHTML = 'Loading... <div class="spinner"></div>';
loadingElement.style.display = 'none';
document.querySelector('.container').appendChild(loadingElement);
form.addEventListener('submit', function(e) {
e.preventDefault();
const url = document.getElementById('url').value.trim();
if (!url) {
showError('Please enter a URL');
return;
}
// Show loading indicator
loadingElement.style.display = 'block';
// Create and open a new tab for downloading Excel file
const downloadUrl = `/scrape?url=${encodeURIComponent(url)}`;
window.open(downloadUrl, '_blank');
// Make a request to get JSON data for display on the page
fetch(`/scrape?url=${encodeURIComponent(url)}`, {
headers: {
'Accept': 'application/json'
}
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
// Hide loading indicator
loadingElement.style.display = 'none';
// Display results
displayResults(data);
})
.catch(error => {
// Hide loading indicator
loadingElement.style.display = 'none';
console.error('Error:', error);
showError('An error occurred while fetching data: ' + error.message);
});
});
function displayResults(data) {
let resultsContainer = document.getElementById('resultsContainer');
if (!resultsContainer) {
// Create container if it doesn't exist yet
const container = document.querySelector('.container');
resultsContainer = document.createElement('div');
resultsContainer.id = 'resultsContainer';
container.appendChild(resultsContainer);
const resultsTitle = document.createElement('h2');
resultsTitle.textContent = 'Extracted Data';
resultsContainer.appendChild(resultsTitle);
} else {
resultsContainer.innerHTML = '';
const resultsTitle = document.createElement('h2');
resultsTitle.textContent = 'Extracted Data';
resultsContainer.appendChild(resultsTitle);
}
if (data.length === 0) {
const noData = document.createElement('p');
noData.className = 'no-data';
noData.textContent = 'No data found';
resultsContainer.appendChild(noData);
return;
}
// Group data by type
const groupedData = {};
data.forEach(item => {
if (!groupedData[item.type]) {
groupedData[item.type] = [];
}
groupedData[item.type].push(item);
});
// Create a table to display structured data
const table = document.createElement('table');
table.id = 'data-table';
// Create table header
const thead = document.createElement('thead');
const headerRow = document.createElement('tr');
const headers = ['Type', 'Tag', 'Content', 'URL'];
headers.forEach(headerText => {
const th = document.createElement('th');
th.textContent = headerText;
headerRow.appendChild(th);
});
thead.appendChild(headerRow);
table.appendChild(thead);
// Create table body
const tbody = document.createElement('tbody');
// Add rows for each type of content
Object.keys(groupedData).forEach(type => {
// Add section header
const sectionRow = document.createElement('tr');
sectionRow.className = 'section-header';
const sectionCell = document.createElement('td');
sectionCell.colSpan = headers.length;
sectionCell.textContent = type.charAt(0).toUpperCase() + type.slice(1) + 's';
sectionRow.appendChild(sectionCell);
tbody.appendChild(sectionRow);
// Add data rows
groupedData[type].forEach(item => {
const dataRow = document.createElement('tr');
const typeCell = document.createElement('td');
typeCell.textContent = item.type;
dataRow.appendChild(typeCell);
const tagCell = document.createElement('td');
tagCell.textContent = item.tag;
dataRow.appendChild(tagCell);
const contentCell = document.createElement('td');
contentCell.textContent = item.text;
dataRow.appendChild(contentCell);
const urlCell = document.createElement('td');
if (item.url) {
const link = document.createElement('a');
link.href = item.url;
link.textContent = item.url;
link.target = '_blank';
urlCell.appendChild(link);
}
dataRow.appendChild(urlCell);
tbody.appendChild(dataRow);
});
});
table.appendChild(tbody);
resultsContainer.appendChild(table);
// Show summary
const summary = document.createElement('div');
summary.className = 'summary';
const counts = Object.keys(groupedData).map(type => {
return `${groupedData[type].length} ${type}s`;
}).join(', ');
summary.textContent = `Found: ${counts}`;
resultsContainer.appendChild(summary);
}
function showError(message) {
const errorContainer = document.getElementById('error') || document.createElement('div');
if (!errorContainer.id) {
errorContainer.id = 'error';
document.querySelector('.container').insertBefore(errorContainer, form.nextSibling);
}
errorContainer.textContent = message;
errorContainer.style.display = 'block';
// Automatically hide message after 5 seconds
setTimeout(() => {
errorContainer.style.display = 'none';
}, 5000);
}
});