File: /www/wwwroot/nasuhang.com/wp-content/plugins/comment-image-lightbox/js/lightbox.js
(function () {
'use strict';
var overlay = null;
var closeBtn = null;
var lightboxImg = null;
function createLightbox() {
if (document.getElementById('comment-image-lightbox-overlay')) {
return;
}
overlay = document.createElement('div');
overlay.id = 'comment-image-lightbox-overlay';
lightboxImg = document.createElement('img');
lightboxImg.alt = '';
overlay.appendChild(lightboxImg);
closeBtn = document.createElement('button');
closeBtn.id = 'comment-image-lightbox-close';
closeBtn.type = 'button';
closeBtn.setAttribute('aria-label', '关闭');
closeBtn.textContent = '\u00D7';
document.body.appendChild(closeBtn);
document.body.appendChild(overlay);
overlay.addEventListener('click', closeLightbox);
closeBtn.addEventListener('click', closeLightbox);
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && overlay.classList.contains('active')) {
closeLightbox();
}
});
}
function openLightbox(src, alt) {
if (!overlay) {
createLightbox();
}
lightboxImg.src = src;
lightboxImg.alt = alt || '';
overlay.classList.add('active');
closeBtn.classList.add('active');
document.body.style.overflow = 'hidden';
}
function closeLightbox() {
if (!overlay) return;
overlay.classList.remove('active');
closeBtn.classList.remove('active');
document.body.style.overflow = '';
}
function init() {
var selectors = [
'.wp-block-comment-content img',
'.comment-content img',
'.wp-block-post-comments .commentlist img'
];
var commentImages = document.querySelectorAll(selectors.join(','));
commentImages.forEach(function (img) {
if (img.closest('a')) return;
if (img.classList.contains('comment-lightbox-bound')) return;
img.classList.add('comment-lightbox-bound');
img.addEventListener('click', function (e) {
e.preventDefault();
e.stopPropagation();
var src = img.getAttribute('data-full') || img.src;
openLightbox(src, img.alt);
});
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
var observer = new MutationObserver(function () {
init();
});
observer.observe(document.body, { childList: true, subtree: true });
})();