<div class="form-group" style="max-width:500px">
<label for="titles">标题</label>
<input id="titles" name="titles" class="form-control" placeholder="英文逗号或顿号分割"></input>
</div>
<div class="form-group" style="max-width:500px">
<label for="category">分类</label>
<input id="category" name="category" class="form-control"></input>
</div>
添加分类
移除分类
<div class="form-group" style="max-width:500px">
<label for="wikitext">wikitext</label>
<input id="wikitext" name="wikitext" class="form-control"></input>
</div>
<div class="checkbox">
<label>
<input id="isPrepend" type="checkbox"> 在页面内容之前插入
</label>
</div>
添加wikitext
<script>
var lock = false;
function editWikitext(mode){
if(lock) return false;
else lock = true
window.titleArray = $("#titles").val().replace(/、|,/g, ",").replace(/, /, ",").split(",")
window.category = $("#wikitext").val()
window.pageIndex = 0
window.editMode = mode
getPage()
}
function editCategories(mode){
if(lock) return false;
else lock = true
window.titleArray = $("#titles").val().replace(/、|,/g, ",").replace(/, /, ",").split(",")
window.category = "["+"[分类:" + $("#category").val() + "]]"
window.pageIndex = 0
window.editMode = mode
getPage()
}
function getPage(){
if(pageIndex >= titleArray.length){lock=false; return alert("刷新完毕")}
var isPrepend = $("#isPrepend").prop("checked")
new mw.Api().edit(
titleArray[pageIndex++],
function (revision) {
var text = revision.content,
hasCategory = text.indexOf(category) !== -1.
if(editMode === "add" && hasCategory) {
console.log(pageIndex-1, titleArray[pageIndex-1], "已有该分类,未操作")
return text;
}
if(editMode === "add") {
isPrepend ? text = category + "\n" + text : text += "\n" + category
} else if(editMode === "remove") text = text.replace(category, "")
return {
text: text,
summary: "批量修改分类",
minor: true
};
}
).then(function () {
console.log(pageIndex-1, titleArray[pageIndex-1], "操作成功")
$.get(mw.config.get("wgCookiePath") + titleArray[pageIndex-1])
setTimeout(getPage, 500)
}).catch(function (e) {
console.log(pageIndex-1, titleArray[pageIndex-1], "操作失败")
setTimeout(getPage, 500)
});
}
</script>