Atcoder Translation! with DeepSeek Translation

Enhance Atcoder experience with DeepSeek translation

当前为 2025-01-19 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Atcoder Translation! with DeepSeek Translation
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Enhance Atcoder experience with DeepSeek translation
// @author       LLDQ
// @match        https://atcoder.jp/*
// @grant        GM_xmlhttpRequest
// @grant        GM_notification
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    const DEEPSEEK_API_KEY = "Your API Key"; // 替换为你的 DeepSeek API Key
    const DEEPSEEK_API_URL = "https://api.deepseek.com/v1/completions"; // DeepSeek API 地址

    // 翻译函数
    async function translateText(text) {
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: "POST",
                url: DEEPSEEK_API_URL,
                headers: {
                    "Content-Type": "application/json",
                    "Authorization": `Bearer ${DEEPSEEK_API_KEY}`
                },
                data: JSON.stringify({
                    model: "deepseek-chat", // 模型名称
                    prompt: `请将以下内容翻译为中文:\n${text}`, // 翻译指令
                    max_tokens: 100000, // 最大 token 数
                    temperature: 1.3, // 温度参数
                    stream: true // 是否流式返回
                }),
                onload: function (response) {
                    if (response.status === 200) {
                        const data = JSON.parse(response.responseText);
                        resolve(data.choices[0].text.trim()); // 返回翻译结果
                    } else {
                        reject(`API 请求失败:${response.statusText}`);
                    }
                },
                onerror: function (error) {
                    reject(`API 请求错误:${error}`);
                }
            });
        });
    }

    // 在 Atcoder 页面中添加翻译按钮
    function addTranslateButton() {
        const problemStatement = document.querySelector(".lang-ja"); // 获取题目描述
        if (!problemStatement) return;

        const button = document.createElement("button");
        button.textContent = "翻译题目";
        button.style.margin = "10px";
        button.style.padding = "5px 10px";
        button.style.backgroundColor = "#4CAF50";
        button.style.color = "white";
        button.style.border = "none";
        button.style.borderRadius = "5px";
        button.style.cursor = "pointer";

        button.addEventListener("click", async () => {
            const textToTranslate = problemStatement.innerText; // 获取需要翻译的文本
            try {
                const translatedText = await translateText(textToTranslate); // 调用翻译函数
                problemStatement.innerText = translatedText; // 显示翻译结果
            } catch (error) {
                GM_notification({
                    title: "翻译失败",
                    text: error,
                    timeout: 5000
                });
            }
        });

        problemStatement.parentNode.insertBefore(button, problemStatement); // 插入按钮
    }

    // 页面加载完成后添加翻译按钮
    window.addEventListener("load", addTranslateButton);
})();