Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 69 additions & 69 deletions screensaver.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,66 +276,64 @@ document.addEventListener("DOMContentLoaded", () => {
}
}

function addToHistory(imageUrl, prompt) {
if (imageHistory.includes(imageUrl)) {
console.log("Duplicate image URL detected, skipping:", imageUrl);
return;
}
imageHistory.unshift(imageUrl);
promptHistory.unshift(prompt);
if (imageHistory.length > MAX_HISTORY) {
imageHistory.pop();
promptHistory.pop();
}
saveImageHistory();
updateThumbnailHistory();
console.log("Current imageHistory length:", imageHistory.length, "Images:", imageHistory);
console.log("Current promptHistory length:", promptHistory.length, "Prompts:", promptHistory);
}

function updateThumbnailHistory() {
const thumbnailContainer = document.getElementById('screensaver-thumbnails');
if (!thumbnailContainer) {
console.error("Thumbnail container not found in DOM.");
window.showToast("Fuck, the thumbnail container is missing. Can’t populate the gallery.");
return;
}

thumbnailContainer.innerHTML = '';
imageHistory.forEach((imageUrl, index) => {
const thumb = document.createElement('img');
thumb.src = imageUrl;
thumb.classList.add('thumbnail');
thumb.title = promptHistory[index] || 'No prompt available';
thumb.alt = "Thumbnail Image";
thumb.style.opacity = '1';
thumb.onerror = () => {
console.log(`Thumbnail ${index + 1} failed to load, using fallback:`, imageUrl);
thumb.src = "https://via.placeholder.com/160x90?text=Image+Failed";
thumb.style.opacity = '1';
};
thumb.onload = () => {
console.log(`Thumbnail ${index + 1} loaded successfully:`, imageUrl);
};
thumb.onclick = () => showHistoricalImage(index);
const currentImgSrc = document.getElementById(`screensaver-${currentImage}`).src;
if (imageUrl === currentImgSrc) {
thumb.classList.add('selected');
console.log("Highlighted thumbnail as selected:", imageUrl);
}
thumbnailContainer.appendChild(thumb);
console.log(`Added thumbnail ${index + 1}/${imageHistory.length} to DOM:`, thumb.src);
});

thumbnailContainer.scrollTo({ left: 0, behavior: 'smooth' });
console.log("Updated thumbnail gallery with", imageHistory.length, "images. DOM count:", thumbnailContainer.children.length);

const offsetWidth = thumbnailContainer.offsetWidth;
thumbnailContainer.style.display = 'none';
thumbnailContainer.offsetHeight;
thumbnailContainer.style.display = 'flex';
console.log("Forced DOM reflow to ensure rendering. Container offsetWidth:", offsetWidth);
}
function addToHistory(imageUrl, prompt) {
// store newest images at the end of the list
imageHistory.push(imageUrl);
promptHistory.push(prompt);
if (imageHistory.length > MAX_HISTORY) {
imageHistory.shift();
promptHistory.shift();
}
saveImageHistory();
updateThumbnailHistory();
console.log("Current imageHistory length:", imageHistory.length, "Images:", imageHistory);
console.log("Current promptHistory length:", promptHistory.length, "Prompts:", promptHistory);
}

function updateThumbnailHistory() {
const thumbnailContainer = document.getElementById('screensaver-thumbnails');
if (!thumbnailContainer) {
console.error("Thumbnail container not found in DOM.");
window.showToast("Fuck, the thumbnail container is missing. Can’t populate the gallery.");
return;
}

thumbnailContainer.innerHTML = '';
imageHistory.forEach((imageUrl, index) => {
const thumb = document.createElement('img');
thumb.src = imageUrl;
thumb.classList.add('thumbnail');
thumb.title = promptHistory[index] || 'No prompt available';
thumb.alt = "Thumbnail Image";
thumb.style.opacity = '1';
thumb.onerror = () => {
console.log(`Thumbnail ${index + 1} failed to load, using fallback:`, imageUrl);
thumb.src = "https://via.placeholder.com/160x90?text=Image+Failed";
thumb.style.opacity = '1';
};
thumb.onload = () => {
console.log(`Thumbnail ${index + 1} loaded successfully:`, imageUrl);
};
thumb.onclick = () => showHistoricalImage(index);
const currentImgSrc = document.getElementById(`screensaver-${currentImage}`).src;
if (imageUrl === currentImgSrc) {
thumb.classList.add('selected');
console.log("Highlighted thumbnail as selected:", imageUrl);
}
thumbnailContainer.appendChild(thumb);
console.log(`Added thumbnail ${index + 1}/${imageHistory.length} to DOM:`, thumb.src);
});

// keep the view scrolled to the latest thumbnail
thumbnailContainer.scrollTo({ left: thumbnailContainer.scrollWidth, behavior: 'smooth' });
console.log("Updated thumbnail gallery with", imageHistory.length, "images. DOM count:", thumbnailContainer.children.length);

const offsetWidth = thumbnailContainer.offsetWidth;
thumbnailContainer.style.display = 'none';
thumbnailContainer.offsetHeight;
thumbnailContainer.style.display = 'flex';
console.log("Forced DOM reflow to ensure rendering. Container offsetWidth:", offsetWidth);
}

function showHistoricalImage(index) {
const imageUrl = imageHistory[index];
Expand All @@ -354,15 +352,17 @@ document.addEventListener("DOMContentLoaded", () => {
currentImage = nextImage;
updateThumbnailHistory();
};
nextImgElement.src = imageUrl;
nextImgElement.alt = "Screensaver Image";
if (nextImgElement.complete && nextImgElement.naturalWidth !== 0) {
nextImgElement.style.opacity = '1';
currentImgElement.style.opacity = '0';
currentImage = nextImage;
updateThumbnailHistory();
}
}
nextImgElement.src = imageUrl;
nextImgElement.alt = "Screensaver Image";
if (nextImgElement.complete && nextImgElement.naturalWidth !== 0) {
nextImgElement.style.opacity = '1';
currentImgElement.style.opacity = '0';
currentImage = nextImage;
updateThumbnailHistory();
}
// restart the timer so new generations resume after viewing a historical image
setOrResetImageInterval();
}

function setOrResetImageInterval() {
clearInterval(imageInterval);
Expand Down