github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/cookie_get.go (about)

     1  package html
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/MontFerret/ferret/pkg/drivers"
     7  	"github.com/MontFerret/ferret/pkg/runtime/core"
     8  	"github.com/MontFerret/ferret/pkg/runtime/values"
     9  	"github.com/MontFerret/ferret/pkg/runtime/values/types"
    10  )
    11  
    12  // COOKIE_GET gets a cookie from a given page by name.
    13  // @param {HTMLPage} page - Target page.
    14  // @param {String} name - Cookie or cookie name to delete.
    15  // @return {HTTPCookie} - Cookie if found, otherwise None.
    16  func CookieGet(ctx context.Context, args ...core.Value) (core.Value, error) {
    17  	err := core.ValidateArgs(args, 2, 2)
    18  
    19  	if err != nil {
    20  		return values.None, err
    21  	}
    22  
    23  	page, err := drivers.ToPage(args[0])
    24  
    25  	if err != nil {
    26  		return values.None, err
    27  	}
    28  
    29  	err = core.ValidateType(args[1], types.String)
    30  
    31  	if err != nil {
    32  		return values.None, err
    33  	}
    34  
    35  	name := args[1].(values.String)
    36  
    37  	cookies, err := page.GetCookies(ctx)
    38  
    39  	if err != nil {
    40  		return values.None, err
    41  	}
    42  
    43  	cookie, found := cookies.Get(name)
    44  
    45  	if found {
    46  		return cookie, nil
    47  	}
    48  
    49  	return values.None, nil
    50  }