Today I found that images in posts can't be enlarged, so some details are hard to see clearly. I hope a click-to-enlarge lightbox effect can be added.
So I wrote a Tampermonkey script. After installing it, clicking any image gives a fullscreen preview, with scroll-wheel zoom and drag support.
// ==UserScript==
// @name NHT Post Image Lightbox
// @namespace https://n.ht/
// @version 1.0.0
// @description Image lightbox for NHT tech community post pages: click images in the main post/replies to view the original image, with zoom, pan, previous/next switching, keyboard and touch controls
// @author Evan
// @match https://n.ht/t/*
// @run-at document-idle
// @grant none
// ==/UserScript==
(function () {
'use strict';
/* ========== Adjustable configuration ========== */
const CFG = {
// Post body/reply image selector (topic body #topicContent, reply floors .reply_content)
imgSelector: '#topicContent img, .reply_content img',
// Loaded images narrower than this are treated as small icons and excluded from the lightbox
minWidth: 60,
// Scroll-wheel zoom step
zoomStep: 1.15,
// Zoom range (multiples relative to the fitted size)
minZoom: 0.5,
maxZoom: 8,
};
/* ========== Utility functions ========== */
function isContentImg(img) {
if (!img || img.tagName !== 'IMG') return false;
if (img.closest('.emoji-inline, .topic-signature, .signature-body, .floor-avatar, .topic-avatar, .reply-to-ref')) return false;
const src = img.currentSrc || img.src || '';
if (/\/assets\/emoji\//i.test(src)) return false;
if (/avatar/i.test(src) && !/file-(private|public)\.n\.ht/i.test(src)) return false;
if (img.complete && (img.naturalWidth || 0) > 0 && img.naturalWidth < CFG.minWidth) return false;
return true;
}
function collectImages() {
const out = [];
document.querySelectorAll(CFG.imgSelector).forEach(function (n) {
if (isContentImg(n) && out.indexOf(n) === -1) out.push(n);
});
return out;
}
/* ========== Style injection (site CSP allows inline style) ========== */
const CSS = `
#nht-lightbox{position:fixed;inset:0;z-index:2147483000;display:none;align-items:stretch;flex-direction:column;background:rgba(0,0,0,.92);user-select:none;-webkit-user-select:none;touch-action:none;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif;}
#nht-lightbox.open{display:flex;}
#nht-lightbox .nhtlb-topbar{display:flex;align-items:center;gap:14px;padding:12px 20px;color:#eee;font-size:14px;flex:0 0 auto;}
#nht-lightbox .nhtlb-counter{background:rgba(255,255,255,.14);padding:3px 10px;border-radius:12px;font-variant-numeric:tabular-nums;white-space:nowrap;}
#nht-lightbox .nhtlb-alt{flex:1;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:#bbb;font-size:13px;}
#nht-lightbox .nhtlb-zoom{white-space:nowrap;font-variant-numeric:tabular-nums;color:#9adcff;}
#nht-lightbox .nhtlb-stage{position:relative;flex:1 1 auto;display:flex;align-items:center;justify-content:center;overflow:hidden;min-height:0;}
#nht-lightbox .nhtlb-wrap{display:flex;align-items:center;justify-content:center;width:100%;height:100%;}
#nht-lightbox .nhtlb-img{max-width:90vw;max-height:calc(100vh - 150px);object-fit:contain;background:transparent;box-shadow:0 4px 40px rgba(0,0,0,.6);transform-origin:center;will-change:transform;cursor:grab;}
#nht-lightbox .nhtlb-img.dragging{cursor:grabbing;}
#nht-lightbox .nhtlb-nav{position:absolute;top:50%;transform:translateY(-50%);width:56px;height:56px;border:none;border-radius:50%;background:rgba(255,255,255,.12);color:#fff;font-size:30px;line-height:1;cursor:pointer;display:flex;align-items:center;justify-content:center;transition:background .15s;z-index:2;}
#nht-lightbox .nhtlb-nav:hover{background:rgba(255,255,255,.28);}
#nht-lightbox .nhtlb-prev{left:16px;}
#nht-lightbox .nhtlb-next{right:16px;}
#nht-lightbox .nhtlb-bar{display:flex;justify-content:center;gap:12px;padding:14px 20px;flex:0 0 auto;}
#nht-lightbox .nhtlb-btn{background:rgba(255,255,255,.14);color:#fff;border:none;border-radius:8px;padding:7px 18px;font-size:14px;cursor:pointer;transition:background .15s;}
#nht-lightbox .nhtlb-btn:hover{background:rgba(255,255,255,.3);}
#nht-lightbox .nhtlb-btn-close{background:rgba(255,80,80,.35);}
#nht-lightbox .nhtlb-btn-close:hover{background:rgba(255,80,80,.55);}
#nht-lightbox .nhtlb-hint{color:#888;font-size:12px;text-align:center;padding-bottom:8px;}
${CFG.imgSelector}:not(.emoji-inline){cursor:zoom-in;}
`;
/* ========== Lightbox state ========== */
let overlay = null;
let imgEl = null;
const S = {
list: [],
index: -1,
scale: 1,
fit: 1,
tx: 0,
ty: 0,
dragging: false,
moved: false,
pointerType: 'mouse',
startX: 0,
startY: 0,
dragX: 0,
dragY: 0,
};
function ensureOverlay() {
if (overlay) return;
const css = document.createElement('style');
css.id = 'nht-lightbox-style';
css.textContent = CSS;
(document.head || document.documentElement).appendChild(css);
overlay = document.createElement('div');
overlay.id = 'nht-lightbox';
const topbar = document.createElement('div');
topbar.className = 'nhtlb-topbar';
topbar.innerHTML = '<span class="nhtlb-counter">1 / 1</span><span class="nhtlb-alt"></span><span class="nhtlb-zoom">100%</span>';
overlay.appendChild(topbar);
const stage = document.createElement('div');
stage.className = 'nhtlb-stage';
stage.innerHTML = '<button class="nhtlb-nav nhtlb-prev" type="button">‹</button><div class="nhtlb-wrap"><img class="nhtlb-img" alt="" draggable="false"></div><button class="nhtlb-nav nhtlb-next" type="button">›</button>';
overlay.appendChild(stage);
const bar = document.createElement('div');
bar.className = 'nhtlb-bar';
bar.innerHTML =
'<button class="nhtlb-btn" type="button" data-act="download">Download</button>' +
'<button class="nhtlb-btn" type="button" data-act="open">Original</button>' +
'<button class="nhtlb-btn nhtlb-btn-close" type="button" data-act="close">Close</button>';
overlay.appendChild(bar);
const hint = document.createElement('div');
hint.className = 'nhtlb-hint';
hint.textContent = 'Click image to toggle zoom · Scroll wheel to zoom · Drag to pan · ← → to switch · Esc to close';
overlay.appendChild(hint);
document.body.appendChild(overlay);
imgEl = overlay.querySelector('.nhtlb-img');
bindOverlayEvents();
}
/* ========== Lightbox core ========== */
function openLightbox(img) {
const list = collectImages();
const idx = list.indexOf(img);
if (idx < 0) return;
ensureOverlay();
S.list = list;
S.index = idx;
overlay.classList.add('open');
document.documentElement.style.overflow = 'hidden';
renderCurrent();
}
function closeLightbox() {
if (!overlay) return;
overlay.classList.remove('open');
document.documentElement.style.overflow = '';
S.list = [];
S.index = -1;
}
function currentImg() {
return S.list[S.index] || null;
}
function renderCurrent() {
const srcImg = currentImg();
if (!srcImg) return;
const url = srcImg.currentSrc || srcImg.src;
S.scale = 1;
S.fit = 1;
S.tx = 0;
S.ty = 0;
imgEl.onload = computeFit;
imgEl.onerror = function () { imgEl.src = url; };
imgEl.src = url;
overlay.querySelector('.nhtlb-alt').textContent = srcImg.alt || '';
overlay.querySelector('.nhtlb-counter').textContent = (S.index + 1) + ' / ' + S.list.length;
updateNavState();
}
function updateNavState() {
const prev = overlay.querySelector('.nhtlb-prev');
const next = overlay.querySelector('.nhtlb-next');
prev.style.visibility = S.list.length > 1 ? '' : 'hidden';
next.style.visibility = S.list.length > 1 ? '' : 'hidden';
}
function nav(delta) {
if (!S.list.length) return;
S.index = (S.index + delta + S.list.length) % S.list.length;
renderCurrent();
}
function computeFit() {
const vw = window.innerWidth * 0.9;
const vh = window.innerHeight - 160;
const iw = imgEl.naturalWidth || vw;
const ih = imgEl.naturalHeight || vh;
S.fit = Math.min(vw / iw, vh / ih, 1);
if (S.scale === 1) S.scale = S.fit;
applyTransform();
}
function applyTransform() {
imgEl.style.transform = 'translate(' + S.tx + 'px,' + S.ty + 'px) scale(' + S.scale + ')';
const pct = Math.round((S.scale / S.fit) * 100);
overlay.querySelector('.nhtlb-zoom').textContent = pct + '%';
}
function clampZoom(v) {
return Math.min(CFG.ma