thqby / ahk2_lib

MIT License
197 stars 26 forks source link

[WebView2] Examples for working with cookies. #46

Closed RaptorX closed 5 months ago

RaptorX commented 5 months ago

Hey,

Im building a little tool that will use your webview2 class. I got it to where i need it but im wondering if it is possible to have an example of how to fetch a given cookie or at least get a list of the current cookies which i could then loop over.

The problem is that i dont seem to use any of the cookie objects/methods because they all have errors like this:

image

image

I've tried many different variations like this:

cookielist := WebView2.CookieList()
loop 10
    OutputDebug cookielist.GetValueAtIndex(A_Index)
cookiemanager := WebView2.CookieManager()
t := cookiemanager.GetCookies('https://www.site.com', WebView2.Handler(cookies))
    cookiemanager := WebView2.CookieManager()
    t := cookiemanager.GetCookies('https://www.site.com', p := ObjPtr(cookieshandler))

    class cookieshandler
    {
        QueryInterface(iid, ppv) {
            ; Implement this method based on your needs
        }

        AddRef() {
            ; Implement this method based on your needs
        }

        Release() {
            ; Implement this method based on your needs
        }

        invoke(errCode, cookies)
        {
            return
        }
    }
cookiemanager := WebView2.CookieManager()
t := cookiemanager.GetCookies('https://www.site.com', CallbackCreate(cookies))

All end up saying that ComCall parameter #2 is invalid. :(

RaptorX commented 5 months ago

hmm, I think I figured something out.

cookiemanager := browser.CookieManager
cookiemanager.GetCookies('https://www.shein.com', WebView2.Handler(cookies))

This didnt throw an error at least, now ill figure out how the handler function must be setup.

RaptorX commented 5 months ago

It seems like the handler receives a pointer but im not sure how to convert that pointer to an ahk object or how to use it:

cookiemanager := browser.CookieManager
cookiemanager.GetCookies('https://www.site.com', WebView2.Handler(cookies))

cookies(result)
{
    OutputDebug result
}

any ideas as to what should i do from here?

thqby commented 5 months ago
class Handler extends Buffer {
        /**
         * Construct ICoreWebView2 Event or Completed Handler.
         * @param invoke_cb Invoke function of handler.
         * The first parameter of the callback function is the event interface pointer.
         * @see https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2acceleratorkeypressedeventhandler
         */

GetCookies uses the ICoreWebView2GetCookiesCompletedHandler

cookiemanager := browser.CookieManager
cookiemanager.GetCookies('https://www.site.com', WebView2.Handler(cookies))

cookies(_, result, cookieList)
{
    cookieList := WebView2.CookieList(cookieList)
}