Sure! Below is the complete HTML, CSS, and JavaScript code with the necessary adjustments to exclude the specified image (liuyifei.png) from being detected and included in the download process. This implementation ensures that any image with the filename liuyifei.png, regardless of its exact URL, will be excluded.
Key Changes:
-
Excluding Specific Image:
- Instead of checking for an exact URL, the code now checks if the image source ends with
liuyifei.png. This makes the exclusion more robust, especially if the replica code (--replicas/3t64n/ or similar) changes.
-
Enhanced Exclusion Logic:
- The exclusion condition is updated to use the
endsWith method to match the filename, ensuring that any image named liuyifei.png is excluded regardless of its directory structure.
Complete Code:
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<title>مبدل تصویر با هوش مصنوعی</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
height: 100vh;
background-color: #f0f0f0;
}
.iframe-container {
position: relative;
width: 100%;
height: 100vh;
overflow: hidden;
}
iframe {
width: 100%;
height: calc(100% + 1244px); /* اضافه کردن 33 سانتیمتر به پایین */
border: none;
display: none;
position: relative;
top: -583.15px; /* مخفی کردن 20.5 سانت از بالا */
}
.loading {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: flex;
flex-direction: column;
align-items: center;
z-index: 10000;
}
.loading-message {
margin-bottom: 20px;
font-size: 18px;
}
.progress {
width: 80%;
max-width: 400px;
height: 20px;
background-color: #ddd;
border-radius: 5px;
overflow: hidden;
}
.progress-bar {
height: 100%;
width: 0;
background-color: #4caf50;
transition: width 1s;
}
.error-message {
display: none;
color: red;
margin-top: 20px;
font-size: 16px;
text-align: center;
}
#retryButton {
display: none;
margin-top: 10px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #4caf50;
color: white;
border: none;
border-radius: 5px;
}
.download-button, .description-button {
display: block;
width: 90px;
color: white;
text-align: center;
padding: 6px;
border-radius: 5px;
cursor: pointer;
font-size: 11px;
transition: background-color 0.3s;
position: fixed;
top: 10px;
z-index: 10001;
text-decoration: none;
}
.download-button {
background-color: #4CAF50;
right: 10px;
}
.description-button {
background-color: rgb(84, 110, 122);
left: 10px;
}
.download-button:hover, .description-button:hover {
background-color: #45a049;
}
/* استایل پنجره سفارشی (modal) */
.modal {
display: none;
position: fixed;
z-index: 10002;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
}
.modal-content {
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
max-width: 400px;
text-align: center;
border-radius: 5px;
}
.modal-button {
margin-top: 20px;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
}
.cancel-btn {
background-color: #f44336;
color: white;
}
.translate-btn {
background-color: #4CAF50;
color: white;
}
.translate-btn:hover {
background-color: #45a049;
}
.image-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.image-item {
margin: 10px;
text-align: center;
}
.image-item img {
width: 80px;
height: 80px;
object-fit: cover;
border-radius: 5px;
margin-bottom: 5px;
}
.image-item button {
background-color: #4CAF50;
color: white;
padding: 5px 10px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 12px;
}
.image-item button:hover {
background-color: #45a049;
}
.download-button-bottom {
position: fixed;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
background-color: #4CAF50;
padding: 10px;
border-radius: 5px;
color: white;
text-align: center;
font-size: 14px;
text-decoration: none;
z-index: 10001;
cursor: pointer;
}
.download-button-bottom:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<!-- دکمههای دانلود و توضیحات -->
<p><a href="#" class="download-button" id="downloadButton">دانلود تصویر 📥</a></p>
<p><a href="#" class="description-button" id="descriptionButton">توضیحات</a></p>
<!-- پنجره سفارشی (modal) برای نمایش تصاویر دانلود -->
<div id="myModal" class="modal">
<div class="modal-content">
<p style="text-align: center;">تصاویر شناساییشده</p>
<div class="image-container" id="imageContainer"><!-- تصاویر کوچک در اینجا قرار خواهند گرفت --></div>
<button class="modal-button cancel-btn" id="closeBtn">بستن</button>
</div>
</div>
<!-- پنجره سفارشی (modal) برای نمایش توضیحات -->
<div id="myDescriptionModal" class="modal">
<div class="modal-content">
<hr />
<p><span style="color: #0000ff;">مبدل تصویر با هوش مصنوعی</span> </p>
<p>اولین مجموعه تبدیل تصویر در <span style="color: #33cccc;">برنامه هوش مصنوعی </span></p>
<p>تبدیل تصاویر به مدل های دلخواه.</p>
<p>تصویر خود را در کادر اول اضافه کنید </p>
<p>و در قسمت پرامپت به چیزی که میخواهید تبدیل بشه توضیحات رو به انگلیسی بنویسید.</p>
<p>چهار جایگاه برای اضافه کردن تصویر است که میتونید ‌ چند تصویر از خودتون قرار بدید تا چهره تونو بهتر شناسایی کنه.</p>
<button class="modal-button translate-btn" id="translateBtn">مترجم</button>
<button class="modal-button cancel-btn" id="closeDescriptionBtn">بستن</button>
</div>
</div>
<!-- لودینگ و iframe -->
<div class="iframe-container">
<div class="loading" id="loadingContainer">
<div class="loading-message">لطفاً صبر کنید...</div>
<div class="progress">
<div class="progress-bar" id="progressBar"></div>
</div>
<div class="error-message" id="errorMessage">بارگیری زیاد طول کشید.<br />در صورت عدم بارگذاری، فیلترشکن خود را روشن کنید.</div>
<button id="retryButton">بارگذاری مجدد</button>
</div>
<iframe id="myIframe" onload="iframeLoaded()" onerror="iframeError()"></iframe>
</div>
<!-- دکمه دانلود تصویر در پایین صفحه -->
<p><a href="#" class="download-button-bottom" id="downloadButtonBottom">دانلود تصویر 📥</a></p>
<script>
let progress = 0;
const progressBar = document.getElementById('progressBar');
const retryButton = document.getElementById('retryButton');
const errorMessage = document.getElementById('errorMessage');
const loadingContainer = document.getElementById('loadingContainer');
let interval;
let iframeLoadedFlag = false;
function startProgress() {
loadingContainer.style.display = 'flex';
progressBar.style.width = '0%';
progress = 0;
interval = setInterval(() => {
if (progress < 100) {
progress += 10;
progressBar.style.width = progress + '%';
}
if (progress === 100) {
clearInterval(interval);
if (!iframeLoadedFlag) {
retryButton.style.display = 'block';
errorMessage.style.display = 'block';
}
}
}, 1000);
}
function iframeLoaded() {
iframeLoadedFlag = true;
clearInterval(interval);
loadingContainer.style.display = 'none';
document.getElementById('myIframe').style.display = 'block';
}
function iframeError() {
clearInterval(interval);
loadingContainer.style.display = 'flex';
retryButton.style.display = 'block';
errorMessage.style.display = 'block';
}
retryButton.addEventListener('click', function() {
progress = 0;
progressBar.style.width = '0%';
iframeLoadedFlag = false;
retryButton.style.display = 'none';
errorMessage.style.display = 'none';
loadingContainer.style.display = 'flex';
document.getElementById('myIframe').style.display = 'none';
loadIframe();
startProgress();
});
function loadIframe() {
const iframe = document.getElementById('myIframe');
const urlParts = ['https:', '', 'yanze-pulid.hf.space', ''];
iframe.src = urlParts.join('/');
}
function handleDownload(event) {
event.preventDefault();
const iframe = document.getElementById('myIframe');
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
try {
const images = iframeDocument.querySelectorAll('img');
if (images.length === 0) {
alert("هیچ تصویری با فرمت PNG برای دانلود یافت نشد.");
return;
}
let pngImages = [];
images.forEach((img) => {
if (img.src.endsWith('.png') && !img.src.endsWith('liuyifei.png')) {
pngImages.push(img.src);
}
});
pngImages = pngImages.slice(0, 4);
if (pngImages.length === 0) {
alert("هیچ تصویر ساخته شدهای یافت نشد.");
return;
}
const imageContainer = document.getElementById('imageContainer');
imageContainer.innerHTML = '';
pngImages.forEach((imageUrl, index) => {
const imageItem = document.createElement('div');
imageItem.classList.add('image-item');
imageItem.innerHTML = `
<img src="${imageUrl}" alt="Image ${index + 1}">
<button onclick="downloadImage('${imageUrl}')">دانلود</button>
`;
imageContainer.appendChild(imageItem);
});
document.getElementById('myModal').style.display = 'block';
} catch (error) {
console.error("خطا در شناسایی تصاویر: ", error);
alert("خطایی در شناسایی تصاویر رخ داد.");
}
}
function downloadImage(imageUrl) {
window.open(imageUrl, '_blank');
}
document.getElementById('downloadButton').addEventListener('click', handleDownload);
document.getElementById('downloadButtonBottom').addEventListener('click', handleDownload);
const modal = document.getElementById('myModal');
const closeBtn = document.getElementById('closeBtn');
closeBtn.addEventListener('click', function() {
modal.style.display = 'none';
});
window.addEventListener('click', function(event) {
if (event.target == modal) {
modal.style.display = 'none';
}
if (event.target == descriptionModal) {
descriptionModal.style.display = 'none';
}
});
const descriptionModal = document.getElementById('myDescriptionModal');
const descriptionButton = document.getElementById('descriptionButton');
const closeDescriptionBtn = document.getElementById('closeDescriptionBtn');
const translateBtn = document.getElementById('translateBtn');
descriptionButton.addEventListener('click', function(event) {
event.preventDefault();
descriptionModal.style.display = 'block';
});
closeDescriptionBtn.addEventListener('click', function() {
descriptionModal.style.display = 'none';
});
translateBtn.addEventListener('click', function() {
window.open('https://translate.google.com/?hl=fa&sl=fa&tl=en&op=translate', '_blank');
});
loadIframe();
startProgress();
</script>
</body>
</html>
Explanation of Changes:
-
Excluding liuyifei.png:
if (img.src.endsWith('.png') && !img.src.endsWith('liuyifei.png')) {
pngImages.push(img.src);
}
- Before: The code was comparing the
img.src to an exact URL, which might not be reliable if the replica identifier changes.
- After: The updated condition checks if the image source ends with
.png and does not end with liuyifei.png. This ensures that any image named liuyifei.png is excluded, regardless of its directory or replica code.
-
Handling Multiple Click Events:
- The
window.onclick was previously defined twice, which can lead to only the last one being effective. It's updated to use window.addEventListener('click', function(event) { ... }) to handle multiple modal close actions within a single event listener.
-
HTML Structure:
- Wrapped the entire code within proper HTML structure, including
<!DOCTYPE html>, <html>, <head>, and <body> tags for better compatibility and structure.
-
Miscellaneous Improvements:
- Ensured consistent indentation and formatting for better readability.
- Added
lang="fa" in the <html> tag to specify the language as Persian.
- Removed unnecessary
<p> tags wrapping the <script> to prevent potential rendering issues.
Usage:
-
Download Button:
- Clicking on either the top-right or bottom download buttons will trigger the
handleDownload function.
- It fetches all PNG images from the iframe content, excludes any image named
liuyifei.png, and displays up to four downloadable images in a modal.
-
Description Button:
- Opens a modal with descriptions about the image converter tool.
- Includes a "Translate" button that redirects to Google Translate for translating the descriptions to English.
-
Iframe Loading:
- Displays a loading bar while the iframe content is loading.
- Shows an error message with a retry option if loading fails or takes too long.
Notes:
-
Cross-Origin Issues:
- Ensure that the iframe content (
yanze-pulid.hf.space) allows access to its DOM. If the iframe content is from a different origin and does not permit cross-origin access, accessing iframeDocument will result in a security error.
- To resolve this, the iframe content server must enable CORS and set appropriate headers to allow your domain to access its resources.
-
Dynamic Exclusions:
This complete and updated code should meet your requirements by effectively excluding the specified image and maintaining the intended functionality of your image downloader tool.