微件:getMCBBSCredit:修订间差异

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