-
Notifications
You must be signed in to change notification settings - Fork 26
feat(jshttp): optional Firefox/WebKit browser engine + ExecutablePath override #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
knoellp
wants to merge
2
commits into
gosom:main
Choose a base branch
from
knoellp:pr/optional-browser-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,12 +21,21 @@ type JSFetcherOptions struct { | |
| PageReuseLimit int | ||
| BrowserReuseLimit int | ||
| UserAgent string | ||
| // BrowserType selects the Playwright browser engine. Accepted values are | ||
| // "chromium" (the default, also for the empty string), "firefox" and | ||
| // "webkit". Callers that never set this field keep the existing Chromium | ||
| // behaviour unchanged. | ||
| BrowserType string | ||
| // ExecutablePath, when non-empty, overrides the Playwright-managed browser | ||
| // binary (for example a custom Firefox build). Empty uses the bundled binary. | ||
| ExecutablePath string | ||
| } | ||
|
|
||
| //nolint:gocritic // Keep value parameter to preserve the public constructor API. | ||
| func New(params JSFetcherOptions) (scrapemate.HTTPFetcher, error) { | ||
| opts := []*playwright.RunOptions{ | ||
| { | ||
| Browsers: []string{"chromium"}, | ||
| Browsers: browsersToInstall(params.BrowserType), | ||
| Verbose: true, | ||
| }, | ||
| } | ||
|
|
@@ -75,11 +84,13 @@ func New(params JSFetcherOptions) (scrapemate.HTTPFetcher, error) { | |
| poolSize: params.PoolSize, | ||
| maxPagesPerBrowser: maxPagesPerBrowser, | ||
| factory: playwrightSlotFactory{ | ||
| pw: pw, | ||
| headless: params.Headless, | ||
| disableImages: params.DisableImages, | ||
| proxyPool: pool, | ||
| ua: params.UserAgent, | ||
| pw: pw, | ||
| headless: params.Headless, | ||
| disableImages: params.DisableImages, | ||
| proxyPool: pool, | ||
| ua: params.UserAgent, | ||
| browserType: params.BrowserType, | ||
| executablePath: params.ExecutablePath, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
|
|
@@ -94,11 +105,13 @@ func New(params JSFetcherOptions) (scrapemate.HTTPFetcher, error) { | |
| ans.slots = make(chan *sessionSlot, params.PoolSize) | ||
|
|
||
| sessionFactory := &playwrightRuntimeFactory{ | ||
| pw: pw, | ||
| headless: params.Headless, | ||
| disableImages: params.DisableImages, | ||
| proxyPool: pool, | ||
| ua: params.UserAgent, | ||
| pw: pw, | ||
| headless: params.Headless, | ||
| disableImages: params.DisableImages, | ||
| proxyPool: pool, | ||
| ua: params.UserAgent, | ||
| browserType: params.BrowserType, | ||
| executablePath: params.ExecutablePath, | ||
| } | ||
| ans.factory = sessionFactory | ||
|
|
||
|
|
@@ -254,39 +267,85 @@ func (o *browser) Close() { | |
| _ = o.browser.Close() | ||
| } | ||
|
|
||
| func newBrowser(pw *playwright.Playwright, headless, disableImages bool, proxyPool *ProxyPool, ua string) (*browser, error) { | ||
| // browsersToInstall maps a BrowserType value to the Playwright install list. | ||
| // An empty string or "chromium" both install Chromium so that callers that | ||
| // never set BrowserType are unaffected. | ||
| func browsersToInstall(browserType string) []string { | ||
| switch browserType { | ||
| case "firefox": | ||
| return []string{"firefox"} | ||
| case "webkit": | ||
| return []string{"webkit"} | ||
| default: | ||
| return []string{"chromium"} | ||
| } | ||
| } | ||
|
|
||
| // chromiumLaunchArgs are the Chromium-specific command-line flags. They are only | ||
| // passed when launching Chromium: Firefox and WebKit reject or mishandle these | ||
| // flags, and forwarding them causes Firefox to hang on the first NewPage call. | ||
| func chromiumLaunchArgs(disableImages bool) []string { | ||
| args := []string{ | ||
| `--start-maximized`, | ||
| `--no-default-browser-check`, | ||
| `--disable-dev-shm-usage`, | ||
| `--no-sandbox`, | ||
| `--disable-setuid-sandbox`, | ||
| `--no-zygote`, | ||
| `--disable-gpu`, | ||
| `--mute-audio`, | ||
| `--disable-extensions`, | ||
| `--single-process`, | ||
| `--disable-breakpad`, | ||
| `--disable-features=TranslateUI,BlinkGenPropertyTrees`, | ||
| `--disable-ipc-flooding-protection`, | ||
| `--enable-features=NetworkService,NetworkServiceInProcess`, | ||
| "--enable-features=NetworkService", | ||
| `--disable-default-apps`, | ||
| `--disable-notifications`, | ||
| `--disable-webgl`, | ||
| `--disable-blink-features=AutomationControlled`, | ||
| "--ignore-certificate-errors", | ||
| "--ignore-certificate-errors-spki-list", | ||
| "--disable-web-security", | ||
| } | ||
| if disableImages { | ||
| args = append(args, `--blink-settings=imagesEnabled=false`) | ||
| } | ||
|
|
||
| return args | ||
| } | ||
|
|
||
| // browserTypeFor returns the playwright.BrowserType for the configured engine. | ||
| // Empty or "chromium" return Chromium so existing callers are unaffected. | ||
| func browserTypeFor(pw *playwright.Playwright, browserType string) playwright.BrowserType { | ||
| switch browserType { | ||
| case "firefox": | ||
| return pw.Firefox | ||
| case "webkit": | ||
| return pw.WebKit | ||
| default: | ||
| return pw.Chromium | ||
| } | ||
| } | ||
|
|
||
| func newBrowser(pw *playwright.Playwright, headless, disableImages bool, proxyPool *ProxyPool, ua, browserType, executablePath string) (*browser, error) { | ||
| opts := playwright.BrowserTypeLaunchOptions{ | ||
| Headless: playwright.Bool(headless), | ||
| Args: []string{ | ||
| `--start-maximized`, | ||
| `--no-default-browser-check`, | ||
| `--disable-dev-shm-usage`, | ||
| `--no-sandbox`, | ||
| `--disable-setuid-sandbox`, | ||
| `--no-zygote`, | ||
| `--disable-gpu`, | ||
| `--mute-audio`, | ||
| `--disable-extensions`, | ||
| `--single-process`, | ||
| `--disable-breakpad`, | ||
| `--disable-features=TranslateUI,BlinkGenPropertyTrees`, | ||
| `--disable-ipc-flooding-protection`, | ||
| `--enable-features=NetworkService,NetworkServiceInProcess`, | ||
| "--enable-features=NetworkService", | ||
| `--disable-default-apps`, | ||
| `--disable-notifications`, | ||
| `--disable-webgl`, | ||
| `--disable-blink-features=AutomationControlled`, | ||
| "--ignore-certificate-errors", | ||
| "--ignore-certificate-errors-spki-list", | ||
| "--disable-web-security", | ||
| }, | ||
| } | ||
| if disableImages { | ||
| opts.Args = append(opts.Args, `--blink-settings=imagesEnabled=false`) | ||
|
|
||
| // Chromium launch flags only apply to Chromium. Firefox/WebKit use the | ||
| // Playwright engine defaults; forwarding Chromium flags hangs Firefox at | ||
| // the first NewPage. | ||
| if browserType == "" || browserType == "chromium" { | ||
| opts.Args = chromiumLaunchArgs(disableImages) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❓ Is there an option to disable image on firefox/webkit? If yes maybe it can be added (optional) |
||
| } | ||
|
|
||
| if executablePath != "" { | ||
| opts.ExecutablePath = playwright.String(executablePath) | ||
| } | ||
|
|
||
| br, err := pw.Chromium.Launch(opts) | ||
| br, err := browserTypeFor(pw, browserType).Launch(opts) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package jshttp //nolint:testpackage // Need access to unexported browser selection helpers. | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestBrowsersToInstall(t *testing.T) { | ||
| cases := map[string]string{ | ||
| "": "chromium", | ||
| "chromium": "chromium", | ||
| "firefox": "firefox", | ||
| "webkit": "webkit", | ||
| "unknown": "chromium", // unknown values fall back to chromium | ||
| } | ||
|
|
||
| for in, want := range cases { | ||
| got := browsersToInstall(in) | ||
| if len(got) != 1 || got[0] != want { | ||
| t.Errorf("browsersToInstall(%q) = %v; want [%q]", in, got, want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestChromiumLaunchArgs_DisableImages(t *testing.T) { | ||
| const imgFlag = "--blink-settings=imagesEnabled=false" | ||
|
|
||
| with := chromiumLaunchArgs(true) | ||
| if !contains(with, imgFlag) { | ||
| t.Errorf("chromiumLaunchArgs(true) missing %q", imgFlag) | ||
| } | ||
|
|
||
| without := chromiumLaunchArgs(false) | ||
| if contains(without, imgFlag) { | ||
| t.Errorf("chromiumLaunchArgs(false) unexpectedly contains %q", imgFlag) | ||
| } | ||
|
|
||
| // The core Chromium flags must always be present. | ||
| if !contains(without, "--no-sandbox") { | ||
| t.Error("chromiumLaunchArgs missing --no-sandbox") | ||
| } | ||
| } | ||
|
|
||
| func contains(s []string, v string) bool { | ||
| for _, x := range s { | ||
| if x == v { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If not mistaken when the
uapassed is empty then a default will be used.See line 348:
when a Firefox is returned and ua == '' then
the user agent set will be the one from line 358.
Probably the default UA for firefox/webkit should be used there