打开/关闭菜单
Toggle preferences menu
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

MediaWiki:Gadget-unziprecords.js

MediaWiki界面页面
Cirno.9讨论 | 贡献2022年12月6日 (二) 09:29的版本

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:Ctrl-F5
function unzip(b64Data) {
    var strData = atob(b64Data);
    strData = pako.ungzip(strData,{to: 'string'});
    return decodeURIComponent(strData);
}
//检查是否为刷题云储存页面
function isRecordsPages(){
    var url = window.location.href
    url = url.split('Records/')
    console.log(url)
    if(url.length>1){
        var id = url[1]
        return id
    }else{
        return false
    }
}

//获取某个页面的历史版本
function getPageRevision(url, title, pageCount){
    var PARAMS = {
        "action": "query",
        "format": "json",
        "prop": "revisions",
        "titles": title,
        "utf8": 1,
        "rvprop": "content",
        "rvlimit": pageCount,
        "rvdir": "older",
        "origin":"*"

    }
    var isSuccess = {}
    $.ajax(
        {
            url: url,
            method: "post",
            async: false,
            data: PARAMS,
            timeout: 5000,
            success: function (results){
                if('-1' in results.query.pages){
                    console.log('===无此页面===')
                    isSuccess = {isSuccess:false, result:results}
                }else {
                    isSuccess = {isSuccess:true, result:results}
                }
            }
        })
    return isSuccess
}
function getFirstPageContent(obj){
    obj = obj.result.query.pages
    for(var i in obj){
        obj = obj[i]
    }
    obj = obj.revisions[0]['*']
    return obj
}
//解析json
function parseRecordsJSON(url,content){
    var data
    var PARAMS = {
        "action": "parse",
        "format": "json",
        "text":content,
        "contentmodel": "json",
        "utf8": 1,
        "origin":"*"
    }
    $.ajax({
        async: true,
        type: "post",
        url: url,
        data: PARAMS,
        datatype: 'json',
        success: function (datas) {
            data =  datas['parse']['text']['*']
            // $('#mw-content-text').html(data)
        }
    });
    return data
}
function getFormatTime(timestamp, clean){
	clean = clean !== undefined ? clean: true
    var nowDate
    if(timestamp===undefined){
        nowDate= new Date();
    }else {
        nowDate = new Date(timestamp);
    }
    var year = nowDate.getFullYear();
    var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
    var date = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
    var hour = nowDate.getHours()< 10 ? "0" + nowDate.getHours() : nowDate.getHours();
    var minute = nowDate.getMinutes()< 10 ? "0" + nowDate.getMinutes() : nowDate.getMinutes();
    var second = nowDate.getSeconds()< 10 ? "0" + nowDate.getSeconds() : nowDate.getSeconds();
    var time = ''
    if(clean){
        time =  year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;
    }else {
        time =  year + "-" + month + "-" + date+"T"+hour+":"+minute+":"+second+'Z'
    }
    return time

}
function reModelData(id,data) {
    var container = $('<div id="'+id+'" class="data-container"></div>')
    var resultsContainer = $('<div class="results"></div>')
    container.append('<div><h2>'+data.info.title+'</h2><div><p class="inline">id:'+data.info.id+'</p>|<p class="inline">做题时间:'+getFormatTime(data.info.time)+'</p></div></div>')
    for(var i in data.results){
        // console.log(data.results[i])
        if(data.results[i].length>0) {
            container.append('<h3>' + i + '型题</h3>')
            for (var n = 0; n < data.results[i].length; n++) {
                console.log(i)
                var btn = $('<button id="' + id + '-' + i + '-' + n + '" class="btn btn-circle">' + (n+1) + '</button>')
                if(data.results[i][n].isDone && data.results[i][n].isRight){
                    btn.addClass('green')
                }else if(!data.results[i][n].isDone){
                    btn.addClass('yellow')
                }else {
                    btn.addClass('red')
                }
                btn.appendTo(resultsContainer)
                // console.log(n)
                // console.log(data.results[i][n])
            }
            resultsContainer.appendTo(container)
            resultsContainer = $('<div class="results"></div>')
        }
    }
    console.log(container)
    return container


}
//主函数
function unzipRecords(){
    var id = isRecordsPages()
    if(!id){
        return '非题库云储存页面!'
    }
    // var zippedContent = getFirstPageContent(getPageRevision('https://www.medforest.cn/medf/api.php','题库:Records/66',1))
    // console.log(zippedContent)
    var zippedContent= $('#mw-content-text').text().replace('[标记此页面为已巡查]','')
    $('#mw-content-text').html('<h4 style="color:red;">正在分析做题数据</h4>')
    console.log(zippedContent)
    var unzipped = unzip(zippedContent)
    var jsonData = JSON.parse(unzipped)
    console.log(jsonData.archive)
    var parsed=parseRecordsJSON('https://www.medforest.cn/medf/api.php',unzipped)
    var id = 0
    var analyzed = $('<div class="analyzed"></div>')
    for(var i in jsonData.archive){
        analyzed.prepend(reModelData(id,jsonData.archive[i]))
        id += 1
    }
    $('#mw-content-text').html('<div><h2>说明:</h2><p class="inline">数据按倒序排列,绿色为正确题目,红色错误,黄色未作答</p><hr></div>')
    $('#mw-content-text').append(analyzed)
}
 unzipRecords()