2017-02-08 07:27:39 +00:00
|
|
|
package chromedp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
2017-12-27 02:30:28 +00:00
|
|
|
"github.com/chromedp/cdproto/cdp"
|
|
|
|
rundom "github.com/chromedp/cdproto/runtime"
|
2017-02-08 07:27:39 +00:00
|
|
|
)
|
|
|
|
|
2017-02-12 04:59:33 +00:00
|
|
|
// Evaluate is an action to evaluate the Javascript expression, unmarshaling
|
|
|
|
// the result of the script evaluation to res.
|
2017-02-08 07:27:39 +00:00
|
|
|
//
|
2017-02-12 04:59:33 +00:00
|
|
|
// When res is a type other than *[]byte, or **chromedp/cdp/runtime.RemoteObject,
|
|
|
|
// then the result of the script evaluation will be returned "by value" (ie,
|
|
|
|
// JSON-encoded), and subsequently an attempt will be made to json.Unmarshal
|
|
|
|
// the script result to res.
|
2017-02-08 08:18:55 +00:00
|
|
|
//
|
2017-02-12 04:59:33 +00:00
|
|
|
// Otherwise, when res is a *[]byte, the raw JSON-encoded value of the script
|
|
|
|
// result will be placed in res. Similarly, if res is a *runtime.RemoteObject,
|
|
|
|
// then res will be set to the low-level protocol type, and no attempt will be
|
|
|
|
// made to convert the result.
|
2017-02-08 08:18:55 +00:00
|
|
|
//
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
2017-12-27 02:30:28 +00:00
|
|
|
return ActionFunc(func(ctxt context.Context, h cdp.Executor) error {
|
2017-02-08 07:27:39 +00:00
|
|
|
// 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-12 04:59:33 +00:00
|
|
|
// EvaluateAsDevTools is an action that evaluates a Javascript expression as
|
|
|
|
// Chrome DevTools would, evaluating the expression in the "console" context,
|
|
|
|
// and making the Command Line API available to the script.
|
2017-02-08 08:18:55 +00:00
|
|
|
//
|
2017-02-12 04:59:33 +00:00
|
|
|
// Note: this should not be used with untrusted Javascript.
|
2017-02-08 08:18:55 +00:00
|
|
|
func EvaluateAsDevTools(expression string, res interface{}, opts ...EvaluateOption) Action {
|
|
|
|
return Evaluate(expression, res, append(opts, EvalObjectGroup("console"), EvalWithCommandLineAPI)...)
|
|
|
|
}
|
|
|
|
|
2017-03-02 02:53:47 +00:00
|
|
|
// EvaluateOption is the type for script evaluation options.
|
2017-02-08 07:27:39 +00:00
|
|
|
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
|
|
|
//
|
2017-02-12 04:59:33 +00:00
|
|
|
// Note: this should not be used with untrusted Javascript.
|
2017-02-08 07:27:39 +00:00
|
|
|
func EvalWithCommandLineAPI(p *rundom.EvaluateParams) *rundom.EvaluateParams {
|
|
|
|
return p.WithIncludeCommandLineAPI(true)
|
|
|
|
}
|
|
|
|
|
2017-02-12 04:59:33 +00:00
|
|
|
// EvalIgnoreExceptions is a evaluate option that will cause script evaluation
|
|
|
|
// to ignore exceptions.
|
|
|
|
func EvalIgnoreExceptions(p *rundom.EvaluateParams) *rundom.EvaluateParams {
|
2017-02-08 07:27:39 +00:00
|
|
|
return p.WithSilent(true)
|
|
|
|
}
|
|
|
|
|
2017-02-12 04:59:33 +00:00
|
|
|
// EvalAsValue is a evaluate option that will cause the evaluated script to
|
|
|
|
// encode the result of the expression as a JSON-encoded value.
|
2017-02-08 07:27:39 +00:00
|
|
|
func EvalAsValue(p *rundom.EvaluateParams) *rundom.EvaluateParams {
|
|
|
|
return p.WithReturnByValue(true)
|
|
|
|
}
|