BatchCreateEquipment:修订间差异
来自卡厄思梦境WIKI
创建页面,内容为“<noinclude> 这是一个用于批量创建装备页面的Widget。 使用方法:在任何页面中添加 {{#widget:BatchCreateEquipment}} </noinclude> <includeonly> <div id="batch-create-equipment"> <style> #equipment-creator { border: 1px solid #ccc; padding: 20px; margin: 20px 0; background: #f9f9f9; border-radius: 5px; } #equipment-list { width: 100%;…” |
无编辑摘要 |
||
| 第115行: | 第115行: | ||
<script> | <script> | ||
// 等待MediaWiki模块加载 | |||
(function() { | (function() { | ||
var isCreating = false; | // 确保MediaWiki API可用 | ||
function initBatchCreator() { | |||
if (typeof mw === 'undefined' || !mw.Api) { | |||
setTimeout(initBatchCreator, 100); | |||
return; | |||
} | |||
var isCreating = false; | |||
var stopRequested = false; | |||
var api = new mw.Api(); | |||
var editToken = null; | |||
// 页面内容模板 | |||
function getPageContent(pageName) { | |||
return '{{面包屑|装备图鉴}}\n' + | |||
'<div style="display:flex;justify-content:center;align-items:center;flex-wrap:wrap;gap:5px">\n' + | |||
'{{#invoke:装备|displaypopup|{{PAGENAME}}|1}} {{#invoke:装备|displaypopup|{{PAGENAME}}|2}} {{#invoke:装备|displaypopup|{{PAGENAME}}|3}}{{#invoke:装备|displaypopup|{{PAGENAME}}|4}} {{#invoke:装备|displaypopup|{{PAGENAME}}|5}}\n' + | |||
'</div>\n' + | |||
'[[分类:装备]]'; | |||
} | |||
function addLog(message, type) { | |||
var logDiv = document.getElementById('create-log'); | |||
if (!logDiv) return; | |||
logDiv.style.display = 'block'; | |||
var logEntry = document.createElement('div'); | |||
logEntry.className = 'log-' + type; | |||
var timestamp = new Date().toLocaleTimeString(); | |||
logEntry.textContent = '[' + timestamp + '] ' + message; | |||
logDiv.appendChild(logEntry); | |||
logDiv.scrollTop = logDiv.scrollHeight; | |||
} | |||
function updateProgress(current, total) { | |||
var progressBar = document.getElementById('progress-bar'); | |||
var progressFill = document.getElementById('progress-fill'); | |||
if (!progressBar || !progressFill) return; | |||
progressBar.style.display = 'block'; | |||
var percentage = (current / total) * 100; | |||
progressFill.style.width = percentage + '%'; | |||
} | |||
function sleep(ms) { | |||
return new Promise(resolve => setTimeout(resolve, ms)); | |||
} | |||
// 获取编辑令牌 | |||
async function getEditToken() { | |||
if (editToken) return editToken; | |||
try { | |||
var result = await api.get({ | |||
action: 'query', | |||
meta: 'tokens', | |||
type: 'csrf', | |||
format: 'json' | |||
}); | |||
editToken = result.query.tokens.csrftoken; | |||
return editToken; | |||
} catch (error) { | |||
console.error('获取编辑令牌失败:', error); | |||
throw error; | |||
} | } | ||
} | } | ||
async function checkPageExists(title) { | |||
try { | |||
var result = await api.get({ | |||
action: 'query', | |||
titles: title, | |||
format: 'json' | |||
}); | |||
var pages = result.query.pages; | |||
for (var pageId in pages) { | |||
return pageId !== '-1' && !pages[pageId].missing; | |||
} | |||
return false; | |||
} catch (error) { | |||
console.error('检查页面存在性失败:', error); | |||
return false; | |||
} | |||
} | } | ||
async function createPage(title, content) { | |||
try { | |||
var token = await getEditToken(); | |||
var result = await api.post({ | |||
action: 'edit', | |||
title: title, | |||
text: content, | |||
summary: '批量创建装备页面', | |||
createonly: true, | |||
token: token, | |||
format: 'json' | |||
}); | |||
return { success: true, result: result }; | |||
} catch (error) { | |||
return { success: false, error: error }; | |||
} | |||
} | } | ||
var | async function startBatchCreate() { | ||
if (isCreating) return; | |||
var equipmentList = document.getElementById('equipment-list').value.trim(); | |||
if (!equipmentList) { | |||
alert('请输入装备名称列表'); | |||
return; | |||
} | |||
var equipments = equipmentList.split('\n') | |||
.map(name => name.trim()) | |||
.filter(name => name); | |||
if (equipments.length === 0) { | |||
alert('没有有效的装备名称'); | |||
return; | |||
} | |||
isCreating = true; | |||
stopRequested = false; | |||
editToken = null; // 重置令牌 | |||
document.getElementById('start-create').style.display = 'none'; | |||
document.getElementById('stop-create').style.display = 'inline-block'; | |||
document.getElementById('stop-create').disabled = false; | |||
document.getElementById('stop-create').textContent = '停止创建'; | |||
document.getElementById('equipment-list').disabled = true; | |||
var skipExisting = document.getElementById('skip-existing').checked; | |||
var delay = parseInt(document.getElementById('create-delay').value) || 1000; | |||
addLog('开始批量创建装备页面,共 ' + equipments.length + ' 个', 'info'); | |||
var successCount = 0; | |||
var skipCount = 0; | |||
var errorCount = 0; | |||
var | for (var i = 0; i < equipments.length; i++) { | ||
if (stopRequested) { | |||
addLog('用户停止了创建过程', 'info'); | |||
break; | |||
} | |||
var equipmentName = equipments[i]; | |||
updateProgress(i + 1, equipments.length); | |||
try { | |||
// 检查页面是否存在 | |||
if (skipExisting) { | |||
var exists = await checkPageExists(equipmentName); | |||
if (exists) { | |||
addLog('页面 "' + equipmentName + '" 已存在,跳过', 'info'); | |||
skipCount++; | |||
await sleep(Math.min(delay / 2, 500)); | |||
continue; | |||
} | |||
} | |||
// 创建页面 | |||
var content = getPageContent(equipmentName); | |||
var result = await createPage(equipmentName, content); | |||
if (result.success) { | |||
addLog('成功创建页面: ' + equipmentName, 'success'); | |||
successCount++; | |||
} else { | |||
if (result.error && result.error.code === 'articleexists') { | |||
addLog('页面 "' + equipmentName + '" 已存在', 'info'); | |||
skipCount++; | |||
} else { | |||
var errorMsg = result.error ? | |||
(result.error.info || result.error.code || '未知错误') : | |||
'未知错误'; | |||
addLog('创建页面 "' + equipmentName + '" 失败: ' + errorMsg, 'error'); | |||
errorCount++; | |||
} | |||
} | |||
} catch (error) { | |||
addLog('处理页面 "' + equipmentName + '" 时出错: ' + error.message, 'error'); | |||
errorCount++; | errorCount++; | ||
} | |||
// 延迟下一次请求 | |||
if (i < equipments.length - 1 && !stopRequested) { | |||
await sleep(delay); | |||
} | } | ||
} | } | ||
// | addLog('批量创建完成!成功: ' + successCount + ', 跳过: ' + skipCount + ', 失败: ' + errorCount, 'info'); | ||
isCreating = false; | |||
} | document.getElementById('start-create').style.display = 'inline-block'; | ||
document.getElementById('stop-create').style.display = 'none'; | |||
document.getElementById('equipment-list').disabled = false; | |||
} | |||
// 绑定事件 | |||
var startBtn = document.getElementById('start-create'); | |||
if (startBtn) { | |||
startBtn.addEventListener('click', startBatchCreate); | |||
} | |||
var stopBtn = document.getElementById('stop-create'); | |||
if (stopBtn) { | |||
stopBtn.addEventListener('click', function() { | |||
stopRequested = true; | |||
this.disabled = true; | |||
this.textContent = '正在停止...'; | |||
}); | |||
} | } | ||
var clearBtn = document.getElementById('clear-log'); | |||
if (clearBtn) { | |||
clearBtn.addEventListener('click', function() { | |||
var log = document.getElementById('create-log'); | |||
var progressBar = document.getElementById('progress-bar'); | |||
var progressFill = document.getElementById('progress-fill'); | |||
if (log) { | |||
log.innerHTML = ''; | |||
log.style.display = 'none'; | |||
} | |||
if (progressBar) { | |||
progressBar.style.display = 'none'; | |||
} | |||
if (progressFill) { | |||
progressFill.style.width = '0'; | |||
} | |||
}); | |||
} | |||
addLog('装备批量创建工具已就绪', 'info'); | |||
} | } | ||
// | // 开始初始化 | ||
if (document.readyState === 'loading') { | |||
document.addEventListener('DOMContentLoaded', initBatchCreator); | |||
} else { | |||
initBatchCreator(); | |||
} | |||
} | |||
})(); | })(); | ||
</script> | </script> | ||
</div> | </div> | ||
</includeonly> | </includeonly> | ||
2025年10月18日 (六) 09:51的最新版本
这是一个用于批量创建装备页面的Widget。 使用方法:在任何页面中添加
批量创建装备页面
(建议设置1000ms以上,避免请求过快)