微件:GF立绘切换
来自OGAS数据中枢
更多操作
<script> (function () {
function initSwitcher() {
document.querySelectorAll('.gf-infobox-new').forEach(function (box) {
if (box.dataset.ready) return;
box.dataset.ready = '1';
var container = box.querySelector('.gf-image-container');
var btns = box.querySelectorAll('.gf-switch-btn');
var overlay = box.querySelector('.gf-switcher-overlay');
var toggle = box.querySelector('.gf-switcher-toggle');
if (toggle && overlay) {
toggle.addEventListener('click', function() {
overlay.classList.toggle('collapsed');
});
}
if (!container) return;
// 重创立绘切换逻辑
container.addEventListener('click', function() {
var img = container.querySelector('img');
if (!img) return;
var currentSrc = img.src;
var newSrc = ;
// 判断当前是否已经是重创图 (包含 _D. 且不包含后缀前的其他 _D)
if (currentSrc.match(/_D\.(png|jpg|jpeg|gif|webp)/i)) {
// 切回原始图
newSrc = currentSrc.replace(/_D\./i, '.');
} else {
// 切到重创图
newSrc = currentSrc.replace(/\.(png|jpg|jpeg|gif|webp)/i, '_D.$1');
}
container.classList.add('loading');
var tempImg = new Image();
tempImg.onload = function() {
img.src = newSrc;
container.classList.remove('loading');
};
tempImg.onerror = function() {
console.warn("未找到对应的重创立绘: " + newSrc);
container.classList.remove('loading');
// 如果加载失败,可以加一个抖动动画提示
};
tempImg.src = newSrc;
});
// 按钮切换逻辑
btns.forEach(function (btn) {
btn.addEventListener('click', function (e) {
e.stopPropagation(); // 防止触发容器点击
if (btn.classList.contains('active')) return;
var src = btn.dataset.src;
if (!src) return;
var oldImg = container.querySelector('img');
if (oldImg) oldImg.style.opacity = '0';
setTimeout(function () {
container.innerHTML = '<img src="' + src + '" style="max-width:100%;max-height:500px;object-fit:contain;opacity:0;transition:opacity 0.3s;height:auto;width:auto;">';
var newImg = container.querySelector('img');
if (newImg) {
newImg.onload = function () { newImg.style.opacity = '1'; };
if (newImg.complete) newImg.style.opacity = '1';
}
}, 150);
btns.forEach(function (b) { b.classList.remove('active'); });
btn.classList.add('active');
});
});
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initSwitcher);
} else {
initSwitcher();
}
if (window.mw) {
mw.hook('wikipage.content').add(initSwitcher);
}
})(); </script>