1
0
Fork 0
UserScript/mcbbs/server-bump.user.js

97 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-12-28 09:28:34 +00:00
// ==UserScript==
// @name 圈云盒子顶帖器
// @namespace https://circlecloud.ltd/
2021-09-03 08:09:39 +00:00
// @version 0.1.7
2020-12-28 09:28:34 +00:00
// @description 自动顶贴
// @author MiaoWoo
2021-03-13 06:12:34 +00:00
// @match https://www.mcbbs.net/forum-server-1.html**
2021-06-11 02:42:18 +00:00
// @run-at document-start
2020-12-28 09:28:34 +00:00
// @grant none
// ==/UserScript==
/* global jq */
(function () {
'use strict'
2021-03-13 06:12:34 +00:00
let store = window.localStorage
2020-12-28 09:28:34 +00:00
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
2021-03-13 06:12:34 +00:00
serverInfo.view = line.parentNode.children[3].children[1].innerText
2020-12-28 09:28:34 +00:00
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() {
2021-06-11 02:42:18 +00:00
await sleep(3000)
2021-03-17 11:53:48 +00:00
//feat: auto refresh when mcbbs return 504
2021-09-03 08:09:39 +00:00
let errorh1 = document.getElementsByTagName('h1')[0]
if (errorh1 && errorh1.innerText.startsWith(50)) {
return refresh()
}
2021-06-11 02:42:18 +00:00
let taskId = setTimeout(refresh, 60000)
2021-03-13 06:12:34 +00:00
let params = location.href.split('?')[1]?.split('&').map(s => s.split('=')).reduce((pre, cur, index) => { pre[cur[0]] = cur[1]; return pre }, {}) || {}
2020-12-28 09:28:34 +00:00
nextPage = document.getElementById('autopbn')
let servers = await readServers()
post('alive', { type: 'servers' })
2021-03-17 11:53:48 +00:00
let lastPostServer = store.lastPostServer && JSON.parse(store.lastPostServer) || {}
2021-03-18 02:34:45 +00:00
if (lastPostServer.first != servers[0].tid
|| lastPostServer.five != servers[5].tid
2021-03-18 02:32:58 +00:00
|| lastPostServer.ten != servers[10].tid
|| lastPostServer.twenty_five != servers[25].tid
2021-03-17 11:53:48 +00:00
) {
2021-03-13 06:12:34 +00:00
await post('updateServers', servers)
2021-03-17 11:53:48 +00:00
console.log('数据上报完成!')
2021-03-13 06:12:34 +00:00
}
2021-09-03 08:09:39 +00:00
document.getElementById('scbar_txt').value = "数据上报完成!"
2021-03-17 11:53:48 +00:00
store.lastPostServer = JSON.stringify({
2021-03-18 02:34:45 +00:00
first: servers[0].tid,
2021-03-17 11:53:48 +00:00
five: servers[5].tid,
ten: servers[10].tid,
twenty_five: servers[25].tid
})
2021-06-11 02:42:18 +00:00
if (!params.autoRefresh) { return clearTimeout(taskId) }
2021-03-17 11:53:48 +00:00
console.log('已开启自动刷新!')
2021-03-13 06:12:34 +00:00
}
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)
}
2020-12-28 09:28:34 +00:00
}
2021-06-11 02:42:18 +00:00
window.alert = function (...args) { console.log(args) }
2020-12-28 09:28:34 +00:00
main()
})()