API 參考
go-browser 完整 API 參考 — 所有匯出函式、型別與常數。
函式
Fetch
func Fetch(ctx context.Context, href string, timeout time.Duration, opt *Option) (*Result, error)
擷取頁面並回傳 Markdown、HTML 或 JSON 格式的內容。根據 domain 與選項自動選擇 headless 或 session 模式。若 headless 擷取遇到 403/429/503 則自動以 headed 模式重試。
| 參數 | 型別 | 說明 |
|---|---|---|
ctx |
context.Context |
用於取消與逾時的 context |
href |
string |
目標 URL |
timeout |
time.Duration |
單次請求逾時;0 表示不逾時 |
opt |
*Option |
設定選項;nil 使用預設值 |
SetMaxConcurrency
func SetMaxConcurrency(n int)
設定最大並發頁面載入數。預設為 8。
Close
func Close()
關閉所有瀏覽器實例 — 包含單例 headless 瀏覽器與互動式分頁瀏覽器。釋放所有資源。
CreateTab
func CreateTab(ctx context.Context, href string, opt *Option) (string, error)
建立互動式瀏覽器分頁,導航至目標 URL,回傳分頁 ID 供後續操作使用。
CloseTab
func CloseTab(tabID string) error
關閉互動式分頁並釋放其 semaphore。若無剩餘分頁,則關閉互動瀏覽器。
TabSnapshot
func TabSnapshot(tabID string) (*Result, error)
擷取互動式分頁的目前狀態為 Markdown、HTML 或 JSON(依分頁的 Option.Type)。
TabClick
func TabClick(tabID, selector string) error
在指定分頁中點擊符合 CSS 選擇器的元素。
TabType
func TabType(tabID, selector, text string) error
在符合 CSS 選擇器的輸入欄位中輸入文字。觸發 input 與 change 事件。
TabScroll
func TabScroll(tabID string, count int) error
滾動分頁頁面。若 count 為 0,使用分頁的 Option.ScrollCount。
TabNavigate
func TabNavigate(tabID, href string) error
將分頁導航至新 URL。等待載入與 DOM 穩定。
TabEval
func TabEval(tabID, js string) (string, error)
在分頁環境中執行 JavaScript 並以字串回傳結果。
Merge
func Merge(snapshots []string) (string, error)
將後續快照的 body 子節點附加至基礎文件的 body,合併多個 HTML 快照。
DedupTree
func DedupTree(nodes []*Node)
使用 FNV-64a 雜湊比較,從 []*Node 樹中移除重複節點。
DedupMarkdownParagraphs
func DedupMarkdownParagraphs(md string) string
透過雜湊從 Markdown 內容移除重複段落。
InlineTimeElements
func InlineTimeElements(htmlSrc string) (string, error)
將 <time> 元素取代為其 datetime 屬性與內部文字。
HTMLToNode
func HTMLToNode(content, baseURL string, keepLinks bool) ([]*Node, error)
將 HTML 內容轉換為結構化 []*Node 樹,供 JSON 輸出。
HTMLToMarkdown
func HTMLToMarkdown(content, baseURL string, keepLinks bool) (string, error)
將 HTML 內容轉換為 Markdown 語法。
型別
Viewport
type Viewport struct {
Width int
Height int
DeviceScaleFactor float64
}
瀏覽器 viewport 尺寸與縮放比例。
Option
type Option struct {
IdleWait time.Duration
MaxLength int
UserAgent string
KeepLinks bool
StealthJS string
SettleJS string
Viewport *Viewport
SameSession bool
Headless bool
Profile string
Type int
ScrollCount int
}
擷取與分頁操作的設定選項。欄位細節請見設定。
Result
type Result struct {
Href string
FinalURL string
Content string
ContentType string `json:",omitempty"`
Title string
Author string
PublishedAt string
Excerpt string
Status int
Tree []*Node `json:",omitempty"`
}
擷取結果,包含提取的內容與中繼資料。
Error
type Error struct {
Status int
Href string
}
HTTP 錯誤,包含狀態碼與失敗 URL。實作 error 介面。
Browser
type Browser struct {
browser *goRod.Browser
lastUsed time.Time
}
內部瀏覽器包裝,含閒置追蹤。未匯出供直接使用。
Node
type Node struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
Level int `json:"level,omitempty"`
Datetime string `json:"datetime,omitempty"`
Href string `json:"href,omitempty"`
Src string `json:"src,omitempty"`
Alt string `json:"alt,omitempty"`
Children []*Node `json:"children,omitempty"`
}
結構化 JSON 樹輸出中的節點。Type 值包含:text、heading、paragraph、link、image、list、list_item、ordered_list、code、code_block、blockquote、bold、italic、linebreak、time、container。
常數
輸出格式型別
const (
TypeMarkdown = iota // 0 — Markdown 輸出(預設)
TypeHTML // 1 — HTML 輸出
TypeJSON // 2 — JSON 樹輸出
)
預設值
const DefaultUserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"