You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+93-43Lines changed: 93 additions & 43 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,20 +5,20 @@ CodeTranslateAI is a powerful Chrome extension that allows you to translate code
5
5
## ✨ Key Features
6
6
7
7
-**On-the-Fly Translation:** Instantly translate code on platforms like Stack Overflow, Medium, and technical blogs.
8
-
-**Secure Backend:** Uses a serverless Cloudflare Worker so your AI API key is never exposed on the frontend.
8
+
-**Secure Serverless Backend:** Uses a Cloudflare Worker.
9
9
-**Multi-Language Tabs:** Translate the same code block into multiple languages and switch between them easily.
10
-
-**Smart Caching:** Translations are cached in your browser for 10 days to reduce API calls and provide instant results for previously translated code.
11
-
-**Elegant UI:** A clean, non-intrusive UI that matches the width of the original code block and uses a tabbed layout for multiple translations.
12
-
-**Powered by Gemini:** Leverages the power of Google's Gemini AI for high-quality code translations.
10
+
-**Smart Caching:** Translations are cached in your browser for 10 days to reduce API calls and provide instant results.
11
+
-**Powered by Gemini:** Leverages Google's Gemini AI for high-quality code translations with syntax highlighting.
13
12
14
13
---
15
14
16
15
## 🔧 Tech Stack
17
16
18
17
-**Frontend:**
19
-
- Plain JavaScript (ES6+)
18
+
- Modular JavaScript (ES6+)
19
+
-**esbuild** & **dotenv** (for bundling and environment variables)
@@ -65,7 +65,9 @@ You must have Node.js and npm installed on your machine.
65
65
66
66
5. **Set the Secret Key**
67
67
68
-
- Run the following command and paste your Gemini API key when prompted. This stores it securely on Cloudflare.
68
+
- Run the following command and paste your Gemini API key when prompted.
69
+
70
+
<!-- end list -->
69
71
70
72
```sh
71
73
npx wrangler secret put GEMINI_API_KEY
@@ -75,33 +77,97 @@ You must have Node.js and npm installed on your machine.
75
77
76
78
- Deploy the backend to make it live.
77
79
80
+
<!-- end list -->
81
+
78
82
```sh
79
83
npx wrangler deploy
80
84
```
81
85
82
-
- After deployment, **copy the URL** that Wrangler provides. It will look like `https://backend.<your-worker-name>.dev`.
86
+
- After deployment, **copy the URL** that Wrangler provides.
83
87
84
88
### 🖥️ Part 2: Frontend Setup (Chrome Extension)
85
89
86
-
1. **Configure the Backend URL**
90
+
1. **Navigate to the Frontend Directory**
91
+
92
+
```sh
93
+
cd ../frontend
94
+
```
95
+
96
+
2. **Install Dependencies**
97
+
98
+
```sh
99
+
npm install
100
+
```
87
101
88
-
- Navigate to your Chrome extension folder (e.g., `my-code-translator`).
89
-
- Open the `background.js` file.
90
-
- Find the `BACKEND_URL` constant and **paste the Cloudflare Worker URL** you copied in the previous step.
102
+
3. **Configure the Backend URL**
103
+
104
+
- In the `frontend` folder, create a new file named `.env`.
105
+
- Add the Cloudflare Worker URL you copied in the previous step to this file:
106
+
107
+
<!-- end list -->
108
+
109
+
```
110
+
# .env file
111
+
BACKEND_URL="https://your-worker-url.workers.dev"
112
+
```
113
+
114
+
4. **Create the Build Configuration**
115
+
116
+
- In the `frontend` folder, create a file named `build.js` and add the following content. This file tells our build script how to use the `.env` variable.
117
+
118
+
<!-- end list -->
91
119
92
120
```javascript
93
-
// background.js
94
-
const BACKEND_URL = "https://backend.<your-worker-name>.dev"; // PASTE YOUR URL HERE
- Run the build command to bundle your scripts and inject the environment variable.
143
+
144
+
<!-- end list -->
145
+
146
+
```sh
147
+
npm run build
148
+
```
149
+
150
+
This will create a `dist` folder containing your final `content.js` and `background.js` files.
151
+
152
+
6. **Load the Extension in Chrome**
98
153
99
154
- Open Google Chrome and navigate to `chrome://extensions`.
100
-
- Enable **"Developer mode"** using the toggle in the top-right corner.
155
+
- Enable **"Developer mode"**.
101
156
- Click the **"Load unpacked"** button.
102
-
- Select your Chrome extension folder (the one containing`manifest.json`).
157
+
- Select your Chrome extension folder (the `frontend` folder that contains`manifest.json`).
103
158
104
-
The **CodeTranslateAI** icon should now appear in your Chrome toolbar, and the extension is ready to use\!
159
+
The **CodeTranslateAI** icon should now appear in your Chrome toolbar\!
160
+
161
+
---
162
+
163
+
## 💻 Development Workflow
164
+
165
+
1. Make any changes to your JavaScript files in the `scripts/` folder or `background.js`.
166
+
2. Run the build commandin your terminal:
167
+
```sh
168
+
npm run build
169
+
```
170
+
3. Go to `chrome://extensions` and click the **reload** button for the CodeTranslateAI extension.
105
171
106
172
---
107
173
@@ -110,40 +176,24 @@ The **CodeTranslateAI** icon should now appear in your Chrome toolbar, and the e
110
176
1. Click the extension icon in the Chrome toolbar.
111
177
2. Select your desired target language from the dropdown menu.
112
178
3. Click the **"Enable Code Selector"** button.
113
-
4. Your cursor will change to a crosshair. Hover over any code block on a webpage and click on it.
114
-
5. A "Translating..." message will appear, followed by a new UI element containing the translated code.
115
-
6. If you translate the same block into another language, a new tab will be added to the UI.
179
+
4. Your cursor will change to a crosshair. Click on any code block on a webpage.
180
+
5. A "Translating..." message will appear, followed by the translated code in a new UI.
116
181
117
182
---
118
183
119
184
## 🐛 Debugging the Backend
120
185
121
-
If you encounter errors or the translation isn't working, the first step is to check the live logs from your Cloudflare Worker. This allows you to see what's happening on the server in real-time.
122
-
123
-
1. **Navigate to your Backend Directory**
124
-
125
-
- Open your terminal and change into the directory where your Cloudflare Worker code is located (e.g., `CodeTranslateAI/backend`).
126
-
127
-
2. **Run the Tail Command**
128
-
129
-
- Execute the following command to start streaming the logs:
186
+
If you encounter errors, check the live logs from your Cloudflare Worker.
130
187
188
+
1. **Navigate to your Backend Directory**.
189
+
2. **Run the Tail Command**:
131
190
```sh
132
191
npx wrangler tail
133
192
```
134
-
135
-
- The terminal will connect and say `Connected to [worker-name], waiting for logs...`.
136
-
137
-
3. **Trigger the Error**
138
-
139
-
- With the log stream running, go to your browser and use the extension to perform an action that causes an error.
140
-
141
-
4. **Check the Terminal**
142
-
143
-
- Look back at your terminal. Any errors or log messages from your worker will appear instantly. Look for lines that start with `(error)`. This will give you the exact reason for the failure, such as an invalid API key or a quota issue.
193
+
3. **Trigger the Error** by using the extension in your browser and check the terminal for error messages.
144
194
145
195
---
146
196
147
197
## ⚖️ License
148
198
149
-
Distributed under the MIT License. See `LICENSE.txt`for more information.
0 commit comments