跳到正文

科技·工程

AI 轻松破解信某队禁止复制的封锁

Ferm_Tawn约 15 分钟
原文
文章信息与版本

文章信息

发布
2026年7月21日
收录
2026年7月28日
更新
2026年11月16日
篇幅
约 15 分钟

版本

  1. v1当前

本文及插件使用 Chatgpt 辅助完成。

信某队题面复制助手:让题目整理更简单

在刷信某队题目时,相信很多同学遇到过这样的麻烦:

  • 题面无法复制;
  • 数学公式复制后变成乱码;
  • n、a_i 等 LaTeX 公式丢失;
  • 代码高亮后复制格式混乱;
  • 复制到 Markdown 编辑器后需要重新整理。

针对这些问题,我开发了 信某队题面复制助手。

功能介绍

1. 一键复制完整题面

插件可以解除网页复制限制,让题面可以正常选择、复制。

复制后的内容不是简单的纯文本,而是自动整理为:

代码
Markdown + LaTeX

例如网页中的:

给定一棵 n 个点的树

复制后会变成:

Markdown
给定一棵 $n$ 个点的树

公式可以直接用于 Typora、Obsidian、博客等 Markdown 编辑器。


2. 自动恢复数学公式

信某队题面中的数学公式通常使用 LaTeX 渲染。

插件会自动识别公式,将:

代码
𝑂(𝑛log𝑛)

恢复为:

latex
$O(n\log n)$

避免复制后公式错乱。


3. 代码复制优化

对于题面中的代码、样例程序,插件会自动保留代码格式。

例如:

C++
int main(){
    return 0;
}

复制后仍然保持代码块结构。


实现原理

插件不会依赖固定关键词判断题面,而是直接解析网页中的内容。

它会:

  1. 获取用户选中的内容;
  2. 遍历网页 DOM;
  3. 识别普通文字、标题、代码、数学公式;
  4. 转换成 Markdown;
  5. 写入剪贴板。

这样即使题面格式发生变化,也可以正常工作。


使用效果

安装插件后,在信某队题面页面:

  1. 选中需要的内容;
  2. 按 Ctrl+C;
  3. 此时你复制的内容就是处理好的文本了。

即可获得格式完整的题面。


总结

信某队题面复制助手的目标很简单:

让整理题面不再浪费时间,把精力放在算法和思考上。

无论是写题解、整理笔记,还是保存题目资料,都可以更加方便。

示例:

++ Ctrl C ++ Ctrl V ==。

++ Ctrl C ++ Ctrl V ==。

使用方法(以谷歌浏览器为例):

创建文件夹,创建 .js 和 .json 文件,命名为 manifest.json 和 content.js。在 Google Chrome 中,点击扩展程序,点击管理扩展程序,打开开发者模式,点击 加载已解压的扩展程序 按钮,选择文件夹。

manifest.json:

JSON
{
    "manifest_version":3,
    "name":"OJ Copy Helper",
    "version":"6.0",
    "description":"OJ题面Markdown LaTeX代码复制助手",
    "content_scripts":[
        {
            "matches":[
                "*://*.xinyoudui.com/*"
            ],
            "js":[
                "content.js"
            ],
            "run_at":"document_start"
        }
    ]
}

content.js:

