67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package cdp
|
|
|
|
import (
|
|
"context"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"git.loafle.net/chrome_devtools_protocol/cdp-go/pkg/cdp"
|
|
"git.loafle.net/chrome_devtools_protocol/protocol-go/pkg/page"
|
|
v1 "git.loafle.net/odds_crawler/odds-crawler-proto/pkg/api/v1"
|
|
)
|
|
|
|
const (
|
|
// apiVersion is version of API is provided by server
|
|
apiVersion = "v1"
|
|
)
|
|
|
|
// cdpServiceServer is implementation of v1.CDPServiceServer proto interface
|
|
type cdpServiceServer struct {
|
|
ctx context.Context
|
|
cancel context.CancelFunc
|
|
}
|
|
|
|
// NewCDPServiceServer creates ToDo service
|
|
func NewCDPServiceServer() v1.CDPServiceServer {
|
|
ctx, cancel := cdp.NewContext(context.Background())
|
|
|
|
return &cdpServiceServer{
|
|
ctx: ctx,
|
|
cancel: cancel,
|
|
}
|
|
}
|
|
|
|
func (s *cdpServiceServer) checkAPI(api string) error {
|
|
// API version is "" means use current version of the service
|
|
if len(api) > 0 {
|
|
if apiVersion != api {
|
|
return status.Errorf(codes.Unimplemented,
|
|
"unsupported API version: service implements API version '%s', but asked for '%s'", apiVersion, api)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *cdpServiceServer) Navigate(ctx context.Context, req *v1.NavigateRequest) (*v1.NavigateReply, error) {
|
|
if err := s.checkAPI(req.Api); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
params := page.Navigate(req.Url)
|
|
params.WithReferrer(req.Referrer)
|
|
params.WithTransitionType(req.TransitionType)
|
|
params.WithFrameID(req.FrameID)
|
|
|
|
frameID, loaderID, errorText, err := params.Do(s.ctx)
|
|
if nil != err {
|
|
return nil, err
|
|
}
|
|
|
|
return &v1.NavigateReply{
|
|
FrameID: frameID,
|
|
LoaderID: loaderID,
|
|
ErrorText: errorText,
|
|
}, nil
|
|
}
|