browser

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const CHROME_DEBUG_PORT = 9222
View Source
const CHROME_DEFAULT_USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
View Source
const CHROME_EXTENSIONS_PATH = "chrome_extensions"
View Source
const CHROME_PROFILE_PATH = "chrome_profile"
View Source
const CHROME_PROFILE_USER = "Default"

Variables

View Source
var CHROME_ARGS = []string{

	"--disable-cookie-encryption",
	"--disable-sync",

	"--allow-legacy-extension-manifests",
	"--allow-pre-commit-input",
	"--disable-blink-features=AutomationControlled",

	"--install-autogenerated-theme=0,0,0",

	"--log-level=2",
	"--enable-logging=stderr",

	"--remote-debugging-port=" + strconv.Itoa(CHROME_DEBUG_PORT),
	"--enable-experimental-extension-apis",
	"--disable-focus-on-load",
	"--disable-window-activation",

	"--export-tagged-pdf",
	"--generate-pdf-document-outline",

	"--no-pings",
	"--no-first-run",
	"--no-default-browser-check",
	"--no-startup-window",
	"--disable-default-apps",
	"--ash-no-nudges",
	"--disable-infobars",
	"--disable-search-engine-choice-screen",
	"--disable-session-crashed-bubble",
	"--simulate-outdated-no-au=\"Tue, 31 Dec 2099 23:59:59 GMT\"",
	"--hide-crash-restore-bubble",
	"--suppress-message-center-popups",
	"--disable-client-side-phishing-detection",
	"--disable-domain-reliability",
	"--disable-component-update",
	"--disable-datasaver-prompt",
	"--disable-hang-monitor",
	"--disable-session-crashed-bubble",
	"--disable-speech-synthesis-api",
	"--disable-speech-api",
	"--disable-print-preview",
	"--safebrowsing-disable-auto-update",
	"--deny-permission-prompts",
	"--disable-external-intent-requests",
	"--disable-notifications",
	"--disable-desktop-notifications",
	"--noerrdialogs",
	"--disable-popup-blocking",
	"--disable-prompt-on-repost",
	"--silent-debugger-extension-api",
	"--block-new-web-contents",
	"--metrics-recording-only",
	"--disable-breakpad",

	"--disable-features=" + strings.Join(CHROME_DISABLED_COMPONENTS, ","),
	"--enable-features=NetworkService",
}
View Source
var CHROME_DETERMINISTIC_RENDERING_ARGS = []string{
	"--deterministic-mode",
	"--js-flags=--random-seed=1157259159",
	"--force-device-scale-factor=1",
	"--hide-scrollbars",

	"--enable-webgl",
	"--font-render-hinting=none",
	"--force-color-profile=srgb",
	"--disable-partial-raster",
	"--disable-skia-runtime-opts",
	"--disable-2d-canvas-clip-aa",

	"--disable-lazy-loading",
	"--disable-renderer-backgrounding",
	"--disable-background-networking",
	"--disable-background-timer-throttling",
	"--disable-backgrounding-occluded-windows",
	"--disable-ipc-flooding-protection",
	"--disable-extensions-http-throttling",
	"--disable-field-trial-config",
	"--disable-back-forward-cache",
}

flags to make chrome behave more deterministically across different OS"s

View Source
var CHROME_DISABLED_COMPONENTS = []string{
	"Translate",
	"AcceptCHFrame",
	"OptimizationHints",
	"ProcessPerSiteUpToMainFrameThreshold",
	"InterestFeedContentSuggestions",
	"CalculateNativeWinOcclusion",
	"BackForwardCache",
	"HeavyAdPrivacyMitigations",
	"LazyFrameLoading",
	"ImprovedCookieControls",
	"PrivacySandboxSettings4",
	"AutofillServerCommunication",
	"CertificateTransparencyComponentUpdater",
	"DestroyProfileOnBrowserClose",
	"CrashReporting",
	"OverscrollHistoryNavigation",
	"InfiniteSessionRestore",

} // it"s always best to give each chrome instance its own exclusive copy of the user profile
View Source
var CHROME_DISABLE_SECURITY_ARGS = []string{

	"--disable-web-security",
	"--disable-site-isolation-trials",
	"--disable-features=IsolateOrigins,site-per-process",

	"--allow-running-insecure-content",
	"--ignore-certificate-errors",
	"--ignore-ssl-errors",
	"--ignore-certificate-errors-spki-list",
	"--allow-insecure-localhost",
}
View Source
var CHROME_DOCKER_ARGS = []string{

	"--no-sandbox",
	"--disable-gpu-sandbox",
	"--disable-setuid-sandbox",
	"--disable-dev-shm-usage",
	"--no-xshm",
}
View Source
var CHROME_EXTENSIONS = map[string]any{} // coming in a separate PR
View Source
var CHROME_HEADLESS_ARGS = []string{
	"--headless=new",
	"--test-type",
	"--test-type=gpu",
}
View Source
var IN_DOCKER = os.Getenv("IN_DOCKER") == "true"

Functions

func NewURLNotAllowedError

func NewURLNotAllowedError(url string) error

func TabsToString

func TabsToString(tabs []*TabInfo) string

Types

type Browser

type Browser struct {
	Config            BrowserConfig
	Playwright        *playwright.Playwright
	PlaywrightBrowser playwright.Browser
}

func NewBrowser

func NewBrowser(customConfig BrowserConfig) *Browser

func (*Browser) Close

func (b *Browser) Close(options ...playwright.BrowserCloseOptions) error

func (*Browser) GetPlaywrightBrowser

func (b *Browser) GetPlaywrightBrowser() playwright.Browser

Get a browser context

func (*Browser) NewContext

func (b *Browser) NewContext() *BrowserContext

type BrowserConfig

