github.com/MontFerret/ferret@v0.18.0/pkg/drivers/cdp/network/event.go (about)

     1  package network
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/mafredri/cdp/protocol/page"
     7  	"github.com/wI2L/jettison"
     8  
     9  	"github.com/MontFerret/ferret/pkg/drivers/cdp/dom"
    10  
    11  	"github.com/MontFerret/ferret/pkg/runtime/core"
    12  	"github.com/MontFerret/ferret/pkg/runtime/values"
    13  )
    14  
    15  var NavigationEventType = core.NewType("ferret.drivers.cdp.network.NavigationEvent")
    16  
    17  type NavigationEvent struct {
    18  	URL      string
    19  	FrameID  page.FrameID
    20  	MimeType string
    21  }
    22  
    23  func (evt *NavigationEvent) MarshalJSON() ([]byte, error) {
    24  	if evt == nil {
    25  		return values.None.MarshalJSON()
    26  	}
    27  
    28  	return jettison.MarshalOpts(map[string]string{
    29  		"url":      evt.URL,
    30  		"frame_id": string(evt.FrameID),
    31  	}, jettison.NoHTMLEscaping())
    32  }
    33  
    34  func (evt *NavigationEvent) Type() core.Type {
    35  	return NavigationEventType
    36  }
    37  
    38  func (evt *NavigationEvent) String() string {
    39  	return evt.URL
    40  }
    41  
    42  func (evt *NavigationEvent) Compare(other core.Value) int64 {
    43  	if other.Type() != NavigationEventType {
    44  		return -1
    45  	}
    46  
    47  	otherEvt := other.(*NavigationEvent)
    48  	comp := values.NewString(evt.URL).Compare(values.NewString(otherEvt.URL))
    49  
    50  	if comp != 0 {
    51  		return comp
    52  	}
    53  
    54  	return values.String(evt.FrameID).Compare(values.String(otherEvt.FrameID))
    55  }
    56  
    57  func (evt *NavigationEvent) Unwrap() interface{} {
    58  	return evt
    59  }
    60  
    61  func (evt *NavigationEvent) Hash() uint64 {
    62  	return values.Parse(evt).Hash()
    63  }
    64  
    65  func (evt *NavigationEvent) Copy() core.Value {
    66  	return *(&evt)
    67  }
    68  
    69  func (evt *NavigationEvent) GetIn(_ context.Context, path []core.Value) (core.Value, core.PathError) {
    70  	if len(path) == 0 {
    71  		return evt, nil
    72  	}
    73  
    74  	switch path[0].String() {
    75  	case "url", "URL":
    76  		return values.NewString(evt.URL), nil
    77  	case "frame":
    78  		return dom.NewFrameID(evt.FrameID), nil
    79  	default:
    80  		return values.None, nil
    81  	}
    82  }