-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-request.html
More file actions
221 lines (196 loc) · 6.08 KB
/
api-request.html
File metadata and controls
221 lines (196 loc) · 6.08 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<!DOCTYPE html>
<html>
<head>
<title>API Request Demo - Zendriver</title>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"
rel="stylesheet"
/>
<style>
body {
background-color: #f5f5f5;
min-height: 100vh;
padding: 2rem 1rem;
}
.card {
max-width: 600px;
margin: 2rem auto;
transition: all 0.3s ease;
}
.card:hover {
transform: translateY(-2px);
box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
}
.loading-container {
text-align: center;
padding: 3rem;
color: rgba(0, 0, 0, 0.54);
}
.spinner-border {
width: 3rem;
height: 3rem;
color: #1976d2;
}
.error-message {
text-align: center;
color: #dc3545;
padding: 2rem;
}
.profile-header {
display: flex;
align-items: center;
gap: 1.5rem;
margin-bottom: 1.5rem;
}
.profile-image {
width: 100px;
height: 100px;
border-radius: 50%;
object-fit: cover;
}
.profile-name {
margin: 0;
color: #1976d2;
}
.profile-title {
color: #666;
margin: 0.5rem 0 0 0;
}
.info-label {
font-weight: 500;
color: #666;
}
.retry-button {
margin-top: 1rem;
}
.disclaimer {
position: fixed;
bottom: 1rem;
left: 0;
right: 0;
text-align: center;
z-index: 1000;
color: rgba(0, 0, 0, 0.6);
}
</style>
</head>
<body>
<div class="container">
<h1 class="text-center mt-4 mb-4">API Request Demo</h1>
<p class="text-center mb-4" style="max-width: 600px; margin: 0 auto">
This is a demo page for
<a href="https://github.com/stephanlensky/zendriver">Zendriver</a>
to show how to scrape a website that loads data via API requests.
</p>
<div id="content">
<!-- Content will be loaded here -->
<div class="loading-container">
<div class="spinner-border" role="status">
<span class="visually-hidden">Loading...</span>
</div>
<p class="mt-3">Loading user data...</p>
</div>
</div>
</div>
<div class="disclaimer">
<small>
This is a demo page for
<a href="https://github.com/stephanlensky/zendriver">Zendriver</a>. Data
is loaded from a static JSON file.
</small>
</div>
<script>
async function fetchUserData() {
try {
// Show loading state
const content = document.getElementById("content");
content.innerHTML = `
<div class="loading-container">
<div class="spinner-border" role="status">
<span class="visually-hidden">Loading...</span>
</div>
<p class="mt-3">Loading user data...</p>
</div>
`;
// Simulate network delay
await new Promise((resolve) => setTimeout(resolve, 1500));
// Fetch the JSON file
const response = await fetch("user-data.json");
if (!response.ok) {
throw new Error(`HTTP error status: ${response.status}`);
}
const userData = await response.json();
displayUserData(userData);
} catch (error) {
displayError(error);
console.error("Error fetching user data:", error);
}
}
function displayUserData(user) {
const content = document.getElementById("content");
content.innerHTML = `
<div class="card">
<div class="card-body">
<div class="profile-header">
<img
src="${user.avatar}"
alt="Profile picture of ${user.name}"
class="profile-image"
aria-label="Profile avatar"
>
<div>
<h2 class="profile-name" aria-label="Name">${user.name}</h2>
<p class="profile-title" aria-label="Job title">${
user.title
}</p>
</div>
</div>
<div class="mb-3" role="group" aria-label="Contact information">
<span class="info-label" id="email-label">Email:</span>
<span aria-labelledby="email-label">${user.email}</span>
</div>
<div class="mb-3" role="group" aria-label="Location information">
<span class="info-label" id="location-label">Location:</span>
<span aria-labelledby="location-label">${user.location}</span>
</div>
<div class="mb-3" role="group" aria-label="Biography">
<span class="info-label" id="bio-label">Bio:</span>
<p class="mt-2" aria-labelledby="bio-label">${user.bio}</p>
</div>
<div class="mb-3" role="group" aria-label="Skills">
<span class="info-label" id="skills-label">Skills:</span>
<div class="mt-2" aria-labelledby="skills-label">
${user.skills
.map(
(skill) => `
<span class="badge bg-primary me-2 mb-2" role="listitem">${skill}</span>
`
)
.join("")}
</div>
</div>
</div>
</div>
`;
}
function displayError(error) {
const content = document.getElementById("content");
content.innerHTML = `
<div class="error-message">
<h3>Error Loading Data</h3>
<p>${error.message}</p>
<button
class="btn btn-primary retry-button"
onclick="fetchUserData()"
aria-label="Retry loading user data"
>
Retry
</button>
</div>
`;
}
// Start the data fetch when page loads
fetchUserData();
</script>
</body>
</html>