Skip to content
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

feat: allow tab to stay opened after context cancel #1093

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion chromedp.go
Expand Up @@ -61,6 +61,10 @@ type Context struct {
// cancel function doesn't do any waiting.
cancel func()

// dontClose configures whether the browser or the tab should be closed
// after the context was canceled.
dontClose bool

// first records whether this context created a brand new Chrome
// process. This is important, because its cancellation should stop the
// entire browser and its handler, and not just a portion of its pages.
Expand Down Expand Up @@ -152,7 +156,7 @@ func NewContext(parent context.Context, opts ...ContextOption) (context.Context,
c.cancelErr = err
}
}
if id := c.Target.TargetID; id != "" {
if id := c.Target.TargetID; !c.dontClose && id != "" {
action := target.CloseTarget(id)
if err := action.Do(cdp.WithExecutor(ctx, c.Browser)); c.cancelErr == nil && err != nil {
c.cancelErr = err
Expand Down Expand Up @@ -436,6 +440,11 @@ func WithBrowserOption(opts ...BrowserOption) ContextOption {
}
}

// WithDontClose allows keeping the browser / tab running after disconnecting closing the context
func WithDontClose() ContextOption {
return func(c *Context) { c.dontClose = true }
}

// RunResponse is an alternative to Run which can be used with a list of actions
// that trigger a page navigation, such as clicking on a link or button.
//
Expand Down