設定
go-browser 設定選項 — Option 欄位、環境變數、Chrome 設定檔路徑與輸出格式類型。
Option 欄位
Option 結構體控制所有擷取與分頁行為。傳遞給 Fetch、CreateTab 及相關函式。
| 欄位 | 型別 | 預設值 | 說明 |
|---|---|---|---|
IdleWait |
time.Duration |
2s |
頁面載入或滾動後等待 DOM 穩定的時間 |
MaxLength |
int |
1 << 20 (1MB) |
內容最大長度(位元組);超過則截斷 |
UserAgent |
string |
DefaultUserAgent |
瀏覽器的自訂 User-Agent 字串 |
KeepLinks |
bool |
false |
在 Markdown/JSON 輸出中保留連結與圖片 |
StealthJS |
string |
內嵌 stealth.js |
透過 EvalOnNewDocument 注入的自訂 stealth JavaScript |
SettleJS |
string |
內嵌 listener.js |
用於延遲載入偵測的自訂 settle JavaScript |
Viewport |
*Viewport |
{1280, 960, 1} |
瀏覽器 viewport 尺寸與縮放比例 |
SameSession |
bool |
false |
使用 Chrome Cookie 會話注入存取需登入頁面 |
Headless |
bool |
false |
強制 headless 模式;無顯示器時自動選取 |
Profile |
string |
"Default" |
用於 Cookie 提取的 Chrome 設定檔名稱 |
Type |
int |
TypeMarkdown (0) |
輸出格式:TypeMarkdown、TypeHTML 或 TypeJSON |
ScrollCount |
int |
3 |
動態內容擷取的滾動疊代次數 |
預設 Option
當傳遞 nil 給 Fetch 或 CreateTab 時,套用以下預設值:
&Option{
IdleWait: 2 * time.Second,
MaxLength: 1 << 20,
UserAgent: DefaultUserAgent,
StealthJS: defaultStealthJS,
SettleJS: defaultListenerJS,
Viewport: &Viewport{Width: 1280, Height: 960, DeviceScaleFactor: 1},
Profile: "Default",
ScrollCount: 3,
Type: TypeMarkdown,
}
輸出格式常數
const (
TypeMarkdown = iota // 0 — Markdown(預設)
TypeHTML // 1 — 原始 HTML
TypeJSON // 2 — 結構化 JSON 樹
)
Chrome 設定檔路徑
go-browser 在以下平台特定路徑尋找 Chrome 設定檔以進行 Cookie 提取:
| 作業系統 | 設定檔根目錄 |
|---|---|
| macOS | ~/Library/Application Support/Google/Chrome/<Profile> |
| Linux | ~/.config/google-chrome/<Profile> |
預設設定檔名稱為 "Default"。可透過 Option.Profile 覆蓋。
Chrome 二進位檔路徑
go-browser 在以下位置搜尋 Chrome:
| 作業系統 | 搜尋路徑 |
|---|---|
| macOS | /Applications/Google Chrome.app/Contents/MacOS/Google Chrome |
| macOS | /Applications/Chromium.app/Contents/MacOS/Chromium |
| Linux | google-chrome、google-chrome-stable、chromium、chromium-browser(透過 PATH) |
並發
預設並發限制為 8 個同時頁面載入。使用 SetMaxConcurrency 覆蓋:
goBrowser.SetMaxConcurrency(16)
瀏覽器閒置回收
單例 headless 瀏覽器在閒置 5 分鐘後自動關閉。回收檢查每 1 分鐘執行一次。
平台支援
| 功能 | macOS | Linux | Windows |
|---|---|---|---|
| Headless 擷取 | ✅ | ✅ | ✅ |
| Headed 擷取 | ✅ | ✅(需 DISPLAY) | ✅ |
| Cookie 會話 | ✅ | ✅ | ❌ |
| 互動式分頁 | ✅ | ✅ | ✅ |