2017-02-08 07:27:39 +00:00
|
|
|
package chromedp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/knq/chromedp/cdp"
|
|
|
|
rundom "github.com/knq/chromedp/cdp/runtime"
|
|
|
|
)
|
|
|
|
|
2017-02-08 08:18:55 +00:00
|
|
|
// Evaluate evaluates the Javascript expression, unmarshaling the result of the
|
|
|
|
// script evaluation to res.
|
2017-02-08 07:27:39 +00:00
|
|
|
//
|
2017-02-08 08:18:55 +00:00
|
|
|
// If res is *[]byte, then the result of the script evaluation will be returned
|
|
|
|
// "by value" (ie, JSON-encoded) and res will be set to the raw value.
|
|
|
|
//
|
|
|
|
// Alternatively, if res is **chromedp/cdp/runtime.RemoteObject, then it will
|
|
|
|
// be set to the returned RemoteObject and no attempt will be made to convert
|
|
|
|
// the value to an equivalent Go type.
|
|
|
|
//
|
|
|
|
// Otherwise, if res is any other Go type, the result of the script evaluation
|
|
|
|
// will be returned "by value" (ie, JSON-encoded), and subsequently will be
|
|
|
|
// json.Unmarshal'd into res.
|
|
|
|
//
|
|
|
|
// Note: any exception encountered will be returned as an error.
|
2017-02-08 07:27:39 +00:00
|
|
|
func Evaluate(expression string, res interface{}, opts ...EvaluateOption) Action {
|
|
|
|
if res == nil {
|
|
|
|
panic("res cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ActionFunc(func(ctxt context.Context, h cdp.FrameHandler) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// set up parameters
|
|
|
|
p := rundom.Evaluate(expression)
|
2017-02-08 08:18:55 +00:00
|
|
|
switch res.(type) {
|
|
|
|
case **rundom.RemoteObject:
|
|
|
|
default:
|
2017-02-08 07:27:39 +00:00
|
|
|
p = p.WithReturnByValue(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply opts
|
|
|
|
for _, o := range opts {
|
|
|
|
p = o(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// evaluate
|
|
|
|
v, exp, err := p.Do(ctxt, h)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if exp != nil {
|
|
|
|
return exp
|
|
|
|
}
|
|
|
|
|
2017-02-08 08:18:55 +00:00
|
|
|
switch x := res.(type) {
|
|
|
|
case **rundom.RemoteObject:
|
|
|
|
*x = v
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case *[]byte:
|
|
|
|
*x = []byte(v.Value)
|
2017-02-08 07:27:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal
|
|
|
|
return json.Unmarshal(v.Value, res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-08 08:18:55 +00:00
|
|
|
// EvaluateAsDevTools is an action that evaluates a Javascript expression in
|
|
|
|
// the same context as Chrome DevTools would, exposing the Command Line API to
|
|
|
|
// the script evaluating the expression in the "console" context.
|
|
|
|
//
|
|
|
|
// Note: this should not be used with any untrusted code.
|
|
|
|
func EvaluateAsDevTools(expression string, res interface{}, opts ...EvaluateOption) Action {
|
|
|
|
return Evaluate(expression, res, append(opts, EvalObjectGroup("console"), EvalWithCommandLineAPI)...)
|
|
|
|
}
|
|
|
|
|
2017-02-08 07:27:39 +00:00
|
|
|
// EvaluateOption is an Evaluate call option.
|
|
|
|
type EvaluateOption func(*rundom.EvaluateParams) *rundom.EvaluateParams
|
|
|
|
|
2017-02-08 08:18:55 +00:00
|
|
|
// EvalObjectGroup is a evaluate option to set the object group.
|
2017-02-08 07:27:39 +00:00
|
|
|
func EvalObjectGroup(objectGroup string) EvaluateOption {
|
|
|
|
return func(p *rundom.EvaluateParams) *rundom.EvaluateParams {
|
|
|
|
return p.WithObjectGroup(objectGroup)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 08:18:55 +00:00
|
|
|
// EvalWithCommandLineAPI is an evaluate option to make the DevTools Command
|
|
|
|
// Line API available to the evaluated script.
|
2017-02-08 07:27:39 +00:00
|
|
|
//
|
|
|
|
// Note: this should not be used with any untrusted code.
|
|
|
|
func EvalWithCommandLineAPI(p *rundom.EvaluateParams) *rundom.EvaluateParams {
|
|
|
|
return p.WithIncludeCommandLineAPI(true)
|
|
|
|
}
|
|
|
|
|
2017-02-08 08:18:55 +00:00
|
|
|
// EvalSilent is a evaluate option that will cause script evaluation to ignore
|
2017-02-08 07:27:39 +00:00
|
|
|
// exceptions.
|
|
|
|
func EvalSilent(p *rundom.EvaluateParams) *rundom.EvaluateParams {
|
|
|
|
return p.WithSilent(true)
|
|
|
|
}
|
|
|
|
|
2017-02-08 08:18:55 +00:00
|
|
|
// EvalAsValue is a evaluate option that will case the script to encode its
|
2017-02-08 07:27:39 +00:00
|
|
|
// result as a value.
|
|
|
|
func EvalAsValue(p *rundom.EvaluateParams) *rundom.EvaluateParams {
|
|
|
|
return p.WithReturnByValue(true)
|
|
|
|
}
|