Atcoder Translation! with DeepSeek Translation

Enhance Atcoder experience with DeepSeek translation

目前為 2025-01-19 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
})();