Skip to content

Commit 097922f

Browse files
Merge pull request #2 from dineshsutihar/progress
V2: Adds Color and syntax highlighting
2 parents d76af63 + 66ccdfb commit 097922f

File tree

15 files changed

+985
-277
lines changed

15 files changed

+985
-277
lines changed

.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Promotional
2+
release
3+
.env
4+
5+
node_modules
6+
.DS_Store
7+
dist
8+
coverage
9+
.vscode
10+
.idea
11+
*.log
12+
*.env.local
13+
*.env.development.local
14+
*.env.test.local
15+
*.env.production.local
16+
*.env.*.local
17+
npm-debug.log*
18+
yarn-debug.log*
19+
yarn-error.log*
20+

README.md

Lines changed: 93 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ CodeTranslateAI is a powerful Chrome extension that allows you to translate code
55
## ✨ Key Features
66

77
- **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.
99
- **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.
1312

1413
---
1514

1615
## 🔧 Tech Stack
1716

1817
- **Frontend:**
19-
- Plain JavaScript (ES6+)
18+
- Modular JavaScript (ES6+)
19+
- **esbuild** & **dotenv** (for bundling and environment variables)
2020
- HTML5 & CSS3
21-
- Chrome Extension APIs (`storage`, `runtime`, `scripting`)
21+
- Chrome Extension APIs (`storage`, `activeTab`)
2222
- Shadow DOM for style isolation.
2323
- **Backend:**
2424
- Cloudflare Workers
@@ -34,13 +34,13 @@ To get a local copy up and running, follow these simple steps.
3434

3535
### Prerequisites
3636

37-
You must have Node.js and npm installed on your machine.
37+
You must have **Node.js** and **npm** installed on your machine.
3838

3939
- [Download Node.js](https://nodejs.org/)
4040

4141
### ⚙️ Part 1: Backend Setup (Cloudflare Worker)
4242

43-
1. **Clone the Repository** (or set up your backend folder).
43+
1. **Clone the Repository**
4444

4545
```sh
4646
git clone https://github.com/dineshsutihar/CodeTranslateAI.git
@@ -65,7 +65,9 @@ You must have Node.js and npm installed on your machine.
6565

6666
5. **Set the Secret Key**
6767

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 -->
6971

7072
```sh
7173
npx wrangler secret put GEMINI_API_KEY
@@ -75,33 +77,97 @@ You must have Node.js and npm installed on your machine.
7577

7678
- Deploy the backend to make it live.
7779

80+
<!-- end list -->
81+
7882
```sh
7983
npx wrangler deploy
8084
```
8185

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.
8387

8488
### 🖥️ Part 2: Frontend Setup (Chrome Extension)
8589

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+
```
87101

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 -->
91119

92120
```javascript
93-
// background.js
94-
const BACKEND_URL = "https://backend.<your-worker-name>.dev"; // PASTE YOUR URL HERE
121+
// build.js
122+
import esbuild from "esbuild";
123+
import "dotenv/config";
124+
125+
const define = {};
126+
for (const k in process.env) {
127+
define[`process.env.${k}`] = JSON.stringify(process.env[k]);
128+
}
129+
130+
esbuild
131+
.build({
132+
entryPoints: ["scripts/content.js", "background.js"],
133+
bundle: true,
134+
outdir: "dist",
135+
define: define,
136+
})
137+
.catch(() => process.exit(1));
95138
```
96139
97-
2. **Load the Extension in Chrome**
140+
5. **Build the Extension**
141+
142+
- 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**
98153
99154
- Open Google Chrome and navigate to `chrome://extensions`.
100-
- Enable **"Developer mode"** using the toggle in the top-right corner.
155+
- Enable **"Developer mode"**.
101156
- 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`).
103158
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 command in your terminal:
167+
```sh
168+
npm run build
169+
```
170+
3. Go to `chrome://extensions` and click the **reload** button for the CodeTranslateAI extension.
105171
106172
---
107173
@@ -110,40 +176,24 @@ The **CodeTranslateAI** icon should now appear in your Chrome toolbar, and the e
110176
1. Click the extension icon in the Chrome toolbar.
111177
2. Select your desired target language from the dropdown menu.
112178
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.
116181
117182
---
118183
119184
## 🐛 Debugging the Backend
120185
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.
130187
188+
1. **Navigate to your Backend Directory**.
189+
2. **Run the Tail Command**:
131190
```sh
132191
npx wrangler tail
133192
```
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.
144194
145195
---
146196
147197
## ⚖️ License
148198
149-
Distributed under the MIT License. See `LICENSE.txt` for more information.
199+
Distributed under the MIT License.

frontend/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BACKEND_URL="https://backend.your-cloudflare-worker.workers.dev"

frontend/background.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
2+
chrome.runtime.onMessage.addListener((request, _, sendResponse) => {
33
if (request.type === "TRANSLATE_CODE") {
4-
const BACKEND_URL = "https://backend.dineshsutihar123.workers.dev";
4+
const BACKEND_URL = process.env.BACKEND_URL;
55

66
chrome.storage.sync.get(['targetLanguage'], (result) => {
77
const targetLanguage = result.targetLanguage || 'Java';

frontend/build.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import esbuild from 'esbuild';
2+
import 'dotenv/config';
3+
4+
const define = {};
5+
for (const k in process.env) {
6+
define[`process.env.${k}`] = JSON.stringify(process.env[k]);
7+
}
8+
9+
esbuild.build({
10+
entryPoints: ['scripts/content.js', 'background.js'],
11+
bundle: true,
12+
outdir: 'dist',
13+
define: define,
14+
}).catch(() => process.exit(1));
15+
16+
console.log('Build complete. Files are in the /dist folder.');

0 commit comments

Comments
 (0)