Skip to content

Latest commit

 

History

History
231 lines (179 loc) · 5.8 KB

File metadata and controls

231 lines (179 loc) · 5.8 KB

bb-browser Fetch API 撠�

璁膩

�?bb-browser daemon 銝剜憓� /api/fetch HTTP 頝舐嚗鈭瘚��其�銝�銝剜銵?fetch 霂瑟���銝?API 憭鈭�� CDP 餈�?tab 蝞∠��餉�嚗隞亥�典��敶�� Cookie�?

�嗆�隡

  • 憭�唳��箇�霈暹嚗�乩蝙�?daemon �?CDP 餈�?TabStateManager
  • �芸 tab 蝞∠�嚗�� ensurePageTarget �芸���撱箏��� tab
  • **�餃����?*嚗瘚��其�銝�銝剜銵��芸�箏蒂 Cookie �恕霂縑�?
  • 蝏��悅嚗蝙�函�� Request/Response �悅嚗噶鈭輕�?

API 蝡舐

POST http://localhost:6666/api/fetch

霂瑟��澆�

{
  "url": "https://example.com/api/data",
  "method": "GET",
  "body": "{\"key\":\"value\"}",
  "headers": {
    "Content-Type": "application/json",
    "X-Custom-Header": "value"
  },
  "tabId": "a1b2"
}

�霂湔�

� 蝐餃� 敹‵ 霂湔�
url string �? �格� URL嚗��撖寡楝敺��詨笆頝臬�嚗?
method string �? HTTP �寞�嚗�霈?GET
body string �? 霂瑟�雿�GET/HEAD 霂瑟�隡蕭�伐�
headers object �? �芸�銋窈瘙仍
tabId string/number �? �� tab ID嚗� ID嚗�銝�摰��芸�

���澆�

����

{
  "status": 200,
  "contentType": "application/json",
  "body": {
    "data": "response data"
  }
}

�秤��

{
  "error": "Fetch error: Network request failed",
  "hint": "Fetch �扯�憭梯揖"
}

雿輻蝷箔�

1. �箸 GET 霂瑟�

curl -X POST http://localhost:6666/api/fetch \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://api.github.com/users/octocat"
  }'

2. POST 霂瑟�

curl -X POST http://localhost:6666/api/fetch \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com/api/login",
    "method": "POST",
    "body": "{\"username\":\"user\",\"password\":\"pass\"}",
    "headers": {
      "Content-Type": "application/json"
    }
  }'

3. �� tab �扯�

curl -X POST http://localhost:6666/api/fetch \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://www.reddit.com/api/me.json",
    "tabId": "a1b2"
  }'

4. Node.js 靚蝷箔�

const fetch = require('node-fetch');

async function bbFetch(url, options = {}) {
  const response = await fetch('http://localhost:6666/api/fetch', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      url,
      method: options.method || 'GET',
      body: options.body,
      headers: options.headers,
      tabId: options.tabId,
    }),
  });
  
  return await response.json();
}

// 雿輻蝷箔�
const result = await bbFetch('https://api.github.com/users/octocat');
console.log(result.body);

5. Python 靚蝷箔�

import requests
import json

def bb_fetch(url, method='GET', body=None, headers=None, tab_id=None):
    """�� bb-browser daemon �扯� fetch 霂瑟�"""
    payload = {
        'url': url,
        'method': method,
    }
    
    if body:
        payload['body'] = body
    if headers:
        payload['headers'] = headers
    if tab_id:
        payload['tabId'] = tab_id
    
    response = requests.post(
        'http://localhost:6666/api/fetch',
        json=payload
    )
    
    return response.json()

# 雿輻蝷箔�
result = bb_fetch('https://api.github.com/users/octocat')
print(json.dumps(result['body'], indent=2))

撌乩�瘚�

  1. �交霂瑟�嚗TTP ��冽�?/api/fetch POST 霂瑟�
  2. 蝑� CDP 撠梁貌嚗&靽?Chrome 餈撌脣遣蝡?
  3. � tab嚗?
    • 憒���鈭?tabId嚗蝙�刻砲 tab
    • �血��� ensurePageTarget �芸���撱?tab
  4. �遣 fetch �嚗��瘚��其葉�扯��?JavaScript 隞��
  5. �扯�霂瑟�嚗�� CDP �?Runtime.evaluate �?tab 銝剜銵?
  6. 餈�蝏�嚗圾��摨僎餈�蝏��冽

銝?CLI fetch �賭誘��?

�寞�? CLI bb-browser fetch HTTP API /api/fetch
靚�孵� �賭誘銵? HTTP POST
��箸 鈭箇掩鈭支����祈��? 蝔�����蝔��?
颲�澆� 蝏垢� JSON
Tab 蝞∠� �芸�寥��� �舀��� tabId
霈方� �? �舀� Bearer token

摰瘜冽�鈭★

  1. 霈方�嚗��?daemon �蔭鈭?token嚗�閬霂瑟�憭港葉瘛餃�嚗?

    -H "Authorization: Bearer YOUR_TOKEN"
  2. CORS嚗PI 撌脤�蝵?CORS �捂頝典�霈輸

  3. �砍霈輸嚗aemon 暺恕� 127.0.0.1嚗��砍�航挪�?

�秤憭�

撣貉��秤

�秤 �� 閫��寞�
Missing url parameter 霂瑟�雿撩撠?url 摮挾 蝖桐�霂瑟�� url
Chrome not connected CDP �芾��? �臬 Chrome 撟園��?daemon
Fetch timeout 霂瑟�頞 璉��亦�蝏�憓�頞�園
Fetch error: ... 瘚��?fetch 憭梯揖 璉��?URL ��蝏�?

�扯��

  • 餈憭嚗��函�� CDP WebSocket 餈
  • Tab 憭嚗����霂瑟�隡��?tab
  • 頞�批嚗�霈方��?30 蝘�COMMAND_TIMEOUT嚗?
  • 撟嗅��舀�嚗��銝芸僎�窈瘙?

�芣�拙�

�航��撘箏��踝�

  1. 瘚���嚗�之�辣銝蝸
  2. 霂瑟��行嚗 fetch ��瘛餃��拙�
  3. 蝻��批嚗��摮��仿�蝵?
  4. 隞���舀�嚗�摰�隞��霈曄蔭
  5. ���箏嚗�券�霂仃韐亦�霂瑟�

�詨�辣

  • packages/shared/src/protocol.ts - �悅摰�
  • packages/daemon/src/command-dispatch.ts - �賭誘憭�
  • packages/daemon/src/http-server.ts - HTTP 頝舐
  • packages/cli/src/commands/fetch.ts - CLI 摰��?