type BrowserConfig = map[string]interface{}

func NewBrowserConfig

func NewBrowserConfig() BrowserConfig

type BrowserContext

type BrowserContext struct {
	ContextId string
	Config    BrowserConfig
	Browser   *Browser
	Session   *BrowserSession
	State     *BrowserContextState
	ActiveTab playwright.Page
	// contains filtered or unexported fields
}

func (*BrowserContext) ClickElementNode

func (bc *BrowserContext) ClickElementNode(elementNode *dom.DOMElementNode) (*string, error)

func (*BrowserContext) Close

func (bc *BrowserContext) Close()

func (*BrowserContext) ConvertSimpleXpathToCssSelector

func (bc *BrowserContext) ConvertSimpleXpathToCssSelector(xpath string) string

func (*BrowserContext) CreateNewTab

func (bc *BrowserContext) CreateNewTab(url string) error

func (*BrowserContext) EnhancedCssSelectorForElement

func (bc *BrowserContext) EnhancedCssSelectorForElement(element *dom.DOMElementNode, includeDynamicAttributes bool) string

func (*BrowserContext) GetCurrentPage

func (bc *BrowserContext) GetCurrentPage() playwright.Page

Get the current page

func (*BrowserContext) GetDomElementByIndex

func (bc *BrowserContext) GetDomElementByIndex(index int) (*dom.DOMElementNode, error)

func (*BrowserContext) GetLocateElement

func (bc *BrowserContext) GetLocateElement(element *dom.DOMElementNode) playwright.Locator

sync DOMElementNode with Playwright

func (*BrowserContext) GetScrollInfo

func (bc *BrowserContext) GetScrollInfo(page playwright.Page) (int, int, error)

Get scroll position information for the current page.

func (*BrowserContext) GetSelectorMap

func (bc *BrowserContext) GetSelectorMap() *dom.SelectorMap

func (*BrowserContext) GetSession

func (bc *BrowserContext) GetSession() *BrowserSession

func (*BrowserContext) GetState

func (bc *BrowserContext) GetState(cacheClickableElementsHashes bool) *BrowserState

func (*BrowserContext) GetTabsInfo

func (bc *BrowserContext) GetTabsInfo() []*TabInfo

func (*BrowserContext) GoBack

func (bc *BrowserContext) GoBack() error

func (*BrowserContext) InputTextElementNode

func (bc *BrowserContext) InputTextElementNode(elementNode *dom.DOMElementNode, text string) error

func (*BrowserContext) IsFileUploader

func (bc *BrowserContext) IsFileUploader(element *dom.DOMElementNode, maxDepth int, currentDepth int) bool

Check if element or its children are file uploaders

func (*BrowserContext) NavigateTo

func (bc *BrowserContext) NavigateTo(url string) error

func (*BrowserContext) PerformClick

func (bc *BrowserContext) PerformClick(clickFunc func(), page playwright.Page) *string

TODO(HIGH): support download path in ClickElementNode

func (*BrowserContext) RemoveHighlights

func (bc *BrowserContext) RemoveHighlights()

Removes all highlight overlays and labels created by the highlightElement function. Handles cases where the page might be closed or inaccessible.

func (*BrowserContext) SwitchToTab

func (bc *BrowserContext) SwitchToTab(pageId int) error

func (*BrowserContext) TakeScreenshot

func (bc *BrowserContext) TakeScreenshot(fullPage bool) (*string, error)

Returns a base64 encoded screenshot of the current page.

type BrowserContextState

type BrowserContextState struct {
	TargetId *string `json:"target_id,omitempty"`
}

State of the browser context

type BrowserError

type BrowserError struct {
	Message string `json:"message"`
}

BrowserError is the base error type for all browser errors.

func (*BrowserError) Error

func (e *BrowserError) Error() string

type BrowserSession

type BrowserSession struct {
	ActiveTab                          playwright.Page
	Context                            playwright.BrowserContext
	CachedState                        *BrowserState
	CachedStateClickableElementsHashes *CachedStateClickableElementsHashes
}

func NewSession

func NewSession(context playwright.BrowserContext, cachedState *BrowserState) *BrowserSession

type BrowserState

type BrowserState struct {
	Url           string              `json:"url"`
	Title         string              `json:"title"`
	Tabs          []*TabInfo          `json:"tabs"`
	Screenshot    *string             `json:"screenshot,omitempty"`
	PixelAbove    int                 `json:"pixel_above"`
	PixelBelow    int                 `json:"pixel_below"`
	BrowserErrors []string            `json:"browser_errors"`
	ElementTree   *dom.DOMElementNode `json:"element_tree"`
	SelectorMap   *dom.SelectorMap    `json:"selector_map"`
}

type BrowserStateHistory

type BrowserStateHistory struct {
	Url               string                   `json:"url"`
	Title             string                   `json:"title"`
	Tabs              []*TabInfo               `json:"tabs"`
	InteractedElement []*dom.DOMHistoryElement `json:"interacted_element"`
}

type CachedStateClickableElementsHashes

type CachedStateClickableElementsHashes struct {
	Url    string
	Hashes []string
}

type GroupTabsAction

type GroupTabsAction struct {
	TabIds []int   `json:"tab_ids"`
	Title  string  `json:"title"`
	Color  *string `json:"color,omitempty"`
}

type TabInfo

type TabInfo struct {
	PageId       int
	Url          string
	Title        string
	ParentPageId *int
}

func (*TabInfo) String

func (ti *TabInfo) String() string

type URLNotAllowedError

type URLNotAllowedError struct {
	BrowserError
}

URLNotAllowedError is returned when a URL is not allowed.

type UngroupTabsAction

type UngroupTabsAction struct {
	TabIds []int `json:"tab_ids"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL