# Browser Profiles Examples

Complete working examples for browser profile automation using the Local API.

**Base URL**: `http://127.0.0.1:40000`

## Full Workflow: Create → Start → Automate → Stop

### curl

```bash
# 1. Create a browser profile
curl -X POST http://127.0.0.1:40000/api/env/create/quick \
  -H "Content-Type: application/json" \
  -d '{"name": "my-profile"}'

# Response: {"code":0,"data":{"envId":"1234567890"}}

# 2. Start the profile
curl -X POST http://127.0.0.1:40000/api/env/start \
  -H "Content-Type: application/json" \
  -d '{"envId": "1234567890"}'

# Response includes debugPort and webdriver path for automation

# 3. Stop the profile
curl -X POST http://127.0.0.1:40000/api/env/close \
  -H "Content-Type: application/json" \
  -d '{"envId": "1234567890"}'
```

### Python (requests + selenium)

```python
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

BASE = "http://127.0.0.1:40000"

# 1. Create a browser profile
resp = requests.post(f"{BASE}/api/env/create/quick", json={
    "name": "automation-profile"
})
env_id = resp.json()["data"]["envId"]
print(f"Created profile: {env_id}")

# 2. Start the profile
resp = requests.post(f"{BASE}/api/env/start", json={
    "envId": env_id
})
data = resp.json()["data"]
debug_port = data["debugPort"]
webdriver_path = data["webdriver"]
print(f"Debug port: {debug_port}, WebDriver: {webdriver_path}")

# 3. Connect Selenium
options = Options()
options.debugger_address = f"127.0.0.1:{debug_port}"
service = Service(executable_path=webdriver_path)
driver = webdriver.Chrome(service=service, options=options)

# 4. Automate
driver.get("https://www.google.com")
print(f"Page title: {driver.title}")

# 5. Cleanup
driver.quit()
requests.post(f"{BASE}/api/env/close", json={"envId": env_id})
print("Profile closed.")
```

### Node.js (axios + puppeteer)

```javascript
const axios = require('axios');
const puppeteer = require('puppeteer');

const BASE = 'http://127.0.0.1:40000';

async function main() {
  // 1. Create a browser profile
  const createResp = await axios.post(`${BASE}/api/env/create/quick`, {
    name: 'automation-profile'
  });
  const envId = createResp.data.data.envId;
  console.log(`Created profile: ${envId}`);

  // 2. Start the profile
  const startResp = await axios.post(`${BASE}/api/env/start`, {
    envId: envId
  });
  const { debugPort } = startResp.data.data;
  console.log(`Debug port: ${debugPort}`);

  // 3. Connect Puppeteer
  const browser = await puppeteer.connect({
    browserWSEndpoint: `ws://127.0.0.1:${debugPort}`,
    defaultViewport: null
  });

  // 4. Automate
  const page = await browser.newPage();
  await page.goto('https://www.google.com');
  console.log(`Page title: ${await page.title()}`);

  // 5. Cleanup
  await browser.disconnect();
  await axios.post(`${BASE}/api/env/close`, { envId });
  console.log('Profile closed.');
}

main().catch(console.error);
```

## Batch Operations

### Batch Update Proxy (Python)

```python
import requests

BASE = "http://127.0.0.1:40000"

# Get all profile IDs
resp = requests.post(f"{BASE}/api/env/page", json={
    "pageNo": 1, "pageSize": 100
})
env_ids = [p["envId"] for p in resp.json()["data"]["dataList"]]

# Batch update proxy for all profiles
requests.post(f"{BASE}/api/env/updateProxy/batch", json={
    "envDataList": [
        {
            "envId": env_id,
            "proxyInfo": {
                "proxyType": "socks5",
                "host": "proxy.example.com",
                "port": 1080,
                "proxyUserName": "user",
                "proxyPassword": "pass"
            }
        }
        for env_id in env_ids
    ]
})
print(f"Updated proxy for {len(env_ids)} profiles.")
```