MediaWiki:Common.js:修订间差异
外观
无编辑摘要 |
无编辑摘要 |
||
| 第5行: | 第5行: | ||
if (!container) return; | if (!container) return; | ||
var total = mw.config.get('wgNumberOfPages') || 0; | var total = mw.config.get('wgNumberOfPages') || 0; | ||
if (!total) total = 123456; // | if (!total) total = 123456; // 测试用,确认正常后删除 | ||
var digits = String(total).padStart(PAD, '0').split(''); | var digits = String(total).padStart(PAD, '0').split(''); | ||
| 第18行: | 第17行: | ||
var imgH = Math.round(imgW * 1.25); | var imgH = Math.round(imgW * 1.25); | ||
digits. | // 用 API 批量获取图片真实 URL | ||
var titles = digits.map(function(d) { return 'File:' + d + '.gif'; }).join('|'); | |||
$.ajax({ | |||
url: mw.util.wikiScript('api'), | |||
data: { | |||
action: 'query', | |||
titles: titles, | |||
prop: 'imageinfo', | |||
iiprop: 'url', | |||
format: 'json' | |||
}, | |||
dataType: 'json', | |||
success: function(data) { | |||
container.appendChild( | var urlMap = {}; | ||
if (data.query && data.query.pages) { | |||
$.each(data.query.pages, function(pageId, page) { | |||
if (page.imageinfo && page.imageinfo[0]) { | |||
// 提取文件名作为 key | |||
var fileName = page.title.replace('File:', ''); | |||
urlMap[fileName] = page.imageinfo[0].url; | |||
} | |||
}); | |||
} | |||
// 渲染图片 | |||
digits.forEach(function(d) { | |||
var img = document.createElement('img'); | |||
var fileName = d + '.gif'; | |||
if (urlMap[fileName]) { | |||
img.src = urlMap[fileName]; | |||
} else { | |||
// API 没返回,fallback | |||
img.onerror(); | |||
} | |||
img.alt = d; | |||
img.style.cssText = 'width:' + imgW + 'px;height:' + imgH + 'px;object-fit:contain;image-rendering:pixelated;display:block;'; | |||
img.onerror = function() { | |||
var fb = document.createElement('div'); | |||
fb.textContent = d; | |||
fb.style.cssText = 'width:' + imgW + 'px;height:' + imgH + 'px;display:flex;align-items:center;justify-content:center;background:#333;color:#ff9ecd;font-size:' + Math.max(12, Math.floor(imgW * 0.4)) + 'px;font-weight:bold;font-family:monospace;border-radius:3px;'; | |||
if (img.parentNode) img.parentNode.replaceChild(fb, img); | |||
}; | |||
container.appendChild(img); | |||
}); | |||
}, | |||
error: function() { | |||
// API 失败,全部 fallback | |||
digits.forEach(function(d) { | |||
var fb = document.createElement('div'); | |||
fb.textContent = d; | |||
fb.style.cssText = 'width:' + imgW + 'px;height:' + imgH + 'px;display:flex;align-items:center;justify-content:center;background:#333;color:#ff9ecd;font-size:' + Math.max(12, Math.floor(imgW * 0.4)) + 'px;font-weight:bold;font-family:monospace;border-radius:3px;'; | |||
container.appendChild(fb); | |||
}); | |||
} | |||
}); | }); | ||
}); | }); | ||
2026年7月13日 (一) 22:11的版本
$(function() {
var PAD = 6;
var container = document.getElementById('neko-page-counter');
if (!container) return;
var total = mw.config.get('wgNumberOfPages') || 0;
if (!total) total = 123456; // 测试用,确认正常后删除
var digits = String(total).padStart(PAD, '0').split('');
container.innerHTML = '';
container.style.cssText = 'display:flex;align-items:flex-end;justify-content:center;gap:0;padding:10px;background:#0f0f1a;border-radius:8px;border:1px solid #2a2a40;min-height:100px;';
var parentW = container.parentElement ? container.parentElement.clientWidth : 600;
var imgW = Math.max(28, Math.min(100, Math.floor((parentW - 20) / PAD)));
var imgH = Math.round(imgW * 1.25);
// 用 API 批量获取图片真实 URL
var titles = digits.map(function(d) { return 'File:' + d + '.gif'; }).join('|');
$.ajax({
url: mw.util.wikiScript('api'),
data: {
action: 'query',
titles: titles,
prop: 'imageinfo',
iiprop: 'url',
format: 'json'
},
dataType: 'json',
success: function(data) {
var urlMap = {};
if (data.query && data.query.pages) {
$.each(data.query.pages, function(pageId, page) {
if (page.imageinfo && page.imageinfo[0]) {
// 提取文件名作为 key
var fileName = page.title.replace('File:', '');
urlMap[fileName] = page.imageinfo[0].url;
}
});
}
// 渲染图片
digits.forEach(function(d) {
var img = document.createElement('img');
var fileName = d + '.gif';
if (urlMap[fileName]) {
img.src = urlMap[fileName];
} else {
// API 没返回,fallback
img.onerror();
}
img.alt = d;
img.style.cssText = 'width:' + imgW + 'px;height:' + imgH + 'px;object-fit:contain;image-rendering:pixelated;display:block;';
img.onerror = function() {
var fb = document.createElement('div');
fb.textContent = d;
fb.style.cssText = 'width:' + imgW + 'px;height:' + imgH + 'px;display:flex;align-items:center;justify-content:center;background:#333;color:#ff9ecd;font-size:' + Math.max(12, Math.floor(imgW * 0.4)) + 'px;font-weight:bold;font-family:monospace;border-radius:3px;';
if (img.parentNode) img.parentNode.replaceChild(fb, img);
};
container.appendChild(img);
});
},
error: function() {
// API 失败,全部 fallback
digits.forEach(function(d) {
var fb = document.createElement('div');
fb.textContent = d;
fb.style.cssText = 'width:' + imgW + 'px;height:' + imgH + 'px;display:flex;align-items:center;justify-content:center;background:#333;color:#ff9ecd;font-size:' + Math.max(12, Math.floor(imgW * 0.4)) + 'px;font-weight:bold;font-family:monospace;border-radius:3px;';
container.appendChild(fb);
});
}
});
});