Atcoder Translation! with DeepSeek Translation

Enhance Atcoder experience with DeepSeek translation

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

  1. // ==UserScript==
  2. // @name Atcoder Translation! with DeepSeek Translation
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Enhance Atcoder experience with DeepSeek translation
  6. // @author LLDQ
  7. // @match https://atcoder.jp/*
  8. // @grant GM_xmlhttpRequest
  9. // @grant GM_notification
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15.  
  16. const DEEPSEEK_API_KEY = "Your API Key"; // 替换为你的 DeepSeek API Key
  17. const DEEPSEEK_API_URL = "https://api.deepseek.com/v1/completions"; // DeepSeek API 地址
  18.  
  19. // 翻译函数
  20. async function translateText(text) {
  21. return new Promise((resolve, reject) => {
  22. GM_xmlhttpRequest({
  23. method: "POST",
  24. url: DEEPSEEK_API_URL,
  25. headers: {
  26. "Content-Type": "application/json",
  27. "Authorization": `Bearer ${DEEPSEEK_API_KEY}`
  28. },
  29. data: JSON.stringify({
  30. model: "deepseek-chat", // 模型名称
  31. prompt: `请将以下内容翻译为中文:\n${text}`, // 翻译指令
  32. max_tokens: 100000, // 最大 token 数
  33. temperature: 1.3, // 温度参数
  34. stream: true // 是否流式返回
  35. }),
  36. onload: function (response) {
  37. if (response.status === 200) {
  38. const data = JSON.parse(response.responseText);
  39. resolve(data.choices[0].text.trim()); // 返回翻译结果
  40. } else {
  41. reject(`API 请求失败:${response.statusText}`);
  42. }
  43. },
  44. onerror: function (error) {
  45. reject(`API 请求错误:${error}`);
  46. }
  47. });
  48. });
  49. }
  50.  
  51. // 在 Atcoder 页面中添加翻译按钮
  52. function addTranslateButton() {
  53. const problemStatement = document.querySelector(".lang-ja"); // 获取题目描述
  54. if (!problemStatement) return;
  55.  
  56. const button = document.createElement("button");
  57. button.textContent = "翻译题目";
  58. button.style.margin = "10px";
  59. button.style.padding = "5px 10px";
  60. button.style.backgroundColor = "#4CAF50";
  61. button.style.color = "white";
  62. button.style.border = "none";
  63. button.style.borderRadius = "5px";
  64. button.style.cursor = "pointer";
  65.  
  66. button.addEventListener("click", async () => {
  67. const textToTranslate = problemStatement.innerText; // 获取需要翻译的文本
  68. try {
  69. const translatedText = await translateText(textToTranslate); // 调用翻译函数
  70. problemStatement.innerText = translatedText; // 显示翻译结果
  71. } catch (error) {
  72. GM_notification({
  73. title: "翻译失败",
  74. text: error,
  75. timeout: 5000
  76. });
  77. }
  78. });
  79.  
  80. problemStatement.parentNode.insertBefore(button, problemStatement); // 插入按钮
  81. }
  82.  
  83. // 页面加载完成后添加翻译按钮
  84. window.addEventListener("load", addTranslateButton);
  85. })();