Browser Extension — Firefox Plugin for Rust Backend

# Browser Extension — Firefox Plugin for Rust Backend

Design for a Firefox browser extension that interfaces with the super-marvin Rust backend for live poker play.

---

## Architecture

```
Firefox Extension ←→ Communication Layer ←→ Rust Backend (super-marvin)
     (JS/TS)              (WebSocket/REST)        (holdem_bots)
```

The extension captures game state from browser poker tables and relays it to the Rust backend for decision-making. The backend returns actions which the extension executes.

---

## Extension Structure

```
firefox-extension/
├── manifest.json          # Manifest V3
├── background.js          # Service worker — manages backend connection
├── content-scripts/
│   └── main.js            # Injected into poker pages — scrapes game state
├── popup/
│   ├── popup.html         # Status/control UI
│   ├── popup.js
│   └── popup.css
├── options/
│   ├── options.html       # Settings (backend URL, session config)
│   └── options.js
└── icons/
```

---

## Communication: Backend Protocol

### Recommended: WebSocket + JSON

Simple, language-agnostic, real-time, well-supported in both JS and Rust.

**Message format:**
```json
{
  "session_id": "sess123",
  "type": "game_state | action_request | action_response",
  "payload": { ... },
  "request_id": "uuid"
}
```

**Extension → Backend:**
```javascript
ws.send(JSON.stringify({
    type: "action_request",
    payload: { hand: "Ah Ks", board: "Ts 7h 2h", pot: 150, ... }
}));
```

**Backend → Extension:**
```json
{ "type": "action_response", "payload": { "action": "raise", "amount": 300 } }
```

### For Scale: Message Queue + WebSocket Gateway

If multiple clients/sessions are needed:

```
Extension ←WebSocket→ Gateway ←MQ→ Rust Workers
                              ↕
                        Session Store (Redis)
```

Use RabbitMQ or NATS for backend routing; Redis for session state; WebSocket for real-time extension communication.

### Alternative: gRPC with Protobuf
Type-safe across languages, efficient binary protocol, built-in versioning. Overkill for single-client use but ideal if multiple client types are planned.

---

## IDE Setup

### JetBrains IntelliJ IDEA Ultimate (Recommended)
Single IDE for both extension (JS/TS) and backend (Rust):
- **Plugins:** Rust / RustRover, WebExtensions API Support, TOML
- **Tools:** `web-ext` CLI for Firefox extension dev, `cargo` for Rust backend

### Alternative: RustRover + WebStorm combo
- RustRover for the Rust backend
- WebStorm for the extension

### Development Workflow
```bash
# Terminal 1: Start Rust backend
cargo run

# Terminal 2: Start extension in dev mode
web-ext run --target=firefox-desktop --reload

# Debug: Firefox → about:debugging → Load Temporary Add-on → manifest.json
```

---

## Native Messaging (Direct Extension ↔ Rust)

For low-latency local-only communication without a server:

**manifest.json:**
```json
{
  "permissions": ["nativeMessaging"],
  "background": { "scripts": ["background.js"] }
}
```

**background.js:**
```javascript
const RUST_APP = 'com.supermarvin.bot';
const port = chrome.runtime.connectNative(RUST_APP);

chrome.runtime.onMessage.addListener((req, sender, respond) => {
    port.postMessage(req);
    port.onMessage.addListener(respond);
    return true; // async response
});
```

The Rust side registers a native messaging host that reads stdin JSON and writes stdout JSON.

---

## Docker Setup (for MQ + Redis architecture)

```yaml
# docker-compose.yml
services:
  redis:
    image: redis:7-alpine
    ports: ["6379:6379"]
  rabbitmq:
    image: rabbitmq:3-management
    ports: ["5672:5672", "15672:15672"]
  backend:
    build: ./backend
    environment:
      - REDIS_URL=redis://redis:6379
      - MQ_URL=amqp://rabbitmq:5672
```

id: c994139aac9245c79da7923dbe8579b3
parent_id: 2c8da247905946c3aa19eb4936e16323
created_time: 2026-02-12T15:00:31.395Z
updated_time: 2026-05-31T11:00:35.866Z
is_conflict: 0
latitude: 48.20817430
longitude: 16.37381890
altitude: 0.0000
author: 
source_url: 
is_todo: 0
todo_due: 0
todo_completed: 0
source: joplin-desktop
source_application: net.cozic.joplin-desktop
application_data: 
order: 0
user_created_time: 2026-02-12T15:00:31.395Z
user_updated_time: 2026-05-31T11:00:35.866Z
encryption_cipher_text: 
encryption_applied: 0
markup_language: 1
is_shared: 0
share_id: 
conflict_original_id: 
master_key_id: 
user_data: 
deleted_time: 0
type_: 1