Initial MANGABOM website
This commit is contained in:
34
dist/images.js
vendored
Normal file
34
dist/images.js
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
(() => {
|
||||
const extensions = ['webp', 'png', 'jpg', 'jpeg', 'avif', 'WEBP', 'PNG', 'JPG', 'JPEG', 'AVIF'];
|
||||
async function load(image) {
|
||||
const base = image.dataset.image;
|
||||
for (const extension of extensions) {
|
||||
const url = base + '.' + extension;
|
||||
try {
|
||||
const response = await fetch(url, { method: 'HEAD', cache: 'no-store' });
|
||||
if (!response.ok) continue;
|
||||
const loaded = await new Promise(resolve => {
|
||||
image.onload = () => resolve(true); image.onerror = () => resolve(false);
|
||||
const version = response.headers.get('etag') || [response.headers.get('last-modified'), response.headers.get('content-length')].join('-') || String(Date.now());
|
||||
image.src = url + '?v=' + encodeURIComponent(version);
|
||||
});
|
||||
image.onload = image.onerror = null;
|
||||
if (loaded) {
|
||||
image.classList.add('image-ready');
|
||||
if (image.parentElement?.classList.contains('hero-art')) image.parentElement.style.setProperty('--hero-image', 'url("' + image.src + '" )');
|
||||
return;
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
if (image.dataset.imageLabel) {
|
||||
const fallback = document.createElement('div'); fallback.className = 'image-placeholder';
|
||||
const brand = document.createElement('span'); brand.textContent = 'MANGABOM';
|
||||
const title = document.createElement('b'); title.textContent = image.dataset.imageLabel;
|
||||
fallback.append(brand, title); image.replaceWith(fallback);
|
||||
} else image.remove();
|
||||
}
|
||||
Promise.all([...document.querySelectorAll('img[data-image]')].map(load)).finally(() => {
|
||||
document.documentElement.dataset.imagesReady = 'true';
|
||||
document.dispatchEvent(new Event('mangabom:images-ready'));
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user