Skip to content

Commit

Permalink
Update index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
hung319 authored Dec 18, 2024
1 parent 9d20479 commit 3e6ec2f
Showing 1 changed file with 76 additions and 36 deletions.
112 changes: 76 additions & 36 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@
</head>
<body>
<div class="top-info">
<div class="datetime">Current Date and Time (UTC): <span id="currentDateTime">2024-12-18 03:33:18</span></div>
<div class="user">Current User's Login: <span id="userLogin">hung319</span></div>
<div class="datetime">Ngày giờ hiện tại (Hồ Chí Minh): <span id="currentDateTime"></span></div>
<div class="user">Người dùng hiện tại: <span id="userLogin">hung319</span></div>
</div>

<div class="container">
Expand All @@ -194,16 +194,64 @@ <h1>Tải ảnh NSFW</h1>
</div>

<script>
// Cập nhật datetime theo múi giờ Hồ Chí Minh
function updateDateTime() {
const now = new Date();
const options = {
timeZone: "Asia/Ho_Chi_Minh",
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
};
const formatter = new Intl.DateTimeFormat("vi-VN", options);
const dateTimeString = formatter.format(now).replace(",", ""); // Bỏ dấu phẩy
document.getElementById("currentDateTime").textContent = dateTimeString;
}
setInterval(updateDateTime, 1000);

const downloadBtn = document.getElementById('downloadBtn');
const status = document.getElementById('status');
const imageCountInput = document.getElementById('imageCount');
const loadingAnimation = document.getElementById('loadingAnimation');
const CONCURRENT_LIMIT = 1000;
const API_URL = 'https://waifu.pics/api/nsfw/waifu';
const WORKER_URL = 'https://white-mud-f95d.hoangxg4.workers.dev/?apiUrl=';

const generateRandomString = (length) => {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
};

const fetchWithRetry = async (url, options, retries = 3) => {
try {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response;
} catch (error) {
if (retries > 0) {
console.log(`Retry attempt ${4 - retries}...`);
return fetchWithRetry(url, options, retries - 1);
}
throw error;
}
};

const fetchImage = async () => {
try {
const response = await fetch(`${WORKER_URL}${encodeURIComponent(API_URL)}`);
const response = await fetchWithRetry(`${WORKER_URL}${encodeURIComponent(API_URL)}`);
const data = await response.json();
const imageUrl = data.url;
const imageResponse = await fetch(`${WORKER_URL}${encodeURIComponent(imageUrl)}`);
const imageResponse = await fetchWithRetry(`${WORKER_URL}${encodeURIComponent(imageUrl)}`);
const imageBlob = await imageResponse.blob();
const fileName = imageUrl.split('/').pop();
return { imageBlob, fileName };
Expand All @@ -216,37 +264,32 @@ <h1>Tải ảnh NSFW</h1>
const downloadImages = async (count) => {
const zip = new JSZip();
let completed = 0;
let retries = 0;

while (completed < count && retries < 5) {
const remaining = count - completed;

const promises = Array.from({ length: remaining }, () =>
fetchImage()
.then(result => {
zip.file(result.fileName, result.imageBlob);
completed++;
status.textContent = `Đã tải ${completed}/${count} ảnh`;
})
.catch(error => console.error('Lỗi:', error))
);

await Promise.all(promises);
retries++;
}
const promises = [];

if (completed < count) {
status.textContent = `Không thể tải đủ ${count} ảnh. Chỉ tải được ${completed} ảnh.`;
for (let i = 0; i < count; i++) {
if (promises.length >= CONCURRENT_LIMIT) {
await Promise.all(promises);
promises.length = 0;
}

const imagePromise = fetchImage().then(result => {
if (result && result.imageBlob) {
zip.file(result.fileName, result.imageBlob);
completed++;
status.textContent = `Đã tải ${completed}/${count} ảnh`;
}
}).catch(error => {
console.error('Lỗi khi tải ảnh:', error);
status.textContent = `Lỗi khi tải ảnh ${i + 1}. Đang tiếp tục...`;
});

promises.push(imagePromise);
}

await Promise.all(promises);
return zip.generateAsync({ type: 'blob' });
};

const downloadBtn = document.getElementById('downloadBtn');
const status = document.getElementById('status');
const imageCountInput = document.getElementById('imageCount');
const loadingAnimation = document.getElementById('loadingAnimation');

downloadBtn.addEventListener('click', async () => {
const imagesToDownload = parseInt(imageCountInput.value) || 5;
downloadBtn.disabled = true;
Expand All @@ -255,14 +298,11 @@ <h1>Tải ảnh NSFW</h1>

try {
const zipBlob = await downloadImages(imagesToDownload);
const randomZipName = `${Math.random().toString(36).substring(2, 10)}.zip`;

if (zipBlob) {
saveAs(zipBlob, randomZipName);
status.textContent = `Đã tải xong! File ZIP: ${randomZipName}.`;
}
const randomZipName = generateRandomString(10) + '.zip';
saveAs(zipBlob, randomZipName);
status.textContent = `Đã tải xong! Kiểm tra file ZIP (${randomZipName}) của bạn.`;
} catch (error) {
status.textContent = 'Đã xảy ra lỗi. Vui lòng thử lại.';
status.textContent = 'Đã xảy ra lỗi khi tải ảnh. Vui lòng thử lại sau.';
console.error('Lỗi:', error);
} finally {
downloadBtn.disabled = false;
Expand Down

0 comments on commit 3e6ec2f

Please sign in to comment.