Webview2 => how to access cookies? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
sashaatx
Posts: 333
Joined: 27 May 2021, 08:27
Contact:

Webview2 => how to access cookies?

Post by sashaatx » 07 Jun 2023, 15:37

Code: Select all

		GetCookies(uri, handler) => ComCall(5, this, 'wstr', uri, 'ptr', handler)	; ICoreWebView2GetCookiesCompletedHandler
This the function for webview2 accessible here:
https://github.com/thqby/ahk2_lib/tree/master/WebView2



this is my simple script, I just want to grab the cookies within the webview, but Im unsure about "ptr", I see a corresponding number in debugger. Doesnt work,
Im just curious if anyone has a shortcut to grab cookies from edge webview2? or Neutron, Im not partial.

Code: Select all

#Include WebView2.ahk

main := Gui('+Resize')
main.OnEvent('Close', (*) => (wvc := wv := 0))
main.Show(Format('w{} h{}', A_ScreenWidth * 0.6, A_ScreenHeight * 0.6))

wvc := WebView2.create(main.Hwnd)
wv := wvc.CoreWebView2
wv.Navigate('https://bing.com/chat')
x := wvc.CookieList
GetCookiesHandler(cookie){
    FileAppend(cookie, "txt.txt")
}
x.GetCookies(wv.ptr, GetCookiesHandler)
wv.AddHostObjectToScript('ahk', {str:'str from ahk',func:MsgBox})
wv.OpenDevToolsWindow()
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Webview2 => how to access cookies?

Post by kczx3 » 08 Jun 2023, 12:00

Why are you passing a ptr to GetCookies? It pretty clearly accepts a URI as the first argument.

sashaatx
Posts: 333
Joined: 27 May 2021, 08:27
Contact:

Re: Webview2 => how to access cookies?

Post by sashaatx » 08 Jun 2023, 15:59

kczx3 wrote:
08 Jun 2023, 12:00
Why are you passing a ptr to GetCookies? It pretty clearly accepts a URI as the first argument.

I'm new to DLLs and ComObjects. I put together the example for the forum post, I copied an older function call.
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Webview2 => how to access cookies?

Post by kczx3 » 09 Jun 2023, 08:10

Using the library has nothing to do with Dlls or ComObjects. That is all abstracted away. Just pass the URI as the first argument to GetCookies

User avatar
thqby
Posts: 397
Joined: 16 Apr 2021, 11:18
Contact:

Re: Webview2 => how to access cookies?  Topic is solved

Post by thqby » 09 Jun 2023, 22:37

Code: Select all

main := Gui('+Resize')
main.OnEvent('Close', (*) => (wvc := wv := 0))
main.Show(Format('w{} h{}', A_ScreenWidth * 0.6, A_ScreenHeight * 0.6))

wvc := WebView2.create(main.Hwnd)
wv := wvc.CoreWebView2
wv.Navigate('https://www.autohotkey.com')
cm := wv.CookieManager
GetCookiesHandler(_, result, cookieList) {
	if !cookieList
		throw OSError(result)
	cookieList := WebView2.CookieList(cookieList)
	cookies := []
	loop cookieList.Count
		cookies.Push(dump(cookieList[A_Index - 1]))
	MsgBox JSON.stringify(cookies)
	dump(cookie) {
		o := {}
		for k in WebView2.Cookie.Prototype.OwnProps()
			if k != '__Class'
				o.%k% := cookie.%k%
		return o
	}
}

cm.GetCookies('https://www.autohotkey.com', WebView2.Handler(GetCookiesHandler))

sashaatx
Posts: 333
Joined: 27 May 2021, 08:27
Contact:

Re: Webview2 => how to access cookies?

Post by sashaatx » 10 Jun 2023, 00:12

thqby wrote:
09 Jun 2023, 22:37

Code: Select all

main := Gui('+Resize')
main.OnEvent('Close', (*) => (wvc := wv := 0))
main.Show(Format('w{} h{}', A_ScreenWidth * 0.6, A_ScreenHeight * 0.6))

wvc := WebView2.create(main.Hwnd)
wv := wvc.CoreWebView2
wv.Navigate('https://www.autohotkey.com')
cm := wv.CookieManager
GetCookiesHandler(_, result, cookieList) {
	if !cookieList
		throw OSError(result)
	cookieList := WebView2.CookieList(cookieList)
	cookies := []
	loop cookieList.Count
		cookies.Push(dump(cookieList[A_Index - 1]))
	MsgBox JSON.stringify(cookies)
	dump(cookie) {
		o := {}
		for k in WebView2.Cookie.Prototype.OwnProps()
			if k != '__Class'
				o.%k% := cookie.%k%
		return o
	}
}

cm.GetCookies('https://www.autohotkey.com', WebView2.Handler(GetCookiesHandler))
thanks buddy! I'll add that to the examples :)
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

Post Reply

Return to “Ask for Help (v2)”