JavaScript
(function(){

"use strict";


// =========================
// 解除复制限制
// =========================


function unlock()
{

    let style=document.createElement("style");

    style.innerHTML=`

*{
 user-select:text!important;
 -webkit-user-select:text!important;
 -moz-user-select:text!important;
}

pre,code,textarea{
 user-select:text!important;
}

`;

    document.documentElement.appendChild(style);



    [
        "contextmenu",
        "selectstart"
    ].forEach(type=>{


        document.addEventListener(
            type,
            e=>{
                e.stopImmediatePropagation();
            },
            true
        );


    });


}



unlock();





// =========================
// 判断公式节点
// =========================


function getLatex(node)
{


    if(!node.tagName)
        return null;


    let tag=
    node.tagName.toLowerCase();



    // MathJax3

    if(tag==="mjx-container")
    {

        let a=node.querySelector(
            "annotation[encoding='application/x-tex']"
        );


        if(a)
            return a.textContent.trim();

    }




    // MathJax2

    if(
        node.classList &&
        node.classList.contains("MathJax")
    )
    {

        let a=node.querySelector(
            "annotation[encoding='application/x-tex']"
        );


        if(a)
            return a.textContent.trim();

    }




    // KaTeX

    if(
        node.classList &&
        node.classList.contains("katex")
    )
    {

        let a=node.querySelector(
            "annotation"
        );


        if(a)
            return a.textContent.trim();

    }





    // MathML

    if(tag==="math")
    {

        return node.textContent.trim();

    }


    return null;

}







// =========================
// 判断代码
// =========================


function getCode(node)
{

    if(!node.tagName)
        return null;


    let tag=
    node.tagName.toLowerCase();



    if(tag==="pre")
    {

        return node.innerText;

    }



    if(
        tag==="code" &&
        node.parentElement &&
        node.parentElement.tagName
        .toLowerCase()==="pre"
    )
    {

        return node.innerText;

    }





    // Monaco

    if(
        node.classList &&
        (
        node.classList.contains("view-lines")
        ||
        node.classList.contains("monaco-editor")
        )
    )
    {

        return node.innerText;

    }




    // CodeMirror

    if(
        node.classList &&
        node.classList.contains(
            "CodeMirror-code"
        )
    )
    {

        return node.innerText;

    }



    return null;

}






// =========================
// DOM -> Markdown
// =========================


function parse(node)
{


    //文字

    if(node.nodeType===Node.TEXT_NODE)
    {
        return node.textContent;
    }



    if(node.nodeType!==Node.ELEMENT_NODE)
        return "";




    // 当前节点公式

    let latex=getLatex(node);


    if(latex)
    {
        return "$"+latex+"$";
    }




    // 当前节点代码

    let code=getCode(node);


    if(code)
    {

        return "\n```\n"
        +
        code
        +
        "\n```\n";

    }





    let tag=
    node.tagName.toLowerCase();




    if(tag==="br")
        return "\n";





    if(/^h[1-6]$/.test(tag))
    {

        let level=
        Number(tag[1]);


        return "\n"
        +
        "#".repeat(level)
        +
        " "
        +
        children(node).trim()
        +
        "\n\n";

    }






    if(tag==="li")
    {

        return "- "
        +
        children(node).trim()
        +
        "\n";

    }






    if(tag==="ol")
    {

        let ans="";
        let id=1;


        for(let x of node.children)
        {

            if(
                x.tagName.toLowerCase()
                ==="li"
            )
            {

                ans+=
                id++
                +
                ". "
                +
                children(x).trim()
                +
                "\n";

            }

        }


        return "\n"+ans+"\n";

    }





    if(tag==="ul")
    {

        let ans="";


        for(let x of node.children)
        {

            if(
                x.tagName.toLowerCase()
                ==="li"
            )
            {

                ans+=
                "- "
                +
                children(x).trim()
                +
                "\n";

            }

        }


        return "\n"+ans+"\n";

    }





    return children(node);

}







function children(node)
{

    let s="";


    for(let x of node.childNodes)
    {
        s+=parse(x);
    }


    return s;

}





function clean(s)
{

    s=s.replace(
        /\n[ \t]+\n/g,
        "\n\n"
    );


    s=s.replace(
        /\n{4,}/g,
        "\n\n\n"
    );


    return s.trim();

}







// =========================
// 接管复制
// =========================


document.addEventListener(
"copy",
function(e)
{


    let sel=
    window.getSelection();


    if(
        !sel ||
        sel.rangeCount===0
    )
        return;



    let box=
    document.createElement("div");



    box.appendChild(
        sel.getRangeAt(0)
        .cloneContents()
    );



    let result=
    clean(
        children(box)
    );



    // 防止空复制破坏正常复制

    if(
        result &&
        result.length>0
    )
    {

        e.clipboardData.setData(
            "text/plain",
            result
        );


        e.preventDefault();

    }



},
true);




// =========================
// 动态页面刷新公式
// =========================


let observer=
new MutationObserver(()=>{});


observer.observe(
    document.documentElement,
    {
        childList:true,
        subtree:true
    }
);



})();
原文
存档评论3 条抓取于