// تابعی برای پیدا کردن نودهای متنی که شامل متن مشخصی هستند
function getTextNodesContaining(node, text) {
let textNodes = [];
if (node.nodeType === Node.TEXT_NODE && node.nodeValue.includes(text)) {
textNodes.push(node);
} else {
node.childNodes.forEach(child => {
textNodes = textNodes.concat(getTextNodesContaining(child, text));
});
}
return textNodes;
}
// تنظیمات و عملکرد
(function() {
const siteURL = 'https://fffiloni-pasd.hf.space/'; // آدرس iframe
let progress = 0;
const progressBar = document.getElementById('progressBar');
const retryButton = document.getElementById('retryButton');
const errorMessage = document.getElementById('errorMessage');
const loadingContainer = document.getElementById('loadingContainer');
const iframe = document.getElementById('myIframe');
const scrollHandle = document.getElementById('scrollHandle');
let interval;
let iframeLoadedFlag = false;
// متونی که باید مخفی شوند
const unwantedLinks = [
'https://gradio.app/',
'https://huggingface.co/spaces/fffiloni/PASD?duplicate=true',
'https://github.com/yangxy/PASD',
'https://huggingface.co/papers/2308.14469'
];
// متونی که باید مخفی شوند
const unwantedTexts = [
"Pixel-Aware Stable Diffusion for Realistic Image Super-resolution and Personalized Stylization",
"Made with Gradio",
"ساخته شده با gradio"
];
// متونی که باید جایگزین شوند
const textsToReplace = {
"PASD Magnify": "افزایش کیفیت تصاویر با برنامه هوش مصنوعی",
"Submit": "ساخت",
"Advanced settings": "تنظیمات پیشرفته",
"Prompt": "پرامپت (پرامپت رو به زبان انگلیسی وارد کنید)",
"You have exceeded your GPU quota": "محدودیت جی پی یو: انگار با ربات زیاد کار کردی! این محدودیت ۱۲ ساعت بعد برداشته میشه، اما اگه میخوای این محدودیت برداشته بشه امکانش هست. طبق ویدیو آموزشی داخل برنامه فیلترشکن پور لایت یک بار خاموش کن و دوباره روشن کن این محدودیت برداشته میشه.",
"Added": "چه چیزی اعمال بشه",
"Negative": "چه چیزی اعمال نشه",
"On mobile, the connection can break if this tab is unfocused or the device sleeps, losing your position in queue.": "اجرای دستور کاربر برای افزایش کیفیت عکس با برنامه هوش مصنوعی",
"Error": "خسته شدم🥺 میخوای دوباره بسازم؟ مشکلی نیست آیپی عوض کن",
"error": "خسته شدم🥺 میخوای دوباره بسازم؟ مشکلی نیست آیپی عوض کن"
};
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';
iframe.style.display = 'block';
scrollHandle.style.display = 'flex';
try {
hideUnwantedElements();
setupElementHiding();
} catch (e) {
console.error('Cannot access iframe content:', e);
}
}
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';
iframe.style.display = 'none';
loadIframe();
startProgress();
});
function loadIframe() {
fetch(siteURL, { method: 'HEAD', mode: 'no-cors' })
.then(() => {
iframe.src = siteURL;
iframe.onload = iframeLoaded;
iframe.onerror = iframeError;
})
.catch(() => {
iframeError();
});
}
function hideUnwantedElements() {
try {
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
if (!iframeDocument) return;
// مخفی کردن لینکهای ناخواسته و تصاویر داخل آنها
unwantedLinks.forEach(url => {
const links = iframeDocument.querySelectorAll(`a[href="${url}"]`);
links.forEach(link => {
link.style.display = 'none';
// مخفی کردن تصاویر داخل لینک
const images = link.querySelectorAll('img');
images.forEach(img => {
img.style.display = 'none';
});
});
});
// مخفی کردن تصاویر با src مشخصشده
const images = iframeDocument.querySelectorAll('img');
images.forEach(img => {
const src = img.src;
if (unwantedLinks.includes(src)) {
img.style.display = 'none';
}
});
// مخفی کردن متون مشخصشده
unwantedTexts.forEach(unwanted => {
const textNodes = getTextNodesContaining(iframeDocument.body, unwanted);
textNodes.forEach(node => {
const parentElement = node.parentElement;
if (parentElement) {
parentElement.style.display = 'none';
}
});
});
// جایگزینی متون مشخصشده
Object.keys(textsToReplace).forEach(originalText => {
const newText = textsToReplace[originalText];
const textNodes = getTextNodesContaining(iframeDocument.body, originalText);
textNodes.forEach(node => {
node.textContent = node.textContent.replace(new RegExp(escapeRegExp(originalText), 'gi'), newText);
});
});
} catch (error) {
console.error("Cannot access iframe content: ", error);
}
}
function setupElementHiding() {
try {
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
if (!iframeDocument) return;
hideUnwantedElements();
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes.length) {
hideUnwantedElements();
}
});
});
observer.observe(iframeDocument.body, {
childList: true,
subtree: true
});
} catch (error) {
console.error("Cannot access iframe content: ", error);
}
}
function handleDownload(event) {
event.preventDefault();
try {
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
const images = iframeDocument.querySelectorAll('img');
if (images.length === 0) {
alert("هیچ تصویری برای دانلود یافت نشد.");
return;
}
let jpegImages = [];
images.forEach((img) => {
if ((img.src.endsWith('.jpeg') || img.src.endsWith('.jpg')) && !img.src.includes('data:image')) {
jpegImages.push(img.src);
}
});
if (jpegImages.length === 0) {
alert("هیچ تصویر ساخته شدهای برای دانلود یافت نشد.");
return;
}
const imageContainer = document.getElementById('imageContainer');
imageContainer.innerHTML = '';
jpegImages.forEach((imageUrl, index) => {
const imageItem = document.createElement('div');
imageItem.classList.add('image-item');
imageItem.innerHTML = `
<img src="${imageUrl}" alt="Image ${index + 1}">
<button onclick="processImage('${imageUrl}')">دانلود</button>
`;
imageContainer.appendChild(imageItem);
});
document.getElementById('downloadModal').style.display = 'block';
} catch (error) {
console.error("خطا در شناسایی تصاویر: ", error);
alert("خطایی در شناسایی تصاویر رخ داد.");
}
}
// تابع برای آپلود تصویر به سرور و دریافت لینک از دامنه خودتان
window.processImage = function(imageUrl) {
// نمایش پیام ساخت لینک دانلود
const buildingLinkNotification = document.getElementById('buildingLinkNotification');
buildingLinkNotification.style.display = 'block';
// دریافت تصویر به صورت blob
fetch(imageUrl)
.then(response => response.blob())
.then(blob => {
// آمادهسازی فرم دیتا
const formData = new FormData();
formData.append('image', blob, 'downloaded_image.jpg');
// ارسال تصویر به upload.php
return fetch('https://www.aisada.ir/hamed/upload.php', { // آدرس کامل upload.php در سرور خودتان
method: 'POST',
body: formData,
mode: 'cors'
});
})
.then(response => response.json())
.then(data => {
buildingLinkNotification.style.display = 'none';
if (data.success) {
if (confirm("لینک دانلود آماده است. برای دانلود تصویر روی تأیید کلیک کنید.")) {
// باز کردن لینک جدید در تب جدید
window.open(data.url, '_blank');
}
} else {
alert('خطا در آپلود تصویر: ' + data.message);
}
})
.catch(error => {
buildingLinkNotification.style.display = 'none';
console.error('خطا:', error);
alert('خطا در آپلود تصویر');
});
}
document.getElementById('downloadButton').addEventListener('click', handleDownload);
document.getElementById('downloadButtonBottom').addEventListener('click', handleDownload);
// مدیریت پنجره مودال دانلود
const downloadModal = document.getElementById('downloadModal');
const closeDownloadModalBtn = document.getElementById('closeDownloadModalBtn');
closeDownloadModalBtn.addEventListener('click', function() {
downloadModal.style.display = 'none';
});
// مدیریت پنجره مودال توضیحات
const descriptionModal = document.getElementById('descriptionModal');
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');
});
// بستن پنجره مودال با کلیک خارج از آن
window.addEventListener('click', function(event) {
if (event.target == downloadModal) {
downloadModal.style.display = 'none';
}
if (event.target == descriptionModal) {
descriptionModal.style.display = 'none';
}
});
// دکمههای اسکرول
scrollHandle.addEventListener('click', (event) => {
if (event.target.classList.contains('up')) {
iframe.contentWindow.scrollBy(0, -100);
} else if (event.target.classList.contains('down')) {
iframe.contentWindow.scrollBy(0, 100);
}
});
// شروع بارگذاری iframe و نوار پیشرفت
loadIframe();
startProgress();
})();
</script>
بهطور کلی برای ارسال تصاویر به سرور و دریافت لینک دانلود، باید یک مثال ساده از کد JavaScript و یک بخش PHP برای پردازش بارگذاری تصویر تهیه کنیم. در زیر، یک کد ساده را برای بارگذاری تصویر و دریافت لینک دانلود از سرور به شما ارائه میدهم. توجه داشته باشید که در کد زیر، نام دامنه و سایر جزئیات مخصوص سرور باید با توجه به نیازهای واقعی شما مشخص شود.
کد JavaScript (Front-end)
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Upload Image Example</title>
</head>
<body>
<input type="file" id="imageInput" accept="image/*">
<button id="uploadButton">آپلود تصویر</button>
<div id="notification"></div>
<script>
document.getElementById('uploadButton').addEventListener('click', function() {
const input = document.getElementById('imageInput');
const file = input.files[0];
if (!file) {
alert("لطفاً یک تصویر انتخاب کنید.");
return;
}
const formData = new FormData();
formData.append('image', file); // تصویر را به فرم دادهها اضافه میکنیم
fetch('upload.php', { // فرض بر این است که upload.php در سرور شما قرار دارد
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
const notification = document.getElementById('notification');
notification.innerHTML = `لینک دانلود: <a href="${data.url}" target="_blank">${data.url}</a>`;
} else {
alert('خطا در بارگذاری تصویر: ' + data.message);
}
})
.catch(error => {
console.error('خطا:', error);
alert('خطا در بارگذاری تصویر');
});
});
</script>
</body>
</html>
کد PHP (Back-end) - upload.php
<?php
$response = ['success' => false];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_FILES['image'])) {
$uploadDir = 'uploads/'; // دایرکتوری بارگذاری تصاویر
$uploadFile = $uploadDir . basename($_FILES['image']['name']);
// بررسی اینکه آیا دایرکتوری وجود دارد یا خیر
if (!file_exists($uploadDir)) {
mkdir($uploadDir, 0777, true);
}
// انتقال فایل به دایرکتوری بارگذاری
if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadFile)) {
$response['success'] = true;
$response['url'] = $uploadFile; // آدرس تصویر بارگذاری شده
} else {
$response['message'] = 'مشکلی در بارگذاری تصویر پیش آمد.';
}
} else {
$response['message'] = 'تصویری ارسال نشده است.';
}
// بازگشت پاسخ به صورت JSON
header('Content-Type: application/json');
echo json_encode($response);
?>
توضیحات:
- HTML و JavaScript: کاربر یک تصویر را انتخاب میکند و بر روی دکمه "آپلود تصویر" کلیک میکند. تصویر به سرور ارسال میشود و پس از دریافت پاسخ، لینک دانلود به کاربر نشان داده میشود.
- PHP: تصویر به دایرکتوری مشخصشده بارگذاری میشود و پس از بارگذاری موفق، لینک تصویر به فرمت JSON به کلاینت برگردانده میشود.
به دوستانتان این دو کد را بدهید و بگویید که upload.php را در سرور خود قرار دهند و از آن استفاده کنند. فقط کافی است دایرکتوری بارگذاری (پوشه uploads) سطح دسترسی لازم را داشته باشد تا فایلها به درستی بارگذاری شوند.