洛谷比赛记录导出工具

· · 个人记录

function Stack(){
    this.items =[]
    Stack.prototype.push = function(element) {
        this.items.push(element)
    }
    Stack.prototype.pop = () => {
        return this.items.pop()
    }
    Stack.prototype.peek = () => {
        return this.items[this.items.length - 1]
    }
    Stack.prototype.isEmpty = () => {
        return this.items.length == 0 
    }
    Stack.prototype.size = () => {
        return this.items.length
    }
    Stack.prototype.toString = () => {
        let resultString = ''
        for (let i of this.items){
            resultString += i + ' '
        }
        return resultString
    }
}
window.loadJQuery = function(){
    var target = document.createElement("script");
    target.src = "https://cdn.luogu.com.cn/js/jquery-2.1.1.min.js";
    target.async = false;
    document.head.appendChild(target);
}
window.Sleep = function(delay){
    var start = (new Date()).getTime();
    while((new Date()).getTime() - start < delay) {
        continue;
    }
}
window.buffer = new Stack();
window.RecordExporter = function(pid, contest, page){
    var url = "https://www.luogu.com.cn/record/list";
    for(var p = 1; p <= page; p++){
        var res = $.ajax({
            url: url,
            method: 'GET',
            async: false,
            data: {"pid": pid, "contestId": contest, "page": p}
        }).responseText;
        if(page % 10 == 0){
            window.Sleep(500);
        }
        const parser = new DOMParser();
        const dom = parser.parseFromString(res, "text/html");
        var scripts = dom.getElementsByTagName('script');
        var target = document.createElement("script");
        target.innerHTML = scripts[0].innerHTML;
        target.async = false;
        document.head.appendChild(target);
        for(var i = 1; i < window._feInjection.currentData.records.result.length; i++)
            window.buffer.push(window._feInjection.currentData.records.result[i]);
    }
    var output = "";
    while(!window.buffer.isEmpty()){
        var res = window.buffer.peek(), Time = new Date();
        Time.setTime(res.submitTime * 1000);
        output += Time.toLocaleString()
               + " " + res.user.uid + " " + res.user.name + " " + res.score + "\n";
        window.buffer.pop();
    }
    console.log(output);
}

使用方法同私信导出工具。