Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
willwade committed Nov 1, 2024
2 parents efbb76a + 3fbb623 commit 3c9fa19
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 4 deletions.
96 changes: 96 additions & 0 deletions nodejs/sender-monitor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@

# Clipboard & OCR Text Translator and Sender

This Windows Electron application captures text from either the system clipboard or a defined screen area via OCR (Optical Character Recognition). The app can then translate this text to a target language and send it to a remote display device via WebRTC.

## Features

- **Clipboard Monitoring**: Automatically captures text copied to the clipboard and processes it.
- **Screen OCR Capture**: Defines a screen area to monitor, capturing and processing text via OCR.
- **Language Translation**: Translates captured text from a source to a target language.
- **WebRTC Connectivity**: Sends translated or original text to a paired remote device.
- **QR Code for Easy Pairing**: Displays a QR code for quick connection setup on the remote device.
- **Tray Icon Controls**: Provides easy access to key functions like reconnecting, accessing logs, and viewing session details from the tray icon.

## Setup and Installation

### Prerequisites
- **Node.js**: Ensure you have Node.js installed on your system.
- **Config File**: Customize the configuration in `config.json` for your language preferences and capture settings.

### Installation
1. Clone this repository.
2. Install the dependencies by running:
```bash
npm install
```
3. Run the application:
```bash
npm start
```

## Configuration

The application uses a `config.json` file, located at:

- **Development**: `./config.json`
- **Production**: `%AppData%/YourAppName/config.json`

This file allows customization of:
- `translation.sourceLang`: Source language for translation.
- `translation.targetLang`: Target language for translation.
- `monitorMode`: `clipboard` for clipboard monitoring or `ocr` for screen OCR.
- `captureArea`: Defines `x`, `y`, `width`, and `height` for OCR screen capture.
- `captureInterval`: Interval (in ms) for capturing and processing content.
- `translation.autoCorrect`: Whether to enable auto-correction on translation.

### Example Configuration

```json
{
"translation": {
"enabled": true,
"sourceLang": "en",
"targetLang": "es",
"autoCorrect": false
},
"monitorMode": "clipboard",
"captureArea": {
"x": 100,
"y": 100,
"width": 500,
"height": 300
},
"captureInterval": 5000
}
```

## Tray Menu Options

- **Show QR Code**: Display a QR code for connecting a remote display device.
- **Reconnect**: Attempts to reconnect to the remote device if disconnected.
- **Open Log Directory**: Opens the directory containing log files.
- **Open Config**: Opens the `config.json` file for editing.
- **Copy Session ID**: Copies the session ID to the clipboard for pairing.
- **Quit**: Exits the application.

## Logging

The app generates a `log.txt` file in the application data directory to record events, errors, and connection status.

## Closing the Application

The app prevents all windows from closing by default. To fully quit, use the **Quit** option in the tray menu or force quit the app from the task manager.

## Troubleshooting

- **Translation Issues**: Ensure network connectivity and verify language codes in `config.json`.
- **OCR Failures**: Check screen capture settings, particularly the capture area coordinates and dimensions.

## License

MIT License

---

This project integrates the `google-translate-api-x` for translation and `node-screenshots` for OCR capture.
27 changes: 23 additions & 4 deletions nodejs/sender-monitor/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ ipcMain.on("close-qr-window", () => {
}
});

function createQRWindow(url) {
async function createQRWindow(url) {
if (qrWindow) {
qrWindow.close();
}

// Use await to ensure QR code generation completes before moving on
const qrDataUrl = await QRCode.toDataURL(url);

qrWindow = new BrowserWindow({
width: 220,
height: 240,
Expand All @@ -52,15 +55,15 @@ function createQRWindow(url) {
show: false,
webPreferences: {
contextIsolation: true,
preload: path.join(__dirname, "preload.js"), // Preload script for IPC
preload: path.join(__dirname, "preload.js"),
}
});

const htmlContent = `
<html>
<body style="display: flex; flex-direction: column; align-items: center; justify-content: center; margin: 0;">
<button id="closeButton" style="position: absolute; top: 10px; right: 10px; padding: 2px 6px; cursor: pointer;">✖</button>
<img src="${url}" width="200" height="200" style="display: block; margin-top: 20px;" />
<img src="${qrDataUrl}" width="200" height="200" style="display: block; margin-top: 20px;" />
<script>
document.getElementById("closeButton").addEventListener("click", () => {
window.closeQRWindow();
Expand All @@ -73,10 +76,22 @@ function createQRWindow(url) {
qrWindow.show();

qrWindow.on("closed", () => {
qrWindow = null; // Nullify qrWindow when it's closed
qrWindow = null;
});
}

function reloadConfig() {
try {
config = JSON.parse(fs.readFileSync(configFilePath, "utf-8"));
console.log("Configuration reloaded.");
logMessage("Configuration reloaded from config.json.");
// You may want to reinitialize any settings here that depend on the config
} catch (error) {
console.error("Failed to reload config:", error);
logMessage("Failed to reload config.");
}
}

// Capture and OCR function with return of recognized text
async function captureAndProcessScreen() {
const { x, y, width, height, useEdgeForOCR } = config.captureArea;
Expand Down Expand Up @@ -216,6 +231,10 @@ function updateTrayMenu() {
click: () => shell.openPath(configFilePath)
.catch(err => console.error("Failed to open config file:", err))
},
{
label: "Reload Config",
click: reloadConfig,
},
{
label: "Copy Session ID",
click: () => {
Expand Down

0 comments on commit 3c9fa19

Please sign in to comment.