diff
// Config.cs
+ using System.IO;
+ using YamlDotNet.Serialization;
namespace translator
{
public partial class Config : Form
{
+ private Settings _settings;
+ private const string CONFIG_FILE = "config.yaml";
+ private class Settings
+ {
+ public string Proxy { get; set; }
+ public string OpenAIKey { get; set; }
+ public string Prompt { get; set; }
+ }
public Config()
{
InitializeComponent();
+ LoadSettings();
+ InitializeUI();
}
+ private void LoadSettings()
+ {
+ if (File.Exists(CONFIG_FILE))
+ {
+ var deserializer = new DeserializerBuilder().Build();
+ _settings = deserializer.Deserialize<Settings>(File.ReadAllText(CONFIG_FILE));
+ }
+ else
+ {
+ _settings = new Settings();
+ }
+ }
+ private void InitializeUI()
+ {
+ this.Size = new Size(400, 300);
+
+ var lblProxy = new Label() { Text = "Proxy:", Location = new Point(20, 20) };
+ var txtProxy = new TextBox() {
+ Location = new Point(120, 20),
+ Width = 200,
+ Text = _settings.Proxy
+ };
+ var lblKey = new Label() { Text = "OpenAI Key:", Location = new Point(20, 60) };
+ var txtKey = new TextBox() {
+ Location = new Point(120, 60),
+ Width = 200,
+ Text = _settings.OpenAIKey
+ };
+ var lblPrompt = new Label() { Text = "Prompt:", Location = new Point(20, 100) };
+ var txtPrompt = new TextBox() {
+ Location = new Point(120, 100),
+ Width = 200,
+ Multiline = true,
+ Height = 100,
+ Text = _settings.Prompt
+ };
+ var btnSave = new Button() {
+ Text = "Save",
+ Location = new Point(150, 220)
+ };
+ btnSave.Click += (s, e) => SaveSettings(txtProxy.Text, txtKey.Text, txtPrompt.Text);
+ Controls.AddRange(new Control[] {
+ lblProxy, txtProxy,
+ lblKey, txtKey,
+ lblPrompt, txtPrompt,
+ btnSave
+ });
+ }
+ private void SaveSettings(string proxy, string key, string prompt)
+ {
+ _settings.Proxy = proxy;
+ _settings.OpenAIKey = key;
+ _settings.Prompt = prompt;
+ var serializer = new SerializerBuilder().Build();
+ File.WriteAllText(CONFIG_FILE, serializer.Serialize(_settings));
+ MessageBox.Show("Settings saved!");
+ }
}
}
// Result.cs
namespace translator
{
public partial class Result : Form
{
+ private TextBox txtResult;
public Result(string translatedText)
{
InitializeComponent();
+ InitializeUI(translatedText);
}
+ private void InitializeUI(string text)
+ {
+ this.Size = new Size(400, 300);
+
+ txtResult = new TextBox() {
+ Multiline = true,
+ ReadOnly = true,
+ Dock = DockStyle.Fill,
+ Text = text
+ };
+ Controls.Add(txtResult);
+ }
}
}
// manifest.json
+{
+ "manifest_version": 3,
+ "name": "Simple Translate",
+ "version": "1.0",
+ "description": "Translation tool using AI",
+ "permissions": ["contextMenus", "storage"],
+ "background": {
+ "service_worker": "background.js"
+ },
+ "content_scripts": [{
+ "matches": ["<all_urls>"],
+ "js": ["content.js"]
+ }],
+ "icons": {
+ "16": "icon16.png",
+ "48": "icon48.png",
+ "128": "icon128.png"
+ }
+}
// background.js
+chrome.runtime.onInstalled.addListener(() => {
+ chrome.contextMenus.create({
+ id: "translateSelection",
+ title: "Translate Selected Text",
+ contexts: ["selection"]
+ });
+});
+chrome.contextMenus.onClicked.addListener((info, tab) => {
+ if (info.menuItemId === "translateSelection") {
+ chrome.tabs.sendMessage(tab.id, {
+ action: "translate",
+ text: info.selectionText
+ });
+ }
+});
// content.js
+let modal = null;
+chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
+ if (request.action === "translate") {
+ showTranslation(request.text);
+ }
+});
+async function showTranslation(text) {
+ const response = await fetch('YOUR_API_ENDPOINT', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify({ text })
+ });
+
+ const result = await response.json();
+
+ if (!modal) {
+ modal = document.createElement('div');
+ modal.style.cssText = `
+ position: fixed;
+ top: 50%;
+ left: 50%;
+ transform: translate(-50%, -50%);
+ background: white;
+ padding: 20px;
+ border-radius: 5px;
+ box-shadow: 0 0 10px rgba(0,0,0,0.5);
+ z-index: 10000;
+ `;
+ }
+
+ modal.innerHTML = `
+ <div style="margin-bottom: 10px">${result.translation}</div>
+ <button onclick="this.parentElement.remove()">Close</button>
+ `;
+
+ document.body.appendChild(modal);
+}