UserScript/mcbbs/server-bump.user.js

97 lines
3.6 KiB
JavaScript

// ==UserScript==
// @name 圈云盒子顶帖器
// @namespace https://circlecloud.ltd/
// @version 0.1.7
// @description 自动顶贴
// @author MiaoWoo
// @match https://www.mcbbs.net/forum-server-1.html**
// @run-at document-start
// @grant none
// ==/UserScript==
/* global jq */
(function () {
'use strict'
let store = window.localStorage
let nextPage = undefined
async function sleep(time) {
return new Promise((resolve, reject) => setTimeout(resolve, time))
}
async function waitNextPage() {
nextPage.click()
await sleep(200)
while (nextPage.innerText != "下一页 »") {
await sleep(100)
}
}
async function readServers() {
let servers = jq('td.icn').splice(2)
return servers.map(function (server) {
let serverInfo = {}
let line = server.nextElementSibling
serverInfo.tid = parseInt(line.children[0].id.split('_')[1])
serverInfo.title = line.children[3].innerText
serverInfo.view = line.parentNode.children[3].children[1].innerText
let timeInfo = line.parentNode.children[4].children[1].children[0].children[0]
if (timeInfo) {
serverInfo.lastpost_time = timeInfo.title
serverInfo.topmost = 0
} else {
serverInfo.lastpost_time = line.parentNode.children[4].children[1].children[0].innerText
serverInfo.topmost = 1
}
return serverInfo
})
}
async function post(method, data) {
return await fetch('https://reward.yumc.pw/mcbbs/' + method, {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}).then(r => r.json())
}
async function main() {
await sleep(3000)
//feat: auto refresh when mcbbs return 504
let errorh1 = document.getElementsByTagName('h1')[0]
if (errorh1 && errorh1.innerText.startsWith(50)) {
return refresh()
}
let taskId = setTimeout(refresh, 60000)
let params = location.href.split('?')[1]?.split('&').map(s => s.split('=')).reduce((pre, cur, index) => { pre[cur[0]] = cur[1]; return pre }, {}) || {}
nextPage = document.getElementById('autopbn')
let servers = await readServers()
post('alive', { type: 'servers' })
let lastPostServer = store.lastPostServer && JSON.parse(store.lastPostServer) || {}
if (lastPostServer.first != servers[0].tid
|| lastPostServer.five != servers[5].tid
|| lastPostServer.ten != servers[10].tid
|| lastPostServer.twenty_five != servers[25].tid
) {
await post('updateServers', servers)
console.log('数据上报完成!')
}
document.getElementById('scbar_txt').value = "数据上报完成!"
store.lastPostServer = JSON.stringify({
first: servers[0].tid,
five: servers[5].tid,
ten: servers[10].tid,
twenty_five: servers[25].tid
})
if (!params.autoRefresh) { return clearTimeout(taskId) }
console.log('已开启自动刷新!')
}
async function refresh() {
try {
await fetch('/api/mobile/index.php?version=4&module=credit')
location.href = 'https://www.mcbbs.net/forum-server-1.html?autoRefresh=true'
} catch (error) {
setTimeout(refresh, 5000)
}
}
window.alert = function (...args) { console.log(args) }
main()
})()