2019-02-21 04:24:05 +02:00
|
|
|
package drivers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-07-03 20:05:02 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
2019-02-21 04:24:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func WithDefaultTimeout(ctx context.Context) (context.Context, context.CancelFunc) {
|
|
|
|
return context.WithTimeout(ctx, DefaultTimeout)
|
|
|
|
}
|
2019-06-19 23:58:56 +02:00
|
|
|
|
|
|
|
func ToPage(value core.Value) (HTMLPage, error) {
|
|
|
|
err := core.ValidateType(value, HTMLPageType)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return value.(HTMLPage), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ToDocument(value core.Value) (HTMLDocument, error) {
|
|
|
|
switch v := value.(type) {
|
|
|
|
case HTMLPage:
|
|
|
|
return v.GetMainFrame(), nil
|
|
|
|
case HTMLDocument:
|
|
|
|
return v, nil
|
|
|
|
default:
|
|
|
|
return nil, core.TypeError(
|
|
|
|
value.Type(),
|
|
|
|
HTMLPageType,
|
|
|
|
HTMLDocumentType,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ToElement(value core.Value) (HTMLElement, error) {
|
|
|
|
switch v := value.(type) {
|
|
|
|
case HTMLPage:
|
|
|
|
return v.GetMainFrame().GetElement(), nil
|
|
|
|
case HTMLDocument:
|
|
|
|
return v.GetElement(), nil
|
|
|
|
case HTMLElement:
|
|
|
|
return v, nil
|
|
|
|
default:
|
|
|
|
return nil, core.TypeError(
|
|
|
|
value.Type(),
|
|
|
|
HTMLPageType,
|
|
|
|
HTMLDocumentType,
|
|
|
|
HTMLElementType,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|