Conmemoración por el Día de la Bandera de Haití en el Colegio Arturo Matte Larraín
function collectGalleryIds(){
// intenta capturar IDs del grid visible detrás del lightbox
var ids = [];
// 1) anchors con id=...
document.querySelectorAll('a[href*="drive.google.com"][href*="id="]').forEach(function(a){
var id = getIdFromNode(a);
if (id) ids.push(id);
});
// 2) data-id en elementos del grid
if (!ids.length){
document.querySelectorAll('[data-id],[data-file-id],[data-drive-id],[data-google-id]').forEach(function(el){
var id = getIdFromNode(el);
if (id) ids.push(id);
});
}
// deduplicate conservando orden
var seen={}; ids = ids.filter(function(x){ if(seen[x]) return false; seen[x]=1; return true; });
return ids;
}
function replaceThumbs(ids){
// reemplaza thumbs del visor
var thumbs = document.querySelectorAll('.lg-outer .lg-thumb-item img, .lg-outer .lg-thumb img');
if (!thumbs.length || !ids.length) return;
thumbs.forEach(function(img, i){
// si ya fue reemplazada, salta
if (img.dataset.igdFixed === '1') return;
// mantener tamaño si viene como wX-hX; si no, usa 120
var w=120, h=120, m = (img.src||'').match(/w(\d+)-h(\d+)/);
if (m){ w = Math.max(64, +m[1]); h = Math.max(64, +m[2]); }
// usa el id por índice si existe; si no, intenta extraer del slide actual
var id = ids[i] || null;
if (!id){
var slide = document.querySelectorAll('.lg-outer .lg-item')[i];
if (slide){
id = getIdFromNode(slide) || getIdFromNode(slide.querySelector('a')) || getIdFromNode(slide.querySelector('img'));
}
}
if (!id) return;
// si la imagen ya es estable, no tocar
if (/drive\.google\.com\/thumbnail\?id=/.test(img.src)) {
img.dataset.igdFixed = '1';
return;
}
img.src = 'https://drive.google.com/thumbnail?id='+id+'&sz=w'+w+'-h'+h;
img.setAttribute('loading','eager');
img.setAttribute('decoding','async');
img.dataset.igdFixed = '1';
});
}
function replaceMainIfBroken(){
// por si el recurso grande también viniera de lh3
document.querySelectorAll('.lg-outer .lg-current img, .lg-outer .lg-image, .lg-outer .lg-object').forEach(function(img){
if (!img || !img.src || img.dataset.igdFixed==='1') return;
if (img.src.indexOf('lh3.googleusercontent.com/drive-storage') === -1) return;
// intenta mapear por índice actual
var current = document.querySelector('.lg-outer .lg-thumb-item.active') ||
document.querySelector('.lg-outer .lg-thumb-item.current');
var ids = window.__igdIds || [];
var idx = current ? Array.prototype.indexOf.call(current.parentNode.children, current) : -1;
var id = (idx>=0 ? ids[idx] : null) || getIdFromNode(img) || getIdFromNode(document.querySelector('.lg-outer .lg-current a'));
if (!id) return;
var w = img.naturalWidth || img.width || 1280;
var h = img.naturalHeight || img.height || 960;
img.src = 'https://drive.google.com/thumbnail?id='+id+'&sz=w'+w+'-h'+h;
img.dataset.igdFixed='1';
});
}
function boot(){
window.__igdIds = collectGalleryIds();
// cada vez que aparece el visor, intenta reemplazar
var tryFix = function(){
if (!document.querySelector('.lg-outer')) return;
replaceThumbs(window.__igdIds);
replaceMainIfBroken();
};
// intentos iniciales + breve intervalo por si LG tarda en construir thumbs
var n=0, iv = setInterval(function(){
tryFix();
if (++n > 20) clearInterval(iv); // ~10s
}, 500);
// observa cambios dentro del visor (LG recicla thumbs)
var outer = document.querySelector('.lg-outer');
if (outer){
var mo = new MutationObserver(function(muts){
muts.forEach(function(m){
if (m.addedNodes && m.addedNodes.length) tryFix();
});
});
mo.observe(outer, {childList:true, subtree:true});
}
// si alguna imagen falla, reintenta con estable
document.addEventListener('error', function(e){
var el = e.target;
if (el && el.tagName==='IMG' && /lh3\.googleusercontent\.com\/drive-storage/.test(el.src)){
replaceMainIfBroken();
replaceThumbs(window.__igdIds);
}
}, true);
}
// disparar al abrir galería (cuando aparece .lg-outer)
var pageObs = new MutationObserver(function(muts){
muts.forEach(function(m){
(m.addedNodes||[]).forEach(function(n){
if (n.nodeType===1 && n.classList && n.classList.contains('lg-outer')){
boot();
}
});
});
});
pageObs.observe(document.documentElement, {childList:true, subtree:true});
})();