Ezmary commited on
Commit
fbe661a
·
verified ·
1 Parent(s): 82fb80f

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +112 -9
index.html CHANGED
@@ -252,10 +252,6 @@
252
  background: linear-gradient(to right, #38bdf8, #bb86fc, #facc15);
253
  animation: progress 7s infinite linear;
254
  }
255
- @keyframes progress {
256
- 0% { width: 0%; }
257
- 100% { width: 100%; }
258
- }
259
 
260
  #result-grid { display: none; grid-template-columns: repeat(2, 1fr); gap: 1.5rem; width: 100%; }
261
  #result-container.has-content #result-grid { display: grid; }
@@ -279,13 +275,12 @@
279
  .history-delete-btn:hover { background-color: var(--panel-border); color: var(--danger-color); }
280
  .history-delete-btn svg { width: 18px; height: 18px; }
281
 
282
- /* --- FIX: Corrected Lightbox CSS --- */
283
  #lightbox {
284
  position: fixed;
285
  inset: 0;
286
  background-color: rgba(18, 24, 38, 0.6);
287
  backdrop-filter: blur(10px) saturate(150%);
288
- display: flex; /* Changed from none to flex */
289
  align-items: center;
290
  justify-content: center;
291
  z-index: 1000;
@@ -294,10 +289,10 @@
294
  pointer-events: none;
295
  }
296
  #lightbox.visible {
 
297
  opacity: 1;
298
  pointer-events: auto;
299
  }
300
- /* --- END OF FIX --- */
301
 
302
  #lightbox-content { position: relative; animation: fadeIn 0.3s ease; display: flex; align-items: center; justify-content: center; }
303
  #lightbox-img { max-width: 85vw; max-height: 80vh; object-fit: contain; border-radius: var(--radius-card); box-shadow: var(--shadow-xl); }
@@ -443,10 +438,53 @@
443
  </div>
444
 
445
  <script>
446
- // --- START OF APPLICATION LOGIC SCRIPT (No changes needed here) ---
447
  const API_URL = 'https://alfa-editor-worker.onrender.com/api/edit';
448
  const uploadArea = document.getElementById('upload-area'); const fileInput = document.getElementById('file-input'); const previewImage = document.getElementById('preview-image-main'); const removeFileBtn = document.getElementById('remove-file-btn-main'); const promptInput = document.getElementById('prompt-input'); const submitBtn = document.getElementById('submit-btn'); const resultContainer = document.getElementById('result-container'); const resultGrid = document.getElementById('result-grid'); const errorMessage = document.getElementById('error-message'); const lightbox = document.getElementById('lightbox'); const lightboxImg = document.getElementById('lightbox-img'); const lightboxClose = document.getElementById('lightbox-close'); const lightboxDownload = document.getElementById('lightbox-download'); const lightboxNext = document.getElementById('lightbox-next'); const historyGrid = document.getElementById('history-grid'); const clearHistoryBtn = document.getElementById('clear-history-btn'); const confirmationModal = document.getElementById('confirmation-modal'); const modalMessageText = document.getElementById('modal-message-text'); const modalConfirmBtn = document.getElementById('modal-confirm-btn'); const modalCancelBtn = document.getElementById('modal-cancel-btn');
449
  let uploadedFile = null; let currentLightboxUrl = null; let currentLightboxGroup = []; let currentLightboxIndex = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
