add support for opening multiple tabs
On a single browser, that is. And port the example from _example, proving that it works.
This commit is contained in:
parent
2b925df0fb
commit
a0a36956a8
|
@ -1,31 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/chromedp/chromedp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// first tab
|
||||
ctx1, cancel := chromedp.NewContext(context.Background())
|
||||
defer cancel()
|
||||
|
||||
// create new tab
|
||||
ctx2, _ := chromedp.NewContext(ctx1)
|
||||
|
||||
// runs in first tab
|
||||
if err := chromedp.Run(ctx1, myTask()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// runs in second tab
|
||||
if err := chromedp.Run(ctx2, myTask()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func myTask() chromedp.Tasks {
|
||||
return []chromedp.Action{}
|
||||
}
|
11
context.go
11
context.go
|
@ -19,7 +19,7 @@ type Context struct {
|
|||
|
||||
Browser *Browser
|
||||
|
||||
sessionID target.SessionID
|
||||
SessionID target.SessionID
|
||||
}
|
||||
|
||||
// NewContext creates a browser context using the parent context.
|
||||
|
@ -29,6 +29,9 @@ func NewContext(parent context.Context, opts ...ContextOption) (context.Context,
|
|||
c := &Context{}
|
||||
if pc := FromContext(parent); pc != nil {
|
||||
c.Allocator = pc.Allocator
|
||||
c.Browser = pc.Browser
|
||||
// don't inherit SessionID, so that NewContext can be used to
|
||||
// create a new tab on the same browser.
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
|
@ -69,12 +72,12 @@ func Run(ctx context.Context, action Action) error {
|
|||
}
|
||||
c.Browser = browser
|
||||
}
|
||||
if c.sessionID == "" {
|
||||
if c.SessionID == "" {
|
||||
if err := c.newSession(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return action.Do(ctx, c.Browser.executorForTarget(ctx, c.sessionID))
|
||||
return action.Do(ctx, c.Browser.executorForTarget(ctx, c.SessionID))
|
||||
}
|
||||
|
||||
func (c *Context) newSession(ctx context.Context) error {
|
||||
|
@ -107,7 +110,7 @@ func (c *Context) newSession(ctx context.Context) error {
|
|||
}
|
||||
}
|
||||
|
||||
c.sessionID = sessionID
|
||||
c.SessionID = sessionID
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -76,3 +76,36 @@ func ExampleExecAllocatorOption() {
|
|||
// Output:
|
||||
// DevToolsActivePort has 2 lines
|
||||
}
|
||||
|
||||
func ExampleManyTabs() {
|
||||
// new browser, first tab
|
||||
ctx1, cancel := chromedp.NewContext(context.Background())
|
||||
defer cancel()
|
||||
|
||||
// ensure the first tab is created
|
||||
if err := chromedp.Run(ctx1, chromedp.Tasks{}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// same browser, second tab
|
||||
ctx2, _ := chromedp.NewContext(ctx1)
|
||||
|
||||
// ensure the second tab is created
|
||||
if err := chromedp.Run(ctx2, chromedp.Tasks{}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
c1 := chromedp.FromContext(ctx1)
|
||||
c2 := chromedp.FromContext(ctx2)
|
||||
|
||||
fmt.Printf("Same browser: %t\n", c1.Browser == c2.Browser)
|
||||
fmt.Printf("Same tab: %t\n", c1.SessionID == c2.SessionID)
|
||||
|
||||
// wait for the resources to be cleaned up
|
||||
cancel()
|
||||
c1.Allocator.Wait()
|
||||
|
||||
// Output:
|
||||
// Same browser: true
|
||||
// Same tab: false
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user