-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
398 lines (331 loc) · 13.8 KB
/
script.js
File metadata and controls
398 lines (331 loc) · 13.8 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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Mock data
const mockData = {
"ok": true,
"result": [
{
"update_id": 123456789,
"message": {
"message_id": 1,
"from": {
"id": 111111111,
"is_bot": false,
"first_name": "John",
"last_name": "Doe",
"username": "johndoe",
"language_code": "en"
},
"chat": {
"id": 111111111,
"first_name": "John",
"last_name": "Doe",
"username": "johndoe",
"type": "private"
},
"date": 1632328992,
"text": "Hello, this is a test message!"
}
},
{
"update_id": 123456790,
"my_chat_member": {
"chat": {
"id": 111111111,
"first_name": "John",
"last_name": "Doe",
"username": "johndoe",
"type": "private"
},
"from": {
"id": 111111111,
"is_bot": false,
"first_name": "John",
"last_name": "Doe",
"username": "johndoe",
"language_code": "en"
},
"date": 1632328992,
"new_chat_member": {
"status": "administrator",
"user": {
"id": 111111111,
"first_name": "John",
"last_name": "Doe",
"username": "johndoe"
}
}
}
}
]
};
// Function to validate the Telegram bot token format using regex
function validateToken(token) {
const tokenPattern = /^[0-9]*:[0-9a-zA-Z_-]{35,36}$/; // Basic Telegram bot token regex pattern
return tokenPattern.test(token);
}
// Function to display mock data in both JSON and Card views
function displayMockData() {
const resultDiv = document.getElementById('result');
const cardsContainer = document.getElementById('cardsContainer');
// Display mock data in JSON view
resultDiv.textContent = JSON.stringify(mockData, null, 2);
const formattedData = JSON.stringify(mockData, null, 2)
.replace(/"([^"]+)":/g, '<span class="data-key">"$1":</span>') // Highlight keys
.replace(/: ("[^"]*")/g, ': <span class="data-value">$1</span>') // Highlight string values
.replace(/: (\d+)/g, ': <span class="data-number">$1</span>') // Highlight numbers
.replace(/: (true|false)/g, ': <span class="data-boolean">$1</span>') // Highlight booleans
.replace(/: null/g, ': <span class="data-null">null</span>'); // Highlight null values
resultDiv.innerHTML = formattedData;
// Display mock data in Cards view
cardsContainer.innerHTML = ''; // Clear previous cards
const events = mockData.result; // Assuming events are in 'result' key
events.forEach(event => {
const card = document.createElement('div');
card.classList.add('card');
const title = document.createElement('h4');
title.textContent = `Event ID: ${event.update_id}`;
let messageContent = "No message";
let footerContent = "Unknown sender";
if (event.message) {
messageContent = event.message.text || "No text content";
footerContent = `From: ${event.message.from.username}`;
} else if (event.my_chat_member) {
messageContent = `Chat changed: ${event.my_chat_member.new_chat_member.status}`;
footerContent = `From: ${event.my_chat_member.from.username}`;
}
const message = document.createElement('p');
message.textContent = messageContent;
const footer = document.createElement('div');
footer.classList.add('card-footer');
footer.textContent = footerContent;
const showDataButton = document.createElement('button');
showDataButton.textContent = "🔎 Show Data";
showDataButton.setAttribute('aria-labelledby', 'showDataLabel');
showDataButton.setAttribute('aria-describedby', 'showDataDescription');
showDataButton.onclick = () => toggleJsonData(card, event);
const jsonSection = document.createElement('div');
jsonSection.classList.add('json-section');
jsonSection.style.display = 'none';
jsonSection.innerHTML = `<pre>${highlightJsonSyntax(event)}</pre>`;
card.appendChild(title);
card.appendChild(message);
card.appendChild(footer);
card.appendChild(showDataButton);
card.appendChild(jsonSection);
cardsContainer.appendChild(card);
});
document.getElementById('result-section').style.display = 'block';
document.getElementById('json-view').style.display = 'block';
document.getElementById('cards-view').style.display = 'block';
}
// Function to fetch updates from Telegram API
// Function to fetch updates from Telegram API
async function fetchUpdates() {
const token = document.getElementById('token').value;
// Validate token format
if (!validateToken(token)) {
alert("Invalid token format.");
return;
}
// Show the loading indicator
document.getElementById('loading').style.display = 'block';
// Hide results before the request is made
document.getElementById('result-section').style.display = 'none';
const url = `https://api.telegram.org/bot${token}/getUpdates`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('Failed to fetch updates');
}
const data = await response.json();
displayResult(data);
} catch (error) {
// Show error message on UI
const resultDiv = document.getElementById('result');
resultDiv.textContent = `Error: ${error.message}`;
resultDiv.style.color = 'red';
} finally {
// Hide the loading indicator after request completes
document.getElementById('loading').style.display = 'none';
}
}
// Function to display the result in both JSON and Card view
function displayResult(data) {
const resultDiv = document.getElementById('result');
const cardsContainer = document.getElementById('cardsContainer');
// Pretty print the JSON/YAML in the existing view
resultDiv.textContent = JSON.stringify(data, null, 2);
// Add enhanced syntax highlighting for both JSON and YAML (already present)
const formattedData = JSON.stringify(data, null, 2)
.replace(/"([^"]+)":/g, '<span class="data-key">"$1":</span>') // Highlight keys
.replace(/: ("[^"]*")/g, ': <span class="data-value">$1</span>') // Highlight string values
.replace(/: (\d+)/g, ': <span class="data-number">$1</span>') // Highlight numbers
.replace(/: (true|false)/g, ': <span class="data-boolean">$1</span>') // Highlight booleans
.replace(/: null/g, ': <span class="data-null">null</span>'); // Highlight null values
resultDiv.innerHTML = formattedData;
// Now, create cards view for the events
cardsContainer.innerHTML = ''; // Clear previous cards
const events = data.result; // Assuming events are in 'result' key
events.forEach(event => {
const card = document.createElement('div');
card.classList.add('card');
const title = document.createElement('h4');
title.textContent = `Event ID: ${event.update_id}`;
// Check if the event has a message and handle accordingly
let messageContent = "No message";
let footerContent = "Unknown sender";
if (event.message) {
messageContent = event.message.text || "No text content";
footerContent = `From: ${event.message.from.username}`;
} else if (event.my_chat_member) {
messageContent = `Chat changed: ${event.my_chat_member.new_chat_member.status}`;
footerContent = `From: ${event.my_chat_member.from.username}`;
} else if (event.new_chat_participant) {
messageContent = `New participant: ${event.new_chat_participant.first_name}`;
footerContent = `From: ${event.message ? event.message.from.username : "Unknown"}`;
} else if (event.new_chat_member) {
messageContent = `New member: ${event.new_chat_member.first_name}`;
footerContent = `From: ${event.message ? event.message.from.username : "Unknown"}`;
}
const message = document.createElement('p');
message.textContent = messageContent;
const footer = document.createElement('div');
footer.classList.add('card-footer');
footer.textContent = footerContent;
// Create the "Show Data" button with aria-labelledby and aria-describedby
const showDataButton = document.createElement('button');
showDataButton.textContent = "🔎 Show Data";
showDataButton.setAttribute('aria-labelledby', 'showDataLabel');
showDataButton.setAttribute('aria-describedby', 'showDataDescription');
showDataButton.onclick = () => toggleJsonData(card, event);
// Create a div for the JSON data (initially hidden)
const jsonSection = document.createElement('div');
jsonSection.classList.add('json-section');
jsonSection.style.display = 'none';
jsonSection.innerHTML = `<pre>${highlightJsonSyntax(event)}</pre>`; // Pretty-print JSON with syntax highlighting
// Append all elements to the card
card.appendChild(title);
card.appendChild(message);
card.appendChild(footer);
card.appendChild(showDataButton);
card.appendChild(jsonSection);
cardsContainer.appendChild(card);
});
// Show both views
document.getElementById('result-section').style.display = 'block';
document.getElementById('json-view').style.display = 'block';
document.getElementById('cards-view').style.display = 'block';
}
// Function to toggle the visibility of JSON data when clicking the "Show Data" button
function toggleJsonData(card, event) {
const jsonSection = card.querySelector('.json-section');
// Toggle the visibility of the JSON section
if (jsonSection.style.display === 'none') {
jsonSection.style.display = 'block';
} else {
jsonSection.style.display = 'none';
}
}
// Function to apply syntax highlighting to JSON data
function highlightJsonSyntax(jsonData) {
let jsonStr = JSON.stringify(jsonData, null, 2);
return jsonStr
.replace(/"([^"]+)":/g, '<span class="data-key">"$1":</span>') // Highlight keys
.replace(/: ("[^"]*")/g, ': <span class="data-value">$1</span>') // Highlight string values
.replace(/: (\d+)/g, ': <span class="data-number">$1</span>') // Highlight numbers
.replace(/: (true|false)/g, ': <span class="data-boolean">$1</span>') // Highlight booleans
.replace(/: null/g, ': <span class="data-null">null</span>'); // Highlight null values
}
// Function to toggle between JSON and Cards view
function toggleView(view) {
if (view === 'json') {
document.getElementById('json-view').style.display = 'block';
document.getElementById('cards-view').style.display = 'none';
} else {
document.getElementById('json-view').style.display = 'none';
document.getElementById('cards-view').style.display = 'block';
}
}
// Function to save token with a custom name in localStorage
function saveToken() {
const token = document.getElementById('token').value;
if (!token) {
alert("Please enter a token to save.");
return;
}
// Prompt the user for the token name
const storageName = prompt("Enter a name for your token:", "default-token");
if (storageName === null || storageName.trim() === "") {
alert("Token name is required!");
return;
}
// Check if a token with the same name already exists in localStorage
if (localStorage.getItem(storageName)) {
alert("A token with that name already exists!");
return;
}
// Save the token with the provided name
localStorage.setItem(storageName, token);
alert(`Token saved with the name "${storageName}".`);
// Refresh the list of saved tokens after saving
loadSavedTokens();
}
// Function to load all saved tokens from localStorage and display them
function loadSavedTokens() {
const tokensList = document.getElementById('tokensList');
tokensList.innerHTML = ''; // Clear current list
// Iterate over all items in localStorage
for (let i = 0; i < localStorage.length; i++) {
const storageName = localStorage.key(i);
const savedToken = localStorage.getItem(storageName);
if (savedToken) {
const listItem = document.createElement('li');
const tokenName = document.createElement('span');
tokenName.classList.add('token-name');
tokenName.textContent = `${storageName} `;
const buttonContainer = document.createElement('div');
const loadButton = document.createElement('button');
loadButton.textContent = `🚚 Load`;
loadButton.setAttribute('data-token-name', storageName);
loadButton.onclick = () => loadSelectedToken(storageName);
const removeButton = document.createElement('button');
removeButton.textContent = `🗑️ Remove`;
removeButton.onclick = () => removeToken(storageName);
buttonContainer.appendChild(loadButton);
buttonContainer.appendChild(removeButton);
listItem.appendChild(tokenName);
listItem.appendChild(buttonContainer); // Add the container with buttons
tokensList.appendChild(listItem);
}
}
// If there are no saved tokens, hide the saved tokens section
const savedTokensSection = document.getElementById('saved-tokens-section');
if (localStorage.length === 0) {
savedTokensSection.style.display = 'none';
} else {
savedTokensSection.style.display = 'block';
}
}
// Function to load selected token from localStorage
function loadSelectedToken(storageName) {
const savedToken = localStorage.getItem(storageName);
if (savedToken) {
document.getElementById('token').value = savedToken;
// Get the load button that was clicked
const loadButton = document.querySelector(`[data-token-name="${storageName}"]`);
// Change the button color to indicate it's been selected
loadButton.classList.add('button-success');
setTimeout(() => {
loadButton.classList.remove('button-success');
loadButton.style.transition = 'background-color 1s ease-out';
}, 1000); // 1 seconds
}
}
// Function to remove a token from localStorage
function removeToken(storageName) {
localStorage.removeItem(storageName);
alert(`Token "${storageName}" removed.`);
// Refresh the list of saved tokens after removing
loadSavedTokens();
}
// Load saved tokens on page load
document.addEventListener('DOMContentLoaded', loadSavedTokens);