450
  const convertToPNG = (file) => {
451
  return new Promise((resolve, reject) => {
452
  const image = new Image();
@@ -497,7 +535,72 @@
497
  checkFormState();
498
  clearResult();
499
  };
500
- const showConfirmationModal = (message, onConfirm) => { modalMessageText.textContent = message; confirmationModal.classList.add('visible'); modalConfirmBtn.onclick = () => { onConfirm(); hideConfirmationModal(); }; modalCancelBtn.onclick = () => { hideConfirmationModal(); }; }; const hideConfirmationModal = () => { confirmationModal.classList.remove('visible'); modalConfirmBtn.onclick = null; modalCancelBtn.onclick = null; }; const resetUploader = () => { uploadedFile = null; fileInput.value = ''; previewImage.src = ''; uploadArea.classList.remove('has-file'); checkFormState(); }; const checkFormState = () => { submitBtn.disabled = !uploadedFile || promptInput.value.trim() === ''; }; const setLoading = (isLoading) => { const btnSpinner = submitBtn.querySelector('.spinner'); const btnText = document.getElementById('btn-text'); const btnIcon = submitBtn.querySelector('svg'); if (isLoading) { resultContainer.classList.remove('has-content'); resultContainer.classList.add('loading'); btnSpinner.style.display = 'inline-block'; btnIcon.style.display = 'none'; btnText.textContent = 'در حال پردازش...'; submitBtn.disabled = true; resultGrid.innerHTML = ''; errorMessage.style.display = 'none'; } else { resultContainer.classList.remove('loading'); btnSpinner.style.display = 'none'; btnIcon.style.display = 'inline-block'; btnText.textContent = 'ایجاد کن'; checkFormState(); } }; const displayResult = (imageUrls) => { resultGrid.innerHTML = ''; imageUrls.forEach((url) => { const img = document.createElement('img'); img.src = url; img.alt = 'تصویر ویرایش شده'; img.addEventListener('click', () => openLightbox(url, imageUrls)); resultGrid.appendChild(img); }); resultContainer.classList.add('has-content'); }; const clearResult = () => { resultGrid.innerHTML = ''; errorMessage.style.display = 'none'; resultContainer.classList.remove('has-content'); }; const displayError = (message) => { errorMessage.textContent = message; errorMessage.style.display = 'block'; }; const updateLightboxImage = () => { const newUrl = currentLightboxGroup[currentLightboxIndex]; lightboxImg.src = newUrl; currentLightboxUrl = newUrl; }; const openLightbox = (clickedUrl, urlGroup) => { currentLightboxGroup = urlGroup; currentLightboxIndex = urlGroup.indexOf(clickedUrl); updateLightboxImage(); lightboxNext.style.display = urlGroup.length > 1 ? 'flex' : 'none'; lightbox.classList.add('visible'); }; const closeLightbox = () => { lightbox.classList.remove('visible'); currentLightboxGroup = []; }; const handleDownload = async () => { if (!currentLightboxUrl) return; const spinner = lightboxDownload.querySelector('.spinner'); const icon = lightboxDownload.querySelector('svg'); spinner.style.display = 'block'; icon.style.display = 'none'; lightboxDownload.disabled = true; try { const response = await fetch(currentLightboxUrl); if (!response.ok) throw new Error('Network response was not ok.'); const blob = await response.blob(); const objectUrl = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = objectUrl; a.download = `ai-edited-image-${Date.now()}.png`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(objectUrl); } catch (e) { console.error('Download failed:', e); alert('خطا در دانلود تصویر. لطفا دوباره تلاش کنید.'); } finally { spinner.style.display = 'none'; icon.style.display = 'block'; lightboxDownload.disabled = false; } }; const getHistory = () => JSON.parse(localStorage.getItem('aiPhotoshopHistory') || '[]'); const saveHistory = (history) => { localStorage.setItem('aiPhotoshopHistory', JSON.stringify(history)); renderHistory(); }; const addToHistory = (item) => { let history = getHistory(); history.unshift(item); if (history.length > 15) history.pop(); saveHistory(history); }; const handleClearHistory = () => { showConfirmationModal('آیا از پاک کردن تمام تاریخچه مطمئن هستید؟ این عمل غیرقابل بازگشت است.', () => { saveHistory([]); }); }; const handleDeleteItem = (index) => { showConfirmationModal('آیا از حذف این مورد از تاریخچه مطمئن هستید؟', () => { let history = getHistory(); history.splice(index, 1); saveHistory(history); }); }; const renderHistory = () => { const history = getHistory(); historyGrid.innerHTML = ''; clearHistoryBtn.style.display = history.length > 0 ? 'flex' : 'none'; history.forEach((item, index) => { const card = document.createElement('div'); card.className = 'history-item'; const deleteBtn = document.createElement('button'); deleteBtn.className = 'history-delete-btn'; deleteBtn.title = 'حذف این مورد'; deleteBtn.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 6h18"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>`; deleteBtn.onclick = () => handleDeleteItem(index); card.appendChild(deleteBtn); const imageGrid = document.createElement('div'); imageGrid.className = 'history-item-grid'; item.urls.forEach(url => { const img = document.createElement('img'); img.src = url; img.alt = 'تصویر از تاریخچه'; img.addEventListener('click', () => openLightbox(url, item.urls)); imageGrid.appendChild(img); }); card.appendChild(imageGrid); historyGrid.appendChild(card); }); };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501
  document.addEventListener('DOMContentLoaded', () => { renderHistory(); checkFormState(); });
502
  removeFileBtn.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); resetUploader(); }); fileInput.addEventListener('change', (e) => handleFile(e.target.files[0])); ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(e => { uploadArea.addEventListener(e, p => { p.preventDefault(); p.stopPropagation(); }); }); ['dragenter', 'dragover'].forEach(e => { uploadArea.addEventListener(e, () => { if (!uploadArea.classList.contains('has-file')) uploadArea.classList.add('drag-over'); }); }); ['dragleave', 'drop'].forEach(e => { uploadArea.addEventListener(e, () => uploadArea.classList.remove('drag-over')); }); uploadArea.addEventListener('drop', e => { if (!uploadArea.classList.contains('has-file')) handleFile(e.dataTransfer.files[0]); }); promptInput.addEventListener('input', checkFormState);
503
  submitBtn.addEventListener('click', async () => { if (submitBtn.disabled) return; setLoading(true); const formData = new FormData(); formData.append('image', uploadedFile); formData.append('prompt', promptInput.value.trim()); try { const response = await fetch(API_URL, { method: 'POST', body: formData }); if (!response.ok) { const errorData = await response.json().catch(() => ({ error: `خطای سرور: ${response.statusText}` })); throw new Error(errorData.error || `یک خطای ناشناخته رخ داد.`); } const result = await response.json(); if (result.image_urls && Array.isArray(result.image_urls) && result.image_urls.length > 0) { displayResult(result.image_urls); addToHistory({ prompt: promptInput.value.trim(), urls: result.image_urls }); } else { throw new Error('پاسخ معتبری از سرور دریافت نشد.'); } } catch (error) { displayError(error.message); } finally { setLoading(false); } });
 
252
  background: linear-gradient(to right, #38bdf8, #bb86fc, #facc15);
253
  animation: progress 7s infinite linear;
254
  }
 
 
 
 
255
 
256
  #result-grid { display: none; grid-template-columns: repeat(2, 1fr); gap: 1.5rem; width: 100%; }
257
  #result-container.has-content #result-grid { display: grid; }
 
275
  .history-delete-btn:hover { background-color: var(--panel-border); color: var(--danger-color); }
276
  .history-delete-btn svg { width: 18px; height: 18px; }
277
 
 
278
  #lightbox {
279
  position: fixed;
280
  inset: 0;
281
  background-color: rgba(18, 24, 38, 0.6);
282
  backdrop-filter: blur(10px) saturate(150%);
283
+ display: none; /* Changed back to none for default hidden state */
284
  align-items: center;
285
  justify-content: center;
286
  z-index: 1000;
 
289
  pointer-events: none;
290
  }
291
  #lightbox.visible {
292
+ display: flex; /* Use flex to display when visible */
293
  opacity: 1;
294
  pointer-events: auto;
295
  }
 
296
 
297
  #lightbox-content { position: relative; animation: fadeIn 0.3s ease; display: flex; align-items: center; justify-content: center; }
298
  #lightbox-img { max-width: 85vw; max-height: 80vh; object-fit: contain; border-radius: var(--radius-card); box-shadow: var(--shadow-xl); }
 
438
  </div>
439
 
440
  <script>
441
+ // --- START OF APPLICATION LOGIC SCRIPT (MODIFIED FOR IFRAME COMPATIBILITY) ---
442
  const API_URL = 'https://alfa-editor-worker.onrender.com/api/edit';
443
  const uploadArea = document.getElementById('upload-area'); const fileInput = document.getElementById('file-input'); const previewImage = document.getElementById('preview-image-main'); const removeFileBtn = document.getElementById('remove-file-btn-main'); const promptInput = document.getElementById('prompt-input'); const submitBtn = document.getElementById('submit-btn'); const resultContainer = document.getElementById('result-container'); const resultGrid = document.getElementById('result-grid'); const errorMessage = document.getElementById('error-message'); const lightbox = document.getElementById('lightbox'); const lightboxImg = document.getElementById('lightbox-img'); const lightboxClose = document.getElementById('lightbox-close'); const lightboxDownload = document.getElementById('lightbox-download'); const lightboxNext = document.getElementById('lightbox-next'); const historyGrid = document.getElementById('history-grid'); const clearHistoryBtn = document.getElementById('clear-history-btn'); const confirmationModal = document.getElementById('confirmation-modal'); const modalMessageText = document.getElementById('modal-message-text'); const modalConfirmBtn = document.getElementById('modal-confirm-btn'); const modalCancelBtn = document.getElementById('modal-cancel-btn');
444
  let uploadedFile = null; let currentLightboxUrl = null; let currentLightboxGroup = []; let currentLightboxIndex = 0;
445
+
446
+ // --- START OF FIX ---
447
+ // Cache to store blob URLs and prevent re-fetching the same image
448
+ const loadedBlobUrls = new Map();
449
+
450
+ /**
451
+ * Loads an image via fetch, converts it to a blob URL, and sets it as the src of an img element.
452
+ * This helps bypass potential cross-origin/referrer policy issues inside an iframe.
453
+ * @param {HTMLImageElement} imgElement The image element to update.
454
+ * @param {string} imageUrl The original URL of the image to load.
455
+ */
456
+ const loadImageAsBlob = (imgElement, imageUrl) => {
457
+ if (!imageUrl) return;
458
+
459
+ // Use cached blob URL if it exists
460
+ if (loadedBlobUrls.has(imageUrl)) {
461
+ imgElement.src = loadedBlobUrls.get(imageUrl);
462
+ return;
463
+ }
464
+
465
+ // Optional: Show a placeholder while the image is loading
466
+ imgElement.src = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'; // 1x1 transparent gif
467
+
468
+ fetch(imageUrl)
469
+ .then(response => {
470
+ if (!response.ok) {
471
+ throw new Error(`خطا در دریافت تصویر: ${response.statusText}`);
472
+ }
473
+ return response.blob();
474
+ })
475
+ .then(blob => {
476
+ const objectURL = URL.createObjectURL(blob);
477
+ loadedBlobUrls.set(imageUrl, objectURL); // Cache the new blob URL
478
+ imgElement.src = objectURL;
479
+ })
480
+ .catch(error => {
481
+ console.error('Image loading via blob failed:', error);
482
+ imgElement.alt = 'خطا در بارگذاری تصویر';
483
+ // You can set a broken image placeholder src here if you want
484
+ });
485
+ };
486
+ // --- END OF FIX ---
487
+
488
  const convertToPNG = (file) => {
489
  return new Promise((resolve, reject) => {
490
  const image = new Image();
 
535
  checkFormState();
536
  clearResult();
537
  };
538
+ const showConfirmationModal = (message, onConfirm) => { modalMessageText.textContent = message; confirmationModal.classList.add('visible'); modalConfirmBtn.onclick = () => { onConfirm(); hideConfirmationModal(); }; modalCancelBtn.onclick = () => { hideConfirmationModal(); }; }; const hideConfirmationModal = () => { confirmationModal.classList.remove('visible'); modalConfirmBtn.onclick = null; modalCancelBtn.onclick = null; }; const resetUploader = () => { uploadedFile = null; fileInput.value = ''; previewImage.src = ''; uploadArea.classList.remove('has-file'); checkFormState(); }; const checkFormState = () => { submitBtn.disabled = !uploadedFile || promptInput.value.trim() === ''; }; const setLoading = (isLoading) => { const btnSpinner = submitBtn.querySelector('.spinner'); const btnText = document.getElementById('btn-text'); const btnIcon = submitBtn.querySelector('svg'); if (isLoading) { resultContainer.classList.remove('has-content'); resultContainer.classList.add('loading'); btnSpinner.style.display = 'inline-block'; btnIcon.style.display = 'none'; btnText.textContent = 'در حال پردازش...'; submitBtn.disabled = true; resultGrid.innerHTML = ''; errorMessage.style.display = 'none'; } else { resultContainer.classList.remove('loading'); btnSpinner.style.display = 'none'; btnIcon.style.display = 'inline-block'; btnText.textContent = 'ایجاد کن'; checkFormState(); } };
539
+ const displayResult = (imageUrls) => {
540
+ resultGrid.innerHTML = '';
541
+ imageUrls.forEach((url) => {
542
+ const img = document.createElement('img');
543
+ img.alt = 'تصویر ویرایش شده';
544
+ img.dataset.originalUrl = url; // Store original URL for lightbox and download
545
+ img.addEventListener('click', () => openLightbox(img.dataset.originalUrl, imageUrls));
546
+ resultGrid.appendChild(img);
547
+ loadImageAsBlob(img, url); // MODIFIED: Load image using the new function
548
+ });
549
+ resultContainer.classList.add('has-content');
550
+ };
551
+ const clearResult = () => { resultGrid.innerHTML = ''; errorMessage.style.display = 'none'; resultContainer.classList.remove('has-content'); }; const displayError = (message) => { errorMessage.textContent = message; errorMessage.style.display = 'block'; };
552
+ const updateLightboxImage = () => {
553
+ const newUrl = currentLightboxGroup[currentLightboxIndex];
554
+ currentLightboxUrl = newUrl; // Keep original URL for download functionality
555
+ loadImageAsBlob(lightboxImg, newUrl); // MODIFIED: Load the main lightbox image using the new function
556
+ };
557
+ const openLightbox = (clickedUrl, urlGroup) => {
558
+ currentLightboxGroup = urlGroup;
559
+ currentLightboxIndex = urlGroup.indexOf(clickedUrl);
560
+ updateLightboxImage();
561
+ lightboxNext.style.display = urlGroup.length > 1 ? 'flex' : 'none';
562
+ lightbox.classList.add('visible');
563
+ };
564
+ const closeLightbox = () => { lightbox.classList.remove('visible'); lightboxImg.src = ''; currentLightboxGroup = []; };
565
+ const handleDownload = async () => {
566
+ if (!currentLightboxUrl) return;
567
+ // --- NEW: Send message to parent for download ---
568
+ if (window.parent) {
569
+ window.parent.postMessage({ type: 'DOWNLOAD_REQUEST', url: currentLightboxUrl }, '*');
570
+ } else {
571
+ // Fallback for when not in an iframe
572
+ console.warn("Not in an iframe, attempting direct download.");
573
+ const spinner = lightboxDownload.querySelector('.spinner'); const icon = lightboxDownload.querySelector('svg'); spinner.style.display = 'block'; icon.style.display = 'none'; lightboxDownload.disabled = true; try { const response = await fetch(currentLightboxUrl); if (!response.ok) throw new Error('Network response was not ok.'); const blob = await response.blob(); const objectUrl = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = objectUrl; a.download = `ai-edited-image-${Date.now()}.png`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(objectUrl); } catch (e) { console.error('Download failed:', e); alert('خطا در دانلود تصویر. لطفا دوباره تلاش کنید.'); } finally { spinner.style.display = 'none'; icon.style.display = 'block'; lightboxDownload.disabled = false; }
574
+ }
575
+ };
576
+ const getHistory = () => JSON.parse(localStorage.getItem('aiPhotoshopHistory') || '[]'); const saveHistory = (history) => { localStorage.setItem('aiPhotoshopHistory', JSON.stringify(history)); renderHistory(); }; const addToHistory = (item) => { let history = getHistory(); history.unshift(item); if (history.length > 15) history.pop(); saveHistory(history); }; const handleClearHistory = () => { showConfirmationModal('آیا از پاک کردن تمام تاریخچه مطمئن هستید؟ این عمل غیرقابل بازگشت است.', () => { saveHistory([]); }); }; const handleDeleteItem = (index) => { showConfirmationModal('آیا از حذف این مورد از تاریخچه مطمئن هستید؟', () => { let history = getHistory(); history.splice(index, 1); saveHistory(history); }); };
577
+ const renderHistory = () => {
578
+ const history = getHistory();
579
+ historyGrid.innerHTML = '';
580
+ clearHistoryBtn.style.display = history.length > 0 ? 'flex' : 'none';
581
+ history.forEach((item, index) => {
582
+ const card = document.createElement('div');
583
+ card.className = 'history-item';
584
+ const deleteBtn = document.createElement('button');
585
+ deleteBtn.className = 'history-delete-btn';
586
+ deleteBtn.title = 'حذف این مورد';
587
+ deleteBtn.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 6h18"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>`;
588
+ deleteBtn.onclick = () => handleDeleteItem(index);
589
+ card.appendChild(deleteBtn);
590
+ const imageGrid = document.createElement('div');
591
+ imageGrid.className = 'history-item-grid';
592
+ item.urls.forEach(url => {
593
+ const img = document.createElement('img');
594
+ img.alt = 'تصویر از تاریخچه';
595
+ img.dataset.originalUrl = url; // Store original URL
596
+ img.addEventListener('click', () => openLightbox(img.dataset.originalUrl, item.urls));
597
+ imageGrid.appendChild(img);
598
+ loadImageAsBlob(img, url); // MODIFIED: Load image using the new function
599
+ });
600
+ card.appendChild(imageGrid);
601
+ historyGrid.appendChild(card);
602
+ });
603
+ };
604
  document.addEventListener('DOMContentLoaded', () => { renderHistory(); checkFormState(); });
605
  removeFileBtn.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); resetUploader(); }); fileInput.addEventListener('change', (e) => handleFile(e.target.files[0])); ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(e => { uploadArea.addEventListener(e, p => { p.preventDefault(); p.stopPropagation(); }); }); ['dragenter', 'dragover'].forEach(e => { uploadArea.addEventListener(e, () => { if (!uploadArea.classList.contains('has-file')) uploadArea.classList.add('drag-over'); }); }); ['dragleave', 'drop'].forEach(e => { uploadArea.addEventListener(e, () => uploadArea.classList.remove('drag-over')); }); uploadArea.addEventListener('drop', e => { if (!uploadArea.classList.contains('has-file')) handleFile(e.dataTransfer.files[0]); }); promptInput.addEventListener('input', checkFormState);
606
  submitBtn.addEventListener('click', async () => { if (submitBtn.disabled) return; setLoading(true); const formData = new FormData(); formData.append('image', uploadedFile); formData.append('prompt', promptInput.value.trim()); try { const response = await fetch(API_URL, { method: 'POST', body: formData }); if (!response.ok) { const errorData = await response.json().catch(() => ({ error: `خطای سرور: ${response.statusText}` })); throw new Error(errorData.error || `یک خطای ناشناخته رخ داد.`); } const result = await response.json(); if (result.image_urls && Array.isArray(result.image_urls) && result.image_urls.length > 0) { displayResult(result.image_urls); addToHistory({ prompt: promptInput.value.trim(), urls: result.image_urls }); } else { throw new Error('پاسخ معتبری از سرور دریافت نشد.'); } } catch (error) { displayError(error.message); } finally { setLoading(false); } });