社区文档构建进行中,欢迎编辑。社区答疑群(非官方):717421103,点点小课堂(腾讯会议):5696651544
Widget:评论区管理/js
<script> class NewWikiCommentManager { sortBy = { by: "time", order: -1 } nb = new NBASE() ngb = new NGBASE() api = mw.config.values.wgScriptPath + "/api.php" uid = getCookie("DedeUserID") cache = { user: {} }
constructor(dom) { this.dom = $(dom) this.dom.data("NWCM", this) this.authenticate() .then(() => this.initPageList()) .then((res) => { this.page = res this.refresh() }) .catch(() => {
this.dom.before("
你无权使用这个功能。
")
this.dom.remove() }) }
authenticate() { return new Promise((resolve, reject) => { $.get(this.api, { action: "query", meta: "userinfo", uiprop: "groups", format: "json" }).done((result) => { if (result.query.userinfo.groups.indexOf("sysop") > -1) { resolve() } else { reject() } }) }) }
initPageList() { return new Promise((resolve, reject) => { $.get(this.api, { action: "query", list: "categorymembers", cmprop: "title", cmtitle: "分类:有评论区的页面", format: "json" }).done((result) => { let $pageList = this.dom.find(".pageList") result.query.categorymembers.forEach((e, i, a) => {
$pageList.append(`
`)
}) resolve(result.query.categorymembers[0].title) }) }) }
load() { return this.nb.read(`评论区/${this.page}`).catch((e) => { if (e.code === 201 || e.code === 202) { this.save([]) return Promise.resolve([]) } }) }
save(body) { if (!this.uid) { return $(".bili-game-header-user-noLogin a img").click() } return this.nb.save(`评论区/${this.page}`, body) }
refresh() { this.dom.find(`.pageList .item.active`).removeClass("active") this.dom.find(`.pageList .item[data-page="${this.page}"]`).addClass("active") this.load().then((res) => { this.show(res) }) }
show(data, dom = this.dom.find(".comment")) { dom.empty() let parentIndex = dom.closest("li").data("index") || [] data.forEach((e, i, a) => { let index = $.extend([], parentIndex) index.push(i)
let $li = $(`
this.getUser(e.uid).then((res) => { $avatar.append(``) $nickname.append(`${res.nickname}`) })
let $text = $(`$info.append($time)
let $index = $(`$info.append($index)
let $dataBox = $(`if (e.delete) {
$funBox.append($(`$li.addClass("deleted") } else {
$funBox.append($(`} $foot.append($info, $dataBox, $funBox) $left.append($avatar) $right.append($nickname, $text, $foot) $body.append($left, $right) $li.append($body)
let $reply = $(`- `)
$li.append($reply)
this.show(e.reply, $reply)
dom.prepend($li)
})
this.sort()
this.dom.removeClass("loading")
}
sort() {
let $comment = this.dom.find(".comment")
let $li = $comment.children("li")
$li.sort((a, b) => {
return NewWikiCommentManager.sortFun[this.sortBy.by](a, b) * this.sortBy.order
})
$comment.append($li)
let $option = this.dom.find(".option .sort")
$option.find(".active").removeClass("active")
$option.find(`[data-type="by"] [data-value="${this.sortBy.by}"]`).addClass("active")
$option.find(`[data-type="order"] [data-value="${this.sortBy.order}"]`).addClass("active")
}
getUser(uid) {
if (!this.cache.user[uid]) {
let sSUser = sessionStorage[`NWC_userCache_${uid}`]
if (sSUser) {
this.cache.user[uid] = Promise.resolve(JSON.parse(sSUser))
} else {
this.cache.user[uid] = this.ngb.read(`userInfo/${uid}`).catch(() => {
return new Promise((resolve, reject) => {
$.getJSON(`https://api.bilibili.com/x/space/acc/info?mid=${uid}`).done((result) => {
resolve({
avatar: result.data.face.replace(/^http:/, "https:"),
nickname: result.data.name
})
})
})
})
}
}
this.cache.user[uid].then((res) => {
sessionStorage[`NWC_userCache_${uid}`] = JSON.stringify(res)
})
return this.cache.user[uid]
}
formatTime(time = new Date()) {
let date = new Date(time)
let YYYY = date.getFullYear() + ""
let MM = (date.getMonth() + 1 + "").padStart(2, "0")
let DD = (date.getDate() + "").padStart(2, "0")
let hh = (date.getHours() + "").padStart(2, "0")
let mm = (date.getMinutes() + "").padStart(2, "0")
let ss = (date.getSeconds() + "").padStart(2, "0")
return `${YYYY}/${MM}/${DD} ${hh}:${mm}:${ss}`
}
delete(index) {
this.load().then((body) => {
let target = this.findTarget(body, index)
target.delete = true
this.save(body).then(() => {
this.show(body)
}).catch(() => {
this.delete(index)
})
})
}
restore(index) {
this.load().then((body) => {
let target = this.findTarget(body, index)
target.delete = false
this.save(body).then(() => {
this.show(body)
}).catch(() => {
this.restore(index)
})
})
}
findTarget(body, index) {
let target = body
for (let i = 0; i < index.length - 1; i++) {
target = target[index[i]].reply
}
return target[index[index.length - 1]]
}
static sortFun = {
time(a, b) {
return new Date($(a).find(".time").html()) - new Date($(b).find(".time").html())
},
like(a, b) {
return $(a).find(`.dataBox [data-type="like"]`).html() - $(b).find(`.dataBox [data-type="like"]`).html()
},
}
}
function getCookie(key) {
let result = {}
document.cookie.split(/ *; */).forEach((e, i, a) => {
let kv = e.split("=")
result[kv[0]] = kv[1]
})
if (key) {
result = result[key]
}
return result
}
(window.RLQ = window.RLQ || []).push(["jquery", () => {
$.getScript("/data/index.php?title=MediaWiki:Api.js&action=raw").done(() => {
window.NWCM = new NewWikiCommentManager("#NewWikiCommentManager")
})
$(document).on("click", `#NewWikiCommentManager .pageList .item`, function () {
let nwcm = $(this).closest("#NewWikiCommentManager").data("NWCM")
if ($(this).attr("data-page") === nwcm.page) {
return
}
nwcm.dom.addClass("loading")
nwcm.page = $(this).attr("data-page")
nwcm.refresh()
})
$(document).on("click", `#NewWikiCommentManager .option .toggle [data-value]`, function () {
let $dom = $(this).closest("#NewWikiCommentManager")
$dom.toggleClass($(this).attr("data-value"))
})
$(document).on("click", `#NewWikiCommentManager .option .sort [data-type] [data-value]`, function () {
let nwcm = $(this).closest("#NewWikiCommentManager").data("NWCM")
let type = $(this).parent().attr("data-type")
nwcm.sortBy[type] = $(this).attr("data-value")
nwcm.sort()
})
$(document).on("click", `#NewWikiCommentManager .funBox [data-action="delete"]`, function () {
let nwcm = $(this).closest("#NewWikiCommentManager").data("NWCM")
nwcm.dom.addClass("loading")
nwcm.delete($(this).closest("li").data("index"))
})
$(document).on("click", `#NewWikiCommentManager .funBox [data-action="restore"]`, function () {
let nwcm = $(this).closest("#NewWikiCommentManager").data("NWCM")
nwcm.dom.addClass("loading")
nwcm.restore($(this).closest("li").data("index"))
})
}])
</script>