github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/cookie_set.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  )
    10  
    11  // COOKIE_SET sets cookies to a given page
    12  // @param {HTMLPage} page - Target page.
    13  // @param {HTTPCookie, repeated} cookies - Target cookies.
    14  func CookieSet(ctx context.Context, args ...core.Value) (core.Value, error) {
    15  	err := core.ValidateArgs(args, 2, core.MaxArgs)
    16  
    17  	if err != nil {
    18  		return values.None, err
    19  	}
    20  
    21  	page, err := drivers.ToPage(args[0])
    22  
    23  	if err != nil {
    24  		return values.None, err
    25  	}
    26  
    27  	cookies := drivers.NewHTTPCookies()
    28  
    29  	for _, c := range args[1:] {
    30  		cookie, err := parseCookie(c)
    31  
    32  		if err != nil {
    33  			return values.None, err
    34  		}
    35  
    36  		cookies.Set(cookie)
    37  	}
    38  
    39  	return values.None, page.SetCookies(ctx, cookies)
    40  }