2018-12-22 06:14:41 +02:00
|
|
|
package html
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers"
|
2018-12-22 06:14:41 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
2019-02-13 19:31:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
2018-12-22 06:14:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Screenshot takes a screenshot of the current page.
|
|
|
|
// @param source (Document) - Document.
|
|
|
|
// @param params (Object) - Optional, An object containing the following properties :
|
|
|
|
// x (Float|Int) - Optional, X position of the viewport.
|
|
|
|
// x (Float|Int) - Optional,Y position of the viewport.
|
|
|
|
// width (Float|Int) - Optional, Width of the viewport.
|
|
|
|
// height (Float|Int) - Optional, Height of the viewport.
|
|
|
|
// format (String) - Optional, Either "jpeg" or "png".
|
|
|
|
// quality (Int) - Optional, Quality, in [0, 100], only for jpeg format.
|
|
|
|
// @returns data (Binary) - Returns a base64 encoded string in binary format.
|
|
|
|
func Screenshot(ctx context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 1, 2)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
arg1 := args[0]
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
err = core.ValidateType(arg1, drivers.HTMLDocumentType, types.String)
|
2018-12-22 06:14:41 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := ValidateDocument(ctx, arg1)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
doc := val.(drivers.HTMLDocument)
|
2018-12-22 06:14:41 +02:00
|
|
|
|
|
|
|
defer doc.Close()
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
screenshotParams := drivers.ScreenshotParams{
|
2018-12-22 06:14:41 +02:00
|
|
|
X: 0,
|
|
|
|
Y: 0,
|
|
|
|
Width: -1,
|
|
|
|
Height: -1,
|
2019-02-20 01:10:18 +02:00
|
|
|
Format: drivers.ScreenshotFormatJPEG,
|
2018-12-22 06:14:41 +02:00
|
|
|
Quality: 100,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 2 {
|
|
|
|
arg2 := args[1]
|
2019-02-13 19:31:18 +02:00
|
|
|
err = core.ValidateType(arg2, types.Object)
|
2018-12-22 06:14:41 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
params, ok := arg2.(*values.Object)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return values.None, core.Error(core.ErrInvalidType, "expected object")
|
|
|
|
}
|
|
|
|
|
|
|
|
format, found := params.Get("format")
|
|
|
|
|
|
|
|
if found {
|
2019-02-13 19:31:18 +02:00
|
|
|
err = core.ValidateType(format, types.String)
|
2018-12-22 06:14:41 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
if !drivers.IsScreenshotFormatValid(format.String()) {
|
2018-12-22 06:14:41 +02:00
|
|
|
return values.None, core.Error(
|
|
|
|
core.ErrInvalidArgument,
|
|
|
|
fmt.Sprintf("format is not valid, expected jpeg or png, but got %s", format.String()))
|
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
screenshotParams.Format = drivers.ScreenshotFormat(format.String())
|
2018-12-22 06:14:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
x, found := params.Get("x")
|
|
|
|
|
|
|
|
if found {
|
2019-02-13 19:31:18 +02:00
|
|
|
err = core.ValidateType(x, types.Float, types.Int)
|
2018-12-22 06:14:41 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
if x.Type() == types.Int {
|
2018-12-22 06:14:41 +02:00
|
|
|
screenshotParams.X = values.Float(x.(values.Int))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
y, found := params.Get("y")
|
|
|
|
|
|
|
|
if found {
|
2019-02-13 19:31:18 +02:00
|
|
|
err = core.ValidateType(y, types.Float, types.Int)
|
2018-12-22 06:14:41 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
if y.Type() == types.Int {
|
2018-12-22 06:14:41 +02:00
|
|
|
screenshotParams.Y = values.Float(y.(values.Int))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
width, found := params.Get("width")
|
|
|
|
|
|
|
|
if found {
|
2019-02-13 19:31:18 +02:00
|
|
|
err = core.ValidateType(width, types.Float, types.Int)
|
2018-12-22 06:14:41 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
if width.Type() == types.Int {
|
2018-12-22 06:14:41 +02:00
|
|
|
screenshotParams.Width = values.Float(width.(values.Int))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
height, found := params.Get("height")
|
|
|
|
|
|
|
|
if found {
|
2019-02-13 19:31:18 +02:00
|
|
|
err = core.ValidateType(height, types.Float, types.Int)
|
2018-12-22 06:14:41 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
if height.Type() == types.Int {
|
2018-12-22 06:14:41 +02:00
|
|
|
screenshotParams.Height = values.Float(height.(values.Int))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
quality, found := params.Get("quality")
|
|
|
|
|
|
|
|
if found {
|
2019-02-13 19:31:18 +02:00
|
|
|
err = core.ValidateType(quality, types.Int)
|
2018-12-22 06:14:41 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
screenshotParams.Quality = quality.(values.Int)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scr, err := doc.CaptureScreenshot(screenshotParams)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return scr, nil
|
|
|
|
}
|