-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
371 lines (306 loc) · 12.4 KB
/
index.html
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Wiki User Details</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 0;
background-color: #f8f9fa;
color: #343a40;
text-align: center;
overflow-x: hidden; /* Prevent horizontal scrollbar */
}
header {
background-color: #007bff;
color: white;
padding: 1em;
text-align: center;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.columns {
display: flex;
width: 100%;
}
.column {
flex: 1;
padding: 10px;
}
main {
max-width: 600px;
margin: 20px auto;
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
animation: fadeIn 0.5s ease-in-out; /* Fade-in animation */
}
form {
display: flex;
flex-direction: column;
align-items: center;
}
label {
margin-bottom: 10px;
color: #007bff;
}
input {
padding: 10px;
margin-bottom: 20px;
width: 60%;
box-sizing: border-box;
border: 1px solid #ced4da;
border-radius: 5px;
}
button {
background-color: #007bff;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
border-radius: 5px;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #0056b3;
}
#wishAnniversaryButton {
background-color: #007bff;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
border-radius: 5px;
transition: background-color 0.3s ease
}
#wishAnniversaryButton:hover {
background-color: #0056b3;
}
#userDetails {
margin-top: 20px;
animation: fadeIn 0.5s ease-in-out; /* Fade-in animation */
display: flex;
flex-wrap: wrap;
text-align: center;
}
#userDetails h2 {
color: #007bff;
text-align: center;
margin-bottom: 10px;
}
#userDetails p {
margin: 8px 0;
}
#daysToAnniversary {
text-align: center;
color: #007bff;
font-weight: bold;
margin-top: 20px;
font-size: 20px;
margin-bottom: 20px;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
</style>
</head>
<body>
<header>
<h1>Wiki Anniversary (User)</h1>
</header>
<main>
<div id="usernameInput" style="display: none;">
<form id="userForm">
<label for="username">Enter Username:</label>
<input type="text" id="username" name="username" placeholder="Wiki Username" required onkeypress="handleKeyPress(event)">
<button type="button" onclick="getUserDetails()">Get User Details</button>
</form>
</div>
<div class="header" id="usernameHeader"></div>
<div id="userDetails" class="columns"></div>
<button id="wishAnniversaryButton" style="display: none;" onclick="handleWishAnniversary()">Wish Anniversary</button>
<div id="daysToAnniversary"></div>
</main>
<script>
setTimeout(function () {
document.getElementById('usernameInput').style.display = 'block';
// Check if the URL contains the username parameter
const urlParams = new URLSearchParams(window.location.search);
const usernameFromURL = urlParams.get('username');
if (usernameFromURL) {
// Set the username in the input field
document.getElementById('username').value = usernameFromURL;
// Trigger the user details retrieval
getUserDetails();
}
}, 2000);
function getUserDetails() {
const username = document.getElementById('username').value;
fetch(`https://www.mediawiki.org/w/api.php?action=query&format=json&list=&meta=globaluserinfo&utf8=1&formatversion=2&guiuser=${username}&guiprop=editcount|rights|merged|groups|unattached&origin=*`, {
method: 'GET',
mode: 'cors',
jsonp: 'callback',
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
displayUserDetails(data.query.globaluserinfo);
displayDaysToAnniversary(data.query.globaluserinfo.registration);
showButtons(); // Show the additional buttons
})
.catch(error => {
console.error('Error fetching user data:', error);
});
}
let homewikiUrl; // Declare a variable to store the home wiki URL
async function displayUserDetails(user) {
const userDetailsDiv = document.getElementById('userDetails');
const usernameHeader = document.getElementById('usernameHeader');
const daysToAnniversaryDiv = document.getElementById('daysToAnniversary');
userDetailsDiv.innerHTML = '';
usernameHeader.innerHTML = '';
daysToAnniversaryDiv.innerHTML = '';
if (user.name) {
usernameHeader.innerHTML = `<h2>${user.name}</h2>`;
const registrationDate = new Date(user.registration);
const formattedDate = registrationDate.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
const leftColumn = document.createElement('div');
leftColumn.classList.add('column');
leftColumn.innerHTML = `<p><strong>Registration Date:</strong></p>
<p>${formattedDate}</p>
<p><strong>Total Edit Count:</strong></p>
<p>${user.editcount}</p>`;
if (user.groups.length > 0) {
leftColumn.innerHTML += `<p><strong>Groups:</strong></p><p> ${user.groups.join(', ')}</p>`;
}
const rightColumn = document.createElement('div');
rightColumn.classList.add('column');
try {
// Fetch homewiki URL asynchronously and store it in the variable
homewikiUrl = await fetchHomewikiUrl(user.home, user.merged);
rightColumn.innerHTML = `<p><strong>Home Wiki:</strong></p>
<p><a href="${homewikiUrl}" target="_blank">${user.home}</a></p>
<p><strong>ID:</strong></p>
<p>${user.id}</p>`;
} catch (error) {
console.error("Error fetching homewiki URL:", error);
rightColumn.innerHTML = `<p><strong>Home Wiki:</strong></p>
<p>Not available</p>
<p><strong>Home Wiki ID:</strong></p>
<p>${user.id}</p>`;
}
if (user.rights.length > 0) {
rightColumn.innerHTML += `<p><strong>Rights:</strong></p><p> ${user.rights.join(', ')}</p>`;
}
userDetailsDiv.appendChild(leftColumn);
userDetailsDiv.appendChild(rightColumn);
// Call displayDaysToAnniversary with the user's registration date
displayDaysToAnniversary(user.registration, daysToAnniversaryDiv);
} else {
usernameHeader.innerHTML = `<h2 style="color: red;">User not found: ${document.getElementById('username').value}</h2>`;
daysToAnniversaryDiv.innerHTML = '';
}
}
// Function to fetch the homewiki URL asynchronously
async function fetchHomewikiUrl(homeWiki, mergedEntries) {
const matchingEntry = mergedEntries.find(entry => entry.wiki === homeWiki);
if (matchingEntry) {
return matchingEntry.url;
} else {
return "Not available";
}
}
// Function to handle the click event of the wishAnniversaryButton
function handleWishAnniversary() {
const username = document.getElementById('username').value;
// Construct the URL using the stored homewikiUrl variable
const anniversaryURL = `${homewikiUrl}/w/index.php?title=User_talk:${username}&action=edit§ion=new`;
// Open the URL in a new tab
window.open(anniversaryURL, '_blank');
}
// Add event listener to the wishAnniversaryButton
document.getElementById('wishAnniversaryButton').addEventListener('click', handleWishAnniversary);
function displayDaysToAnniversary(registrationDate, daysToAnniversaryDiv) {
const anniversaryDate = new Date(registrationDate);
const today = new Date();
// Create a copy of anniversaryDate to avoid modifying it directly
const anniversaryDateCopy = new Date(anniversaryDate);
// Set the anniversary date to the current year
anniversaryDateCopy.setFullYear(today.getFullYear());
// If today is the anniversary day, set it to next year
if (today >= anniversaryDateCopy) {
anniversaryDateCopy.setFullYear(today.getFullYear() + 1);
}
// Calculate the difference in days between the current date and the anniversary date
const daysToAnniversary = Math.ceil((anniversaryDateCopy - today) / (1000 * 60 * 60 * 24));
// Display the number of days to the next anniversary
daysToAnniversaryDiv.innerHTML = `<strong>Days to next Anniversary:</strong> ${daysToAnniversary}`;
if (daysToAnniversary === 0 || (daysToAnniversary < 11 || daysToAnniversary > 355)) {
showAnniversaryNotification(daysToAnniversary);
}
// Call showButtons with the calculated number of days
showButtons(daysToAnniversary);
}
function showButtons(daysToAnniversary) {
if (daysToAnniversary < 11 || daysToAnniversary > 355) {
document.getElementById('wishAnniversaryButton').style.display = 'inline-block';
} else {
document.getElementById('wishAnniversaryButton').style.display = 'none';
}
}
function handleKeyPress(event) {
if (event.key === 'Enter') {
getUserDetails();
}
}
function showAnniversaryNotification(daysToAnniversary) {
// Check if the browser supports notifications
if ('Notification' in window && 'serviceWorker' in navigator && 'PushManager' in window) {
Notification.requestPermission().then(permission => {
if (permission === 'granted') {
const username = document.getElementById('username').value;
const anniversaryURL = `${homewikiUrl}/w/index.php?title=User_talk:${username}&action=edit§ion=new`;
let notificationMessage = '';
let notificationUrl = '';
if (daysToAnniversary === 0) {
notificationMessage = `${username}'s anniversary is today! 🎉`;
notificationUrl = `${homewikiUrl}/w/index.php?title=User_talk:${username}&action=edit§ion=new`;
} else if (daysToAnniversary < 11) {
notificationMessage = `${username}'s anniversary is in ${daysToAnniversary} day${daysToAnniversary === 1 ? '' : 's'}! 🎉`;
notificationUrl = `${homewikiUrl}/w/index.php?title=User_talk:${username}&action=edit§ion=new`;
} else if (daysToAnniversary > 355) {
notificationMessage = `${username}'s anniversary was within the past 10 days! 🎉`;
notificationUrl = `${homewikiUrl}/w/index.php?title=User_talk:${username}&action=edit§ion=new`;
}
const notificationOptions = {
body: notificationMessage,
};
const notification = new Notification('Anniversary Reminder', notificationOptions);
// Add an event listener to handle the click on the notification
notification.addEventListener('click', () => {
if (notificationUrl) {
window.open(notificationUrl, '_blank');
}
});
}
});
}
}
setTimeout(function () {
document.getElementById('usernameInput').style.display = 'block';
}, 2000);
</script>
</body>
</html>