find cheat

· · 个人记录

请在发工单时加入如下内容:

使用 [工具](https://www.luogu.com.cn/article/92py95rq) 辅助检查,均经过人工复核。

// ==UserScript==
// @name         Luogu Find Cheater
// @namespace    https://www.luogu.com.cn/user/841039
// @version      2026-2-9
// @author       wanganze
// @description  luogu helper
// @icon         https://fecdn.luogu.com.cn/favicon.ico
// @match        *://*.luogu.com.cn/*
// @match        *://*.luogu.org/*
// @match        *://*.luogu.com/*
// @require      https://cdn.luogu.com.cn/js/jquery-2.1.1.min.js
// @grant        unsafeWindow
// @run-at       document-end
// @namespace    http://tampermonkey.net/
// ==/UserScript==
let $=window.jQuery;
class FetchRateLimiter
{
    constructor(limit)
    {
        // limit 以毫秒为单位,表示相邻两次 fetch 操作间的最小间隔
        this.limit = limit;
        this.queue = [];
        this.queuePrior = [];
        this.active = false;
        this.requestCache = new Map();
    }
    async process()
    {
        this.active = 1;
        let data, url,isp = 0;
        if(this.queuePrior.length > 0){
            ({url,data} = this.queuePrior.shift());
            isp = 1;
        }
        else if(this.queue.length > 0){
            ({url,data} = this.queue.shift());
        }
        else{
            this.active = 0;
            return;
        }

        try{
            console.log("fetch "+isp+" "+url);
            let r = await $.ajax(url,data)
        }
        catch(error){console.log("fetch error",error);}
        setTimeout(this.process.bind(this), this.limit);
    }
    push(url,data,prior=false)
    {
        if(this.requestCache.has({url,data})) return this.requestCache.get({url,data});
        const request = new Promise((resolve, reject) => {
            data.success = resolve;
            data.error=reject;
            if(prior) this.queuePrior.push({url, data});
            else this.queue.push({url, data});
            let res;
            if(!this.active)
            {
                this.process();
            }
        }).then((res)=>{
            this.requestCache.set({url,data},res);
            return res;
        });
        return request;
    }
}
const limiter = new FetchRateLimiter(500);
async function get_context(url)
{
    if(url == location.pathname && unsafeWindow._feInstance) return;
    console.log("get_context:"+url);
    try{
        let csrf = document.querySelector("meta[name=csrf-token]").content;
        let data = await limiter.push(url,{
            type: "GET",
            headers: {
                "x-luogu-type": "content-only",
                "Accept": "application/json, text/plain, */*",
                "x-lentille-request": "content-only"
            },
        });
        return data;
    } catch (error) {
        console.error('Error geting context:', error);
        console.log("error on",url);
        return;
    }
}
unsafeWindow.get_context = get_context;
function get_code(markdown)
{
    let codes = [];
    let lst = 0,flag = 0;
    let mkd = markdown + '```';
    while(mkd.indexOf("```",lst) != -1)
    {
        let cur = mkd.indexOf("```",lst);
        if(flag)
        {
            let res = mkd.slice(mkd.indexOf("\n",lst),cur - 1);
            codes.push(res)
        }
        lst = cur + 3;
        flag ^= 1;
    }
    return codes;
}
function change(code)
{
    let lst = 0,flag = 0;
    let del = [];
    while(flag == 0 && code.indexOf("/*",lst) != -1 || flag == 1 && code.indexOf("*/",lst) != -1)
    {
        let cur;
        if(flag == 0) cur = code.indexOf("/*",lst);
        else cur = code.indexOf("*/",lst);
        if(flag)
        {
            del.push({s:lst - 2,t:cur + 1});
        }
        lst = cur + 2;
        flag ^= 1;
    }
    let tmp = "";
    for(let i = 0; i < code.length; i ++)
    {
        let f = 1;
        for(let j = 0; j < del.length; j ++)
        {
            if(j < del.length && i >= del[j].s && i <= del[j].t)
            {
                f = 0;
            }
        }
        if(f)
        {
            tmp += code[i];
        }
    }
    let lines = tmp.split(/\r?\n/);
    let reallines = [];
    let t = 0;
    for(let s of lines)
    {
        if(t) reallines.push(s);
        else reallines[reallines.length - 1] += s;
        if(s[s.length - 1] == '\\') t = 1;
        else t = 0;
    }
    let res = "";
    for(let s of lines)
    {
        //console.log(s);
        if(s.search("#") != -1) continue;
        if(s.search("//") != -1) s = s.substring(0,Math.max(0,s.search("//") - 1));
        for(let x of s)
        {
            if(x > ' ' && x != '{' && x != '}' && x !=';' && x !=',') res += x;
        }
    }
    return res;
}
unsafeWindow.change = change;
async function levenshtein(a, b) {
    const matrix = [];
    let i, j;
    for (i = 0; i <= b.length; i++) {
        matrix[i] = [i];
    }
    for (j = 0; j <= a.length; j++) {
        matrix[0][j] = j;
    }
    for (i = 1; i <= b.length; i++) {
        for (j = 1; j <= a.length; j++) {
            if (b.charAt(i - 1) === a.charAt(j - 1)) {
                matrix[i][j] = matrix[i - 1][j - 1];
            } else {
                matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, Math.min(matrix[i][j - 1] + 1, matrix[i - 1][j] + 1));
            }
        }
    }
    return matrix[b.length][a.length];
}
unsafeWindow.cmp = similar;
let dis;
async function similar(code,std)
{
    let lim = Math.min(200,0.1 * Math.max(code.length,std.length));
    let code1 = await change(code);
    let std1 = await change(std);
    //console.log("len",code.length,std.length);
    if(Math.abs(code1.length - std1.length) > lim) return;
    //console.log(code1);
    //console.log(std1);
    dis = await levenshtein(code1,std1);
    console.log(dis,lim);
    if(dis <= lim) return 1;
    return 0;
}
async function getsolutions(pid)
{
    let allcodes = [];
    let all = await get_context("https://www.luogu.com.cn/problem/solution/" + pid + "/");
    console.log(all);
    let cnt = all.data.solutions.count,per = all.data.solutions.perPage;
    for(let i = 0,page = 1; i < cnt; i += per,page ++)
    {
        let res = (await get_context("https://www.luogu.com.cn/problem/solution/" + pid + "/?page=" + page)).data;
        for(let k of res.solutions.result)
        {
            let codes = await get_code(k.content);
            allcodes.push({lid:k.lid,codes:codes});
            //console.log(codes);
        }
        /*
        for(let j = i; j < Math.min(cnt,i + per); j ++)
        {

        }
        */
    }
    console.log(allcodes);
    return allcodes;
}
unsafeWindow.check = check_record
async function check_record()
{
    console.log(unsafeWindow._feInstance.currentData);
    let pid = unsafeWindow._feInstance.currentData.record.problem.pid;
    let code = unsafeWindow._feInstance.currentData.record.sourceCode;
    let sols = await getsolutions(pid);
    for(let x of sols)
    {
        let flag = 0;
        for(let y of x.codes)
        {
            if((await similar(code,y)) == 1)
            {
                flag = 1;
                console.log(code,y);
                console.log(dis);
                console.log(pid,"https://www.luogu.com.cn/article/"+x.lid,location.href);
            }
        }
    }
}
unsafeWindow.find = get_user
async function get_user(l,r)
{
    if(l == null && r == null)
    {
        let intro = (await get_context(location.href));
        if(intro == null) return;
        let list = intro.data.dailyCounts;
        console.log(list);
        for(let x in list)
        {
            if(list.hasOwnProperty(x))
            {
                if(list[x][0]>30 && list[x][1] > 2) alert(' '+x + ' ' + list[x]);
            }
        }
    }
    for(let i = l; i <= r; i ++)
    {
        let intro = (await get_context("https://www.luogu.com.cn/user/"+i));
        if(intro == null) continue;
        let list = intro.data.dailyCounts;
        console.log(list);
        for(let x in list)
        {
            if(list.hasOwnProperty(x))
            {
                if(list[x][0]>30 && list[x][1] > 2) alert(i+' '+x + ' ' + list[x]);
            }
        }
    }
}
(function() {
    'use strict';

//getsolutions("P1001");
    // Your code here...
})();