微件:getMCBBSCredit:修订间差异

添加1,971字节 、​ 2020年11月15日 (星期日)
无编辑摘要
(将会绘制在一个id为userpie的元素里;uid从用户模板获取,如果没有,则尝试从#userpie里获取)
 
无编辑摘要
(function () {
// 添加highcharts开源库
addScript('https://cdn.staticfile.org/highcharts/8.2.2/highcharts.min.js')
var scr = document.createElement('script')
scr.src = addScript('https://cdn.staticfile.org/highcharts/8.2.2/highchartsmodules/exporting.min.js')
document.head.appendChild(scr)
window.addEventListener('load', () => {
let uid = getUID(); if (!uid || isNaN(uid)) { console.log('未获取到MCBBSUID'); return }
// let el = document.querySelector('#mw-content-text .mw-parser-output')
// let n = document.createElement('div'); n.id = 'userpie'; el.appendChild(n)
getPIE(uid)
})
function getUID() {
let el = document.querySelector('.infolist')
// console.log(el)
if (el) {
let tr = el.querySelectorAll('tr')
// console.log(tr)
for (let i = 0; i < tr.length; i++) {
// console.log(tr[i])
// console.log(tr[i].textContent.indexOf('UID'))
if (tr[i].childElementCount < 2) { continue }
if (tr[i].textContent.replace('\n', '').indexOf('UID') == 0) {
}
el = document.querySelector('#userpie')
// console.log(el)
if (el) {
return el.textContent.match(/\d+/)[0]
}
}
let retry = 1 // 重试次数
function getPIE(uid) {
let $url = 'https://www.mcbbs.net/api/mobile/index.php?module=profile&uid=' + uid; console.log('正在获取用户信息: ' + $url)
fetch('https://proxy.onesrc.workers.dev/ajax/' + $url).then(response => response.text()).then((res) => {
//console.logthen(res)response => {
let data = JSON.parse if (resresponse.ok) {
let space = data.Variables return response.spacejson()
//let credits = space.credits; } else //总积分{
let post = space.posts; return Promise.reject(Object.assign({}, json, //回帖{
let thread = space.threads; //主题status: response.status,
statusText: response.statusText
let digestpost = space.digestposts; //精华
let popular = space.extcredits1; //人气 }))
let contrib = space.extcredits6; //贡献}
let kindnes = space.extcredits7; //爱心})
let diamond.then((data) => space.extcredits8; //钻石{
let uname = space console.usernamelog(data); window.res = data
var json let space = {};data.Variables.space
json. let credits = {space.credits; //总积分
href:let 'https://mcbbs-wikipost = space.cnposts; /wiki/%E7%94%A8%E6%88%B7:Salt_lovely',回帖
text:let '小工具由Salt_lovely制作,使用了highcharts开源库'thread = space.threads; //主题
} let digestpost = space.digestposts; //精华
let digestpostpopular = space.digestpostsextcredits1; //精华人气
json.chart = { backgroundColor: '#fbf2da', plotShadow: false };
// let goldngt = space.extcredits2; //金粒
json.tooltip = { pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' };
json.title = { text: uname// +let '积分构成'goldigt }= space.extcredits3; //金锭
json.plotOptions // let emerald = {space.extcredits4; //绿宝石
pie:// {let nethers = space.extcredits5; //下界之星
let contrib = space.extcredits6; allowPointSelect: true, //贡献
let kindnes = space.extcredits7; cursor: 'pointer', //爱心
let diamond = space.extcredits8; //钻石
colors: ['#7ccade', '#cae07b', '#e37bf9', '#fce37c', '#ff9800', '#fd957e', '#9ba8f3'],
let uname = space.username dataLabels:|| {''; //用户名
let group = space.group.grouptitle || ''; enabled: true,//用户组
var json = format: '{point.name}: {point.y}分, 占{point.percentage:.1f} %',;
json.credits = {
href: 'https://mcbbs-wiki.cn/wiki/%E7%94%A8%E6%88%B7:Salt_lovely',
text: '小工具由Salt_lovely制作,使用了highcharts开源库'
// console.log(tr) }
json.chart = { backgroundColor: '#fbf2da', plotShadow: false };
json.tooltip = { pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' };
// 标题: <用户名>积分构成
json.title = { text: uname + ' 积分构成' };
// 副标题: UID: <UID> ; 积分: <总积分> ; 用户组: <用户组>
json.subtitle = { text: 'UID: ' + uid + ' ; 积分: ' + credits + ' ; 用户组: ' + group };
json.plotOptions = {
showInLegendpie: true{
allowPointSelect: true,
cursor: 'pointer',
colors: ['#7ccade', '#cae07b', '#e37bf9', '#fce37c', '#ff9800', '#fd957e', '#9ba8f3'],
dataLabels: {
enabled: true,
format: '{point.name}: {point.y}分, 占{point.percentage:.1f} %',
},
showInLegend: true
}
};
json.series = [{
type: 'pie',
name: '积分占比',
// console.log(tr data: [i])
{ name: '发帖数/' + post + '帖', y: Math.round(post / 3) },
{ name: '主题数/' + thread + '帖', y: thread * 2 },
{ name: '精华帖/' + digestpost + '帖', y: digestpost * 45 },
{ name: '人气/' + popular + '点', y: popular * 3 },
{ name: '贡献/' + contrib + '点', y: contrib * 10 },
{ name: '爱心/' + kindnes + '颗', y: kindnes * 4 },
{ name: '钻石/' + diamond + '颗', y: diamond * 2 },
]
}];
json.navigation = {
buttonOptions: {
enabled: true
},
showInLegend: true
}
}; Highcharts.setOptions({
json.series = [ lang: {
type contextButtonTitle: 'pie图表菜单',
name viewFullscreen: '积分占比'"全屏查看",
data exitFullscreen: ["退出全屏",
{ name: '发帖数/' + post + '帖', yprintChart: Math.round(post / 3) }"打印图表",
{ name: '主题数/' + thread +downloadJPEG: '帖',导出 y:JPEG thread * 2 }图片',
{ name: '精华帖/' + digestpost +downloadPDF: '帖',导出 y:PDF digestpost * 45 }文档',
{ name: '人气/' + popular +downloadPNG: '点',导出 y:PNG popular * 3 }图片',
{ name: '贡献/' + contrib +downloadSVG: '点',导出 y:SVG contrib * 10 },矢量图'
{ name: '爱心/' + kindnes + '颗', y: kindnes * 4 },
{ name: '钻石/' + diamond + '颗', y: diamond * 2 },)
]Highcharts.chart('userpie', json)
}];)
Highcharts.chartcatch('userpie',error json)=> {
} console.log(error);
if (retry > 0) {
// console.log(el'重试...')
retry--
setTimeout(getPIE, 0)
}
});
}
function addScript(src, asynchronous = false) {
var let scr = document.createElement('script')
scr.src = src
scr.async = asynchronous
document.head.appendChild(scr)
}
})()