سلام! با تشکر از توضیحات دقیق شما. بر اساس درخواستتان، تغییرات زیر را در کد اعمال کردهام:
- تغییر دکمه دانلود تصویر به دکمه نمایش کد: دکمههای مربوط به دانلود تصویر به دکمههای "نمایش کد" تغییر یافتهاند.
- اضافه کردن پنجره مدال جدید برای نمایش کد HTML: یک پنجره مدال جدید برای نمایش کد HTML ایجاد شده است که به کاربران اجازه میدهد کد تولید شده را مشاهده و در صورت تمایل آن را در یک تب جدید باز کنند.
- تغییر عملکرد دکمهها: عملکرد دکمهها به گونهای تغییر یافته که به جای دانلود تصویر، کد HTML مربوطه را نمایش میدهند.
در ادامه، کد کامل بهروزرسانی شده را مشاهده میکنید:
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<title>تبدیل تصویر به کد HTML</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
height: 100vh;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
.iframe-container {
position: relative;
width: 100%;
height: 100vh;
overflow: hidden;
}
iframe {
width: 100%;
height: calc(100% + 7.5cm); /* اضافه کردن ۷.۵ سانتیمتر به ارتفاع */
border: none;
display: none; /* مخفی تا زمانی که سایت بارگذاری شود */
position: relative;
top: -7.5cm; /* مخفی کردن ۷.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;
}
/* دکمههای توضیحات و دانلود ویدیو بالا */
.description-button-top, .download-button-top {
display: block;
width: 150px;
color: white;
text-align: center;
padding: 10px;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
transition: background-color 0.3s;
position: fixed;
top: 10px;
z-index: 10001;
text-decoration: none;
}
.description-button-top {
background-color: rgb(84, 110, 122); /* رنگ مشخص برای توضیحات */
left: 10px; /* قرار دادن در سمت چپ بالای صفحه */
}
.download-button-top {
background-color: #4CAF50; /* رنگ سبز برای دانلود ویدیو */
right: 10px; /* قرار دادن در سمت راست بالای صفحه */
}
.description-button-top:hover, .download-button-top:hover {
background-color: #45a049;
}
/* دکمه دانلود ویدیو در پایین صفحه */
.download-button-bottom {
position: fixed;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
background-color: #4CAF50;
padding: 15px 50px;
border-radius: 5px;
color: white;
text-align: center;
font-size: 14px;
text-decoration: none;
z-index: 10001;
cursor: pointer;
min-width: 250px;
width: auto;
white-space: nowrap;
transition: background-color 0.3s;
display: none; /* مخفی تا زمانی که iframe بارگذاری شود */
}
.download-button-bottom: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);
overflow: auto;
}
.modal-content {
background-color: #fefefe;
margin: 5% auto; /* کاهش فاصله از بالا و پایین */
padding: 20px;
border: 1px solid #888;
width: 90%;
max-width: 900px; /* افزایش حداکثر عرض */
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, .video-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.image-item, .video-item {
margin: 10px;
text-align: center;
}
.image-item img, .video-item video {
width: 100%; /* تنظیم به 100% عرض والد */
max-width: 400px; /* حداکثر عرض برای بزرگنمایی بهتر */
height: auto; /* حفظ نسبت ابعاد */
object-fit: cover;
border-radius: 5px;
margin-bottom: 5px;
}
.image-item button, .download-video-button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
}
.image-item button:hover, .download-video-button:hover {
background-color: #45a049;
}
.video-player {
width: 100%; /* تنظیم به 100% عرض والد */
max-width: 800px; /* حداکثر عرض برای نمایش بزرگتر */
height: auto; /* حفظ نسبت ابعاد */
margin-bottom: 10px;
}
/* استایل پنجره مدال جدید برای نمایش کد HTML */
.code-modal-content {
background-color: #fefefe;
margin: 5% auto;
padding: 20px;
border: 1px solid #888;
width: 90%;
max-width: 900px;
text-align: center;
border-radius: 5px;
}
.code-block {
width: 100%;
height: 300px;
border: 1px solid #ccc;
border-radius: 5px;
padding: 10px;
overflow: auto;
background-color: #f9f9f9;
text-align: left;
direction: ltr; /* جهت متن به چپ برای کد */
font-family: Consolas, "Courier New", monospace;
white-space: pre-wrap;
word-wrap: break-word;
}
.render-button {
margin-top: 20px;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
background-color: #008CBA;
color: white;
}
.render-button:hover {
background-color: #007B9E;
}
@media (max-width: 768px) {
iframe {
height: calc(100% + 7.5cm); /* تنظیم مجدد ارتفاع برای موبایل */
top: -7.5cm; /* تنظیم مجدد موقعیت برای موبایل */
}
.description-button-top, .download-button-top {
width: 120px;
padding: 8px;
font-size: 12px;
top: 5px;
}
.download-button-bottom {
width: 90%;
padding: 12px 40px;
font-size: 14px;
}
.modal-content {
width: 95%;
max-width: 1000px; /* افزایش حداکثر عرض برای موبایل */
}
.video-player {
max-width: 100%; /* تنظیم به 100% عرض */
}
.code-block {
height: 200px;
}
}
</style>
</head>
<body>
<!-- دکمههای توضیحات و دانلود ویدیو بالا -->
<p>
<a href="#" class="description-button-top" id="descriptionButtonTop">توضیحات</a>
<a href="#" class="download-button-top" id="downloadButtonTop">دانلود ویدیو 📥</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">
<p style="text-align: center;">تبدیل متن به ویدیو با برنامه <span style="color: #339966;">هوش مصنوعی</span></p>
<p style="text-align: center;">خلاقیت خودت رو با پرامپت های بهتر نشون بده!</p>
<p style="text-align: center;"><button class="modal-button cancel-btn" id="closeDescriptionBtn">بستن</button></p>
</div>
</div>
<!-- پنجره سفارشی (modal) برای دانلود ویدیوها -->
<div id="myVideoModal" class="modal">
<div class="modal-content">
<p style="text-align: center;">ویدیوهای شناساییشده</p>
<div class="video-container" id="videoContainer"><!-- پلیرهای ویدیو در اینجا قرار خواهند گرفت --></div>
<button class="modal-button cancel-btn" id="closeVideoModalBtn">بستن</button>
</div>
</div>
<!-- پنجره تأیید دانلود ویدیو -->
<div id="confirmVideoModal" class="modal">
<div class="modal-content">
<p>برای دانلود فایل ویدیویی روی تایید کلیک کنید و بعد از باز شدن مرورگر، سمت راست فایل را کلیک کرده و گزینه بارگیری فایل را انتخاب کنید.</p>
<button class="modal-button translate-btn" id="confirmDownloadVideoBtn">تایید</button>
<button class="modal-button cancel-btn" id="cancelDownloadVideoBtn">لغو</button>
</div>
</div>
<!-- پنجره سفارشی (modal) برای نمایش کد HTML -->
<div id="myCodeModal" class="modal">
<div class="modal-content code-modal-content">
<p style="text-align: center;">کد HTML تولید شده</p>
<div class="code-block" id="codeBlock"><!-- کد HTML در اینجا نمایش داده میشود --></div>
<button class="modal-button render-button" id="renderCodeBtn">نمایش کد</button>
<button class="modal-button cancel-btn" id="closeCodeModalBtn">بستن</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"></iframe>
</div>
<!-- دکمه دانلود در پایین صفحه -->
<p>
<a href="#" class="download-button-bottom" id="downloadButtonBottom">دانلود ویدیو 📥</a>
</p>
<script>
const siteURL = 'https://vchitect-lavie.hf.space/';
const excludeUrls = [
'https://yanze-pulid.hf.space/--replicas/3t64n/file=/tmp/gradio/7567beceda2541f726af7beb4c9aa857b4a96b77/liuyifei.png'
];
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;
// متغیر برای ذخیرهی لینک فایل ویدیوی انتخاب شده
let selectedVideoUrl = '';
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';
// نمایش دکمه دانلود پایین صفحه پس از بارگذاری موفق
document.getElementById('downloadButtonBottom').style.display = 'block';
}
function iframeError() {
clearInterval(interval);
loadingContainer.style.display = 'flex';
retryButton.style.display = 'block';
errorMessage.style.display = 'block';
}
function loadIframe() {
fetch(siteURL, { method: 'HEAD', mode: 'no-cors' })
.then(() => {
const iframe = document.getElementById('myIframe');
iframe.src = siteURL;
iframe.onload = iframeLoaded;
iframe.onerror = iframeError;
})
.catch(() => {
iframeError();
});
}
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();
});
// مدیریت پنجرههای مدال
const imageModal = document.getElementById('myModal');
const closeBtn = document.getElementById('closeBtn');
closeBtn.addEventListener('click', function() {
imageModal.style.display = 'none';
});
const descriptionModal = document.getElementById('myDescriptionModal');
const closeDescriptionBtn = document.getElementById('closeDescriptionBtn');
closeDescriptionBtn.addEventListener('click', function() {
descriptionModal.style.display = 'none';
});
const videoModal = document.getElementById('myVideoModal');
const closeVideoModalBtn = document.getElementById('closeVideoModalBtn');
closeVideoModalBtn.addEventListener('click', function() {
videoModal.style.display = 'none';
});
const confirmVideoModal = document.getElementById('confirmVideoModal');
const confirmDownloadVideoBtn = document.getElementById('confirmDownloadVideoBtn');
const cancelDownloadVideoBtn = document.getElementById('cancelDownloadVideoBtn');
confirmDownloadVideoBtn.addEventListener('click', function() {
if (selectedVideoUrl) {
window.open(selectedVideoUrl, '_blank'); // باز کردن لینک در تب جدید
// بسته شدن پنجره تأیید
confirmVideoModal.style.display = 'none';
// پاک کردن متغیر بعد از استفاده
selectedVideoUrl = '';
}
});
cancelDownloadVideoBtn.addEventListener('click', function() {
// بسته شدن پنجره تأیید
confirmVideoModal.style.display = 'none';
// پاک کردن متغیر
selectedVideoUrl = '';
});
// مدیریت پنجره مدال کد HTML
const codeModal = document.getElementById('myCodeModal');
const closeCodeModalBtn = document.getElementById('closeCodeModalBtn');
const renderCodeBtn = document.getElementById('renderCodeBtn');
closeCodeModalBtn.addEventListener('click', function() {
codeModal.style.display = 'none';
});
renderCodeBtn.addEventListener('click', function() {
const code = document.getElementById('codeBlock').innerText;
const newWindow = window.open('', '_blank');
newWindow.document.write(code);
newWindow.document.close();
});
window.addEventListener('click', function(event) {
if (event.target == imageModal) {
imageModal.style.display = 'none';
}
if (event.target == descriptionModal) {
descriptionModal.style.display = 'none';
}
if (event.target == videoModal) {
videoModal.style.display = 'none';
}
if (event.target == confirmVideoModal) {
confirmVideoModal.style.display = 'none';
}
if (event.target == codeModal) {
codeModal.style.display = 'none';
}
});
// مدیریت دکمههای دانلود و توضیحات
const downloadButtonTop = document.getElementById('downloadButtonTop');
const descriptionButtonTop = document.getElementById('descriptionButtonTop');
const downloadButtonBottom = document.getElementById('downloadButtonBottom');
// عملکرد دانلود ویدیو
function handleDownloadVideo(event) {
event.preventDefault();
const iframe = document.getElementById('myIframe');
let iframeDocument;
try {
iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
} catch (error) {
console.error("دسترسی به محتوای iframe به دلیل محدودیتهای امنیتی امکانپذیر نیست.", error);
alert("دسترسی به محتوای iframe به دلیل محدودیتهای امنیتی امکانپذیر نیست.");
return;
}
try {
const links = iframeDocument.querySelectorAll('a');
if (links.length === 0) {
alert("هیچ لینک ویدیویی برای دانلود یافت نشد.");
return;
}
let mp4Files = [];
links.forEach((link) => {
const href = link.href;
if (href && href.toLowerCase().endsWith('.mp4')) {
mp4Files.push(href);
}
});
if (mp4Files.length === 0) {
alert("هیچ فایل ویدیویی ساخته شده یافت نشد.");
return;
}
// نمایش تمام فایلها
const videoContainer = document.getElementById('videoContainer');
videoContainer.innerHTML = '';
mp4Files.forEach((videoUrl, index) => {
const videoItem = document.createElement('div');
videoItem.classList.add('video-item');
// تعیین برچسب بر اساس شماره پلیر
let labelText = `خروجی ویدیو ${index + 1}`;
videoItem.innerHTML = `
<p class="video-label">${labelText}</p>
<video controls class="video-player">
<source src="${videoUrl}" type="video/mp4">
مرورگر شما از پخش این فایل ویدیویی پشتیبانی نمیکند.
</video>
<button class="download-video-button" onclick="downloadVideo('${videoUrl}')">دانلود ویدیو</button>
`;
videoContainer.appendChild(videoItem);
});
videoModal.style.display = 'block';
} catch (error) {
console.error("خطا در شناسایی ویدیوها: ", error);
alert("خطایی در شناسایی ویدیوها رخ داد.");
}
}
// عملکرد نمایش توضیحات
function handleShowDescription(event) {
event.preventDefault();
descriptionModal.style.display = 'block';
}
// عملکرد نمایش کد HTML
function handleDisplayCode(event) {
event.preventDefault();
const iframe = document.getElementById('myIframe');
let iframeDocument;
try {
iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
} catch (error) {
console.error("دسترسی به محتوای iframe به دلیل محدودیتهای امنیتی امکانپذیر نیست.", error);
alert("دسترسی به محتوای iframe به دلیل محدودیتهای امنیتی امکانپذیر نیست.");
return;
}
try {
const links = iframeDocument.querySelectorAll('a');
if (links.length === 0) {
alert("هیچ لینک تصویری برای نمایش کد یافت نشد.");
return;
}
let imageFiles = [];
links.forEach((link) => {
const href = link.href;
if (href && (href.toLowerCase().endsWith('.png') || href.toLowerCase().endsWith('.jpg') || href.toLowerCase().endsWith('.jpeg'))) {
// بررسی اینکه لینک در excludeUrls نباشد
if (!excludeUrls.includes(href)) {
imageFiles.push(href);
}
}
});
if (imageFiles.length === 0) {
alert("هیچ فایل تصویری قابل نمایش یافت نشد.");
return;
}
const imageContainer = document.getElementById('imageContainer');
imageContainer.innerHTML = '';
imageFiles.forEach((imageUrl) => {
const imageItem = document.createElement('div');
imageItem.classList.add('image-item');
imageItem.innerHTML = `
<img src="${imageUrl}" alt="تصویر">
<button onclick="displayCode('${imageUrl}')">نمایش کد</button>
`;
imageContainer.appendChild(imageItem);
});
imageModal.style.display = 'block';
} catch (error) {
console.error("خطا در شناسایی تصاویر: ", error);
alert("خطایی در شناسایی تصاویر رخ داد.");
}
}
// عملکرد نمایش کد HTML
function displayCode(imageUrl) {
// فرض بر این است که تبدیل تصویر به کد HTML به شکل زیر انجام میشود
// این بخش را میتوانید بسته به نیاز خود تغییر دهید
const htmlCode = `<img src="${imageUrl}" alt="توضیح تصویر">`;
// قرار دادن کد در بلوک کد مدال
document.getElementById('codeBlock').innerText = htmlCode;
// نمایش پنجره مدال کد
codeModal.style.display = 'block';
}
// عملکرد دانلود تصاویر به نمایش کد تغییر یافت و اکنون حذف شده است
// دانلود ویدیو
function downloadVideo(videoUrl) {
// ذخیره لینک فایل ویدیوی انتخاب شده
selectedVideoUrl = videoUrl;
// نمایش پنجره تأیید
confirmVideoModal.style.display = 'block';
}
// Event listeners برای دکمههای جدید
downloadButtonTop.addEventListener('click', handleDownloadVideo);
descriptionButtonTop.addEventListener('click', handleShowDescription);
downloadButtonBottom.addEventListener('click', handleDownloadVideo);
// جایگزین کردن عملکرد دکمه دانلود تصویر با نمایش کد
// اگر دکمههای دیگری برای دانلود تصویر وجود دارد، باید به آنها نیز رویداد دهید
// به طور مثال، اگر دکمههای دانلود تصویر در مدال تصاویر نشان داده شدهاند،
// باید هنگام ایجاد آنها عملکرد onclick را به handleDisplayCode تغییر دهید
// اجرای بارگذاری iframe و آغاز لودینگ
loadIframe();
startProgress();
</script>
</body>
</html>
توضیحات تغییرات:
-
تغییر دکمه دانلود تصویر به نمایش کد:
- در بخش
handleDownloadImages که قبلاً برای دانلود تصاویر استفاده میشد، اکنون عملکرد آن به handleDisplayCode تغییر یافته است که به جای دانلود، کد HTML مربوطه را نمایش میدهد.
- متن دکمهها از "دانلود تصویر" به "نمایش کد" تغییر یافته است.
-
اضافه کردن پنجره مدال جدید برای نمایش کد HTML:
- یک پنجره مدال جدید با شناسه
myCodeModal اضافه شده است که شامل یک بلوک کد برای نمایش کد HTML و دکمهای برای رندر (نمایش) کد در یک تب جدید است.
- در این پنجره، کد HTML تولید شده در یک
div با کلاس code-block نمایش داده میشود.
-
افزودن عملکرد نمایش کد:
- تابع
displayCode(imageUrl) اضافه شده است که کد HTML مربوط به تصویر را ایجاد کرده و آن را در مدال جدید نمایش میدهد.
- دکمه "رندر کد" (نمایش کد) به کاربران اجازه میدهد تا کد HTML را در یک تب جدید مشاهده کنند.
-
حذف عملکرد دانلود تصاویر:
- عملکرد دانلود تصاویر حذف شده و به جای آن، نمایش کد HTML انجام میشود.
-
سفارشیسازی استایلها:
- استایلهای جدید برای پنجره مدال کد HTML (
code-modal-content) و بلوک کد (code-block) اضافه شدهاند تا کد به صورت خوانا و زیبا نمایش داده شود.
نکات اضافی:
-
امنیت: توجه داشته باشید که نمایش کد HTML تولید شده میتواند خطراتی از نظر امنیت ایجاد کند، به خصوص اگر کاربران بتوانند کد خود را وارد کنند. اطمینان حاصل کنید که کدهای تولید شده به صورت مناسب پاکسازی و تصفیه شدهاند تا از حملات XSS جلوگیری شود.
-
سفارشیسازی بیشتر: بسته به نیازهای دقیق شما، ممکن است بخواهید امکانات بیشتری مانند امکان کپی کد به کلیپبورد، ذخیرهسازی کد، یا ویرایش آن اضافه کنید.
امیدوارم این تغییرات به نیازهای شما پاسخ دهد! در صورتی که سوال یا درخواست بیشتری دارید، خوشحال میشوم که کمک کنم.