chromedp/nav.go

131 lines
3.3 KiB
Go
Raw Normal View History

2017-01-24 15:09:23 +00:00
package chromedp
import (
"context"
"errors"
2017-12-27 02:30:28 +00:00
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/page"
2017-01-24 15:09:23 +00:00
)
// Navigate navigates the current frame.
func Navigate(urlstr string) Action {
return ActionFunc(func(ctx context.Context, h cdp.Executor) error {
_, _, _, err := page.Navigate(urlstr).Do(ctx, h)
return err
2017-01-24 15:09:23 +00:00
})
}
// NavigationEntries is an action to retrieve the page's navigation history
// entries.
func NavigationEntries(currentIndex *int64, entries *[]*page.NavigationEntry) Action {
if currentIndex == nil || entries == nil {
panic("currentIndex and entries cannot be nil")
}
return ActionFunc(func(ctx context.Context, h cdp.Executor) error {
2017-01-24 15:09:23 +00:00
var err error
*currentIndex, *entries, err = page.GetNavigationHistory().Do(ctx, h)
2017-01-24 15:09:23 +00:00
return err
})
}
// NavigateToHistoryEntry is an action to navigate to the specified navigation
// entry.
func NavigateToHistoryEntry(entryID int64) Action {
return page.NavigateToHistoryEntry(entryID)
}
// NavigateBack navigates the current frame backwards in its history.
2017-02-22 13:57:02 +00:00
func NavigateBack() Action {
return ActionFunc(func(ctx context.Context, h cdp.Executor) error {
cur, entries, err := page.GetNavigationHistory().Do(ctx, h)
2017-02-22 13:57:02 +00:00
if err != nil {
return err
}
2017-01-24 15:09:23 +00:00
2017-02-22 13:57:02 +00:00
if cur <= 0 || cur > int64(len(entries)-1) {
return errors.New("invalid navigation entry")
}
2017-01-24 15:09:23 +00:00
return page.NavigateToHistoryEntry(entries[cur-1].ID).Do(ctx, h)
2017-02-22 13:57:02 +00:00
})
2017-01-24 15:09:23 +00:00
}
// NavigateForward navigates the current frame forwards in its history.
2017-02-22 13:57:02 +00:00
func NavigateForward() Action {
return ActionFunc(func(ctx context.Context, h cdp.Executor) error {
cur, entries, err := page.GetNavigationHistory().Do(ctx, h)
2017-02-22 13:57:02 +00:00
if err != nil {
return err
}
2017-01-24 15:09:23 +00:00
2017-02-22 13:57:02 +00:00
if cur < 0 || cur >= int64(len(entries)-1) {
return errors.New("invalid navigation entry")
}
2017-01-24 15:09:23 +00:00
return page.NavigateToHistoryEntry(entries[cur+1].ID).Do(ctx, h)
2017-02-22 13:57:02 +00:00
})
2017-01-24 15:09:23 +00:00
}
// Stop stops all navigation and pending resource retrieval.
func Stop() Action {
return page.StopLoading()
}
// Reload reloads the current page.
func Reload() Action {
return page.Reload()
}
// CaptureScreenshot captures takes a screenshot of the current viewport.
//
// Note: this an alias for page.CaptureScreenshot.
2017-01-24 15:09:23 +00:00
func CaptureScreenshot(res *[]byte) Action {
if res == nil {
panic("res cannot be nil")
}
return ActionFunc(func(ctx context.Context, h cdp.Executor) error {
2017-01-24 15:09:23 +00:00
var err error
*res, err = page.CaptureScreenshot().Do(ctx, h)
2017-01-24 15:09:23 +00:00
return err
})
}
// AddOnLoadScript adds a script to evaluate on page load.
/*func AddOnLoadScript(source string, id *page.ScriptIdentifier) Action {
2017-01-24 15:09:23 +00:00
if id == nil {
panic("id cannot be nil")
}
return ActionFunc(func(ctx context.Context, h cdp.Executor) error {
2017-01-24 15:09:23 +00:00
var err error
*id, err = page.AddScriptToEvaluateOnLoad(source).Do(ctx, h)
2017-01-24 15:09:23 +00:00
return err
})
}
// RemoveOnLoadScript removes a script to evaluate on page load.
func RemoveOnLoadScript(id page.ScriptIdentifier) Action {
return page.RemoveScriptToEvaluateOnLoad(id)
}*/
2017-01-24 15:09:23 +00:00
// Location retrieves the document location.
2017-01-24 15:09:23 +00:00
func Location(urlstr *string) Action {
if urlstr == nil {
panic("urlstr cannot be nil")
}
return EvaluateAsDevTools(`document.location.toString()`, urlstr)
}
// Title retrieves the document title.
func Title(title *string) Action {
if title == nil {
panic("title cannot be nil")
}
return EvaluateAsDevTools(`document.title`, title)
2017-01-24 15:09:23 +00:00
}