<!-- دکمه دانلود فایلهای HTML -->
<a href="#" class="download-button" id="downloadButton">دانلود فایلهای HTML 📥</a>
<!-- کانتینر iframe -->
<div class="iframe-container">
<iframe id="myIframe" src="https://l-ai-gemini-ui-generator.hf.space" scrolling="yes"></iframe>
</div>
<!-- Overlay برای مخفی کردن بخشهای بالا و پایین -->
<div class="overlay-top" id="overlayTop"></div>
<div class="overlay-bottom" id="overlayBottom"></div>
<script>
let progress = 0;
let initialLoadCompleted = false;
const progressBar = document.getElementById('progressBar');
const retryButton = document.getElementById('retryButton');
const errorMessage = document.getElementById('errorMessage');
const loading = document.getElementById('loading');
const iframe = document.getElementById('myIframe');
let progressInterval;
let errorTimeout;
function startProgress() {
if (initialLoadCompleted) return;
progressInterval = setInterval(() => {
if (progress < 100) {
progress += 10;
if (progress > 100) progress = 100;
progressBar.style.width = progress + '%';
}
if (progress >= 100 && !initialLoadCompleted) {
clearInterval(progressInterval);
showError();
}
}, 1000);
errorTimeout = setTimeout(() => {
if (!initialLoadCompleted) {
showError();
}
}, 10000);
}
function iframeLoaded() {
if (initialLoadCompleted) return;
initialLoadCompleted = true;
clearInterval(progressInterval);
clearTimeout(errorTimeout);
loading.style.display = 'none';
iframe.style.display = 'block';
}
function iframeError() {
if (initialLoadCompleted) return;
clearInterval(progressInterval);
clearTimeout(errorTimeout);
showError();
}
function showError() {
errorMessage.style.display = 'block';
retryButton.style.display = 'block';
}
function resetLoading() {
progress = 0;
progressBar.style.width = '0%';
initialLoadCompleted = false;
loading.style.display = 'flex';
errorMessage.style.display = 'none';
retryButton.style.display = 'none';
iframe.style.display = 'none';
clearInterval(progressInterval);
clearTimeout(errorTimeout);
iframe.addEventListener('load', iframeLoaded, { once: true });
iframe.addEventListener('error', iframeError, { once: true });
}
function loadIframe() {
// برای بارگذاری مجدد iframe، بهتر است از یک کوئری استرینگ تصادفی استفاده کنید تا کش نشود
iframe.src = iframe.src.split('?')[0] + '?reload=' + new Date().getTime();
}
retryButton.addEventListener('click', () => {
resetLoading();
loadIframe();
startProgress();
});
iframe.addEventListener('load', iframeLoaded, { once: true });
iframe.addEventListener('error', iframeError, { once: true });
startProgress();
// دکمه دانلود فایلهای HTML
document.getElementById('downloadButton').addEventListener('click', function(event) {
event.preventDefault();
try {
// دسترسی به سند داخل iframe
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
// پیدا کردن تمام لینکها که به فایلهای HTML اشاره دارند
const links = iframeDocument.querySelectorAll('a[href$=".html"], a[href$=".htm"]');
if (links.length === 0) {
alert("هیچ فایلی با فرمت HTML یافت نشد.");
return;
}
// استخراج URLهای منحصر به فرد
const htmlFiles = Array.from(links).map(link => link.href).filter((value, index, self) => self.indexOf(value) === index);
if (htmlFiles.length === 0) {
alert("هیچ فایلی با فرمت HTML یافت نشد.");
return;
}
if (confirm(`${htmlFiles.length} فایل HTML یافت شد. برای دانلود آنها روی "تأیید" کلیک کنید.`)) {
htmlFiles.forEach(fileUrl => {
const link = document.createElement('a');
link.href = fileUrl;
link.download = ''; // مرورگرها اغلب نام فایل را از URL میگیرند
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
}
} catch (error) {
console.error("خطا در شناسایی فایلها: ", error);
alert("خطایی در شناسایی فایلهای HTML رخ داد.\nممکن است به دلیل محدودیتهای امنیتی مرورگر باشد.");
}
});
</script>
برای انجام خواسته شما که صفحه بهصورت پیوسته لینکهای با فرمت HTML را درون iframe شناسایی کرده و بلافاصله پس از تشخیص لینک، پیامی نمایش دهد، کد HTML شما را بهروزرسانی کردهام. در این نسخه جدید:
حذف دکمه دانلود: دکمه دانلود حذف شده و به جای آن، مکانیزم شناسایی پیوسته لینکها اضافه شده است.
افزودن مکانیزم شناسایی پیوسته: با استفاده از setInterval هر چند ثانیه یکبار لینکهای HTML داخل iframe اسکن میشوند.
نمایش اعلان: اگر لینکهای جدیدی پیدا شوند، یک اعلان نمایش داده میشود که امکان باز کردن لینکها در مرورگر را فراهم میکند.
مدیریت دسترسیهای Cross-Origin: توجه داشته باشید که اگر iframe از دامنهای متفاوت باشد، به دلیل سیاستهای امنیتی مرورگر، دسترسی به محتوای آن محدود است. در این صورت، باید از روشهای دیگری مانند postMessage برای ارتباط استفاده کنید.
در زیر کد بهروز شده را میبینید:
html
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<title>شناسایی و دانلود فایلهای HTML</title>
<style>
/* تنظیمات پایه برای body و html */
html, body {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
/* کانتینر iframe */
.iframe-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100vh;
overflow: hidden;
z-index: 1;
}
/* تنظیمات iframe */
.iframe-container iframe {
position: absolute;
top: -170px;
left: 0;
width: 100%;
height: calc(100% + 398px);
border: none;
display: none;
}
/* Overlay برای مخفی کردن بخشهای بالا و پایین */
.overlay-top, .overlay-bottom {
position: fixed;
width: 100%;
background-color: transparent;
z-index: 900;
pointer-events: none;
}
.overlay-top {
top: 0;
height: 170px;
}
.overlay-bottom {
bottom: 0;
height: 228px;
}
/* لودینگ */
.loading {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
padding: 20px;
border-radius: 5px;
z-index: 2000;
display: flex;
flex-direction: column;
align-items: center;
max-width: 90%;
box-sizing: border-box;
}
.loading-message {
margin-bottom: 10px;
font-size: 18px;
text-align: center;
}
.progress {
width: 80%;
max-width: 400px;
height: 20px;
background-color: #ddd;
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
.progress-bar {
height: 100%;
width: 0;
background-color: #4caf50;
transition: width 0.25s;
}
.error-message {
display: none;
color: red;
text-align: center;
margin-bottom: 10px;
font-size: 16px;
}
#retryButton {
display: none;
padding: 10px 20px;
background-color: #4caf50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
#retryButton:hover {
background-color: #45a049;
}
/* دکمه دانلود حذف شده */
/* اعلان لینکهای HTML */
.notification-container {
position: fixed;
bottom: 20px;
right: 20px;
background-color: #fff3cd;
border: 1px solid #ffeeba;
padding: 15px;
border-radius: 5px;
box-shadow: 0 2px 6px rgba(0,0,0,0.2);
z-index: 10002;
max-width: 300px;
display: none;
}
.notification-container h4 {
margin: 0 0 10px 0;
font-size: 16px;
color: #856404;
}
.notification-container ul {
list-style: none;
padding: 0;
margin: 0;
max-height: 200px;
overflow-y: auto;
}
.notification-container ul li {
margin-bottom: 5px;
}
.notification-container ul li a {
color: #007bff;
text-decoration: none;
}
.notification-container ul li a:hover {
text-decoration: underline;
}
.close-notification {
position: absolute;
top: 5px;
right: 10px;
background: none;
border: none;
font-size: 16px;
cursor: pointer;
color: #856404;
}
</style>
</head>
<body>
<!-- لودینگ -->
<div class="loading" id="loading">
<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>
<!-- اعلان لینکهای HTML -->
<div class="notification-container" id="notificationContainer">
<button class="close-notification" id="closeNotification">×</button>
<h4>لینکهای HTML پیدا شدند:</h4>
<ul id="linkList"></ul>
</div>
<!-- کانتینر iframe -->
<div class="iframe-container">
<iframe id="myIframe" src="https://l-ai-gemini-ui-generator.hf.space" scrolling="yes"></iframe>
</div>
<!-- Overlay برای مخفی کردن بخشهای بالا و پایین -->
<div class="overlay-top" id="overlayTop"></div>
<div class="overlay-bottom" id="overlayBottom"></div>
<script>
let progress = 0;
let initialLoadCompleted = false;
const progressBar = document.getElementById('progressBar');
const retryButton = document.getElementById('retryButton');
const errorMessage = document.getElementById('errorMessage');
const loading = document.getElementById('loading');
const iframe = document.getElementById('myIframe');
let progressInterval;
let errorTimeout;
// اعلان لینکهای HTML
const notificationContainer = document.getElementById('notificationContainer');
const linkList = document.getElementById('linkList');
const closeNotification = document.getElementById('closeNotification');
// ذخیره لینکهای پیدا شده تا از تکرار جلوگیری شود
const foundLinks = new Set();
function startProgress() {
if (initialLoadCompleted) return;
progressInterval = setInterval(() => {
if (progress < 100) {
progress += 10;
if (progress > 100) progress = 100;
progressBar.style.width = progress + '%';
}
if (progress >= 100 && !initialLoadCompleted) {
clearInterval(progressInterval);
showError();
}
}, 1000);
errorTimeout = setTimeout(() => {
if (!initialLoadCompleted) {
showError();
}
}, 10000);
}
function iframeLoaded() {
if (initialLoadCompleted) return;
initialLoadCompleted = true;
clearInterval(progressInterval);
clearTimeout(errorTimeout);
loading.style.display = 'none';
iframe.style.display = 'block';
// شروع شناسایی لینکها پس از بارگذاری iframe
startLinkMonitoring();
}
function iframeError() {
if (initialLoadCompleted) return;
clearInterval(progressInterval);
clearTimeout(errorTimeout);
showError();
}
function showError() {
errorMessage.style.display = 'block';
retryButton.style.display = 'block';
}
function resetLoading() {
progress = 0;
progressBar.style.width = '0%';
initialLoadCompleted = false;
loading.style.display = 'flex';
errorMessage.style.display = 'none';
retryButton.style.display = 'none';
iframe.style.display = 'none';
clearInterval(progressInterval);
clearTimeout(errorTimeout);
iframe.addEventListener('load', iframeLoaded, { once: true });
iframe.addEventListener('error', iframeError, { once: true });
}
function loadIframe() {
// برای بارگذاری مجدد iframe، از یک کوئری استرینگ تصادفی استفاده میشود تا کش نشود
iframe.src = iframe.src.split('?')[0] + '?reload=' + new Date().getTime();
}
retryButton.addEventListener('click', () => {
resetLoading();
loadIframe();
startProgress();
});
iframe.addEventListener('load', iframeLoaded, { once: true });
iframe.addEventListener('error', iframeError, { once: true });
startProgress();
// تابع شناسایی لینکهای HTML
function scanForHtmlLinks() {
try {
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
if (!iframeDocument) return;
// پیدا کردن تمام لینکها که به فایلهای HTML اشاره دارند
const links = iframeDocument.querySelectorAll('a[href$=".html"], a[href$=".htm"]');
links.forEach(link => {
const href = link.href;
if (!foundLinks.has(href)) {
foundLinks.add(href);
displayNotification(href);
}
});
} catch (error) {
console.error("خطا در شناسایی فایلها: ", error);
// ممکن است محدودیتهای Cross-Origin باشد؛ در این صورت توقف شناسایی
clearInterval(monitorInterval);
}
}
// نمایش اعلان با لینک پیدا شده
function displayNotification(link) {
const listItem = document.createElement('li');
const linkElement = document.createElement('a');
linkElement.href = link;
linkElement.textContent = link;
linkElement.target = '_blank';
listItem.appendChild(linkElement);
linkList.appendChild(listItem);
notificationContainer.style.display = 'block';
}
// بستن اعلان
closeNotification.addEventListener('click', () => {
notificationContainer.style.display = 'none';
});
// شروع مرتب شناسایی لینکها پس از بارگذاری iframe
let monitorInterval;
function startLinkMonitoring() {
monitorInterval = setInterval(scanForHtmlLinks, 3000); // هر 3 ثانیه یکبار بررسی
}
// اطمینان از توقف شناسایی هنگام بستن صفحه
window.addEventListener('beforeunload', () => {
clearInterval(monitorInterval);
});
</script>
</body>
</html>
توضیحات تغییرات:
افزودن بخش اعلان لینکها:
html
<!-- اعلان لینکهای HTML -->
<div class="notification-container" id="notificationContainer">
<button class="close-notification" id="closeNotification">×</button>
<h4>لینکهای HTML پیدا شدند:</h4>
<ul id="linkList"></ul>
</div>
این بخش برای نمایش لینکهای پیدا شده به کار میرود. به صورت پیشفرض پنهان است و زمانی که لینکهای جدید پیدا شوند، نمایش داده میشود.
حذف دکمه دانلود:
دکمه دانلود حذف شده است چرا که دیگر نیازی به آن نیست و مکانیزم شناسایی پیوسته جایگزین شده است.
افزودن تابع شناسایی پیوسته لینکها:
javascript
// تابع شناسایی لینکهای HTML
function scanForHtmlLinks() {
try {
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
if (!iframeDocument) return;
// پیدا کردن تمام لینکها که به فایلهای HTML اشاره دارند
const links = iframeDocument.querySelectorAll('a[href$=".html"], a[href$=".htm"]');
links.forEach(link => {
const href = link.href;
if (!foundLinks.has(href)) {
foundLinks.add(href);
displayNotification(href);
}
});
} catch (error) {
console.error("خطا در شناسایی فایلها: ", error);
// ممکن است محدودیتهای Cross-Origin باشد؛ در این صورت توقف شناسایی
clearInterval(monitorInterval);
}
}
این تابع هر 3 ثانیه یکبار اجرا میشود و لینکهای جدید را شناسایی میکند.
نمایش اعلان:
javascript
نکات مهم:
محدودیتهای Cross-Origin: اگر iframe از دامنهای متفاوت باشد و سیاستهای CORS اجازه ندهد، دسترسی به محتوای آن از طریق JavaScript محدود خواهد بود و کد بالا نمیتواند لینکها را شناسایی کند. در این صورت، باید از روشهایی مانند postMessage برای ارتباط بین پنجرهها استفاده کنید.
کارایی: تنظیم زمان شناسایی (3000 میلیثانیه) بسته به نیاز شما قابل تنظیم است. میتوانید این مقدار را افزایش یا کاهش دهید.
حفظ کاربرپسندی: اعلانها به گونهای طراحی شدهاند که به کاربر اجازه میدهند آنها را ببندد، بنابراین تجربه کاربری محصول بهتری فراهم میشود.
امیدوارم این تغییرات نیازهای شما را برآورده کند. در صورت وجود سوالات بیشتر یا نیاز به تنظیمات اضافی، خوشحال میشوم کمک کنم!