WebView2

Post your working scripts, libraries and tools.
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: 2.0-beta.1 - WebView2

Post by kczx3 » 20 Oct 2021, 15:09

Just pass the function reference directly and you can call it after you've "awaited" it in JS.

Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

Re: 2.0-beta.1 - WebView2

Post by Tre4shunter » 20 Oct 2021, 15:41

Yes - thanks for pointing that out!

Could anyone help me how to handle the ExecuteScript handler callback?

this code 'works' to execute some js on the page, but ive no idea how to actually use the handler, and say, 'handle' a response or any response data/values.

Code: Select all

	testArr:=['one','two','three']
   	wv.ExecuteScript("(function () {alert(" JxonEncode(testArr) ");document.getElementById('inputEmail').value})();",myHandler(wv,arg:=0))
	return 0
	myHandler(wv,arg){
		return 0
	}
	

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

Re: 2.0-beta.1 - WebView2

Post by kczx3 » 20 Oct 2021, 19:47

You’re calling the myHandler function rather than passing it to ExecuteScript. Don’t call it. WebView2 calls your function after it runs the provider code

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

Re: 2.0-beta.1 - WebView2

Post by thqby » 21 Oct 2021, 02:45

@Tre4shunter
ButtonsFromHTML.Bind() is redundant.
And the second parameter type of ExecuteScript is WebView2.handler object.

Code: Select all

FormFromHTML(p*) {
    static handler := WebView2.Handler(myhandler)
    ...
    wv.ExecuteScript("alert(" JxonEncode(testArr) ")",handler)   
    return 0
    myHandler(handler, errorCode, result){
        msgbox(strget(result))
        return(0)
    }
}

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

Re: 2.0-beta.1 - WebView2

Post by thqby » 21 Oct 2021, 05:03

kczx3 wrote:
20 Oct 2021, 07:55
Have you figured out working with AHK arrays in JavaScript via AddHostObjectToScript?
I forgot this needs special treatment.

Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

Re: 2.0-beta.1 - WebView2

Post by Tre4shunter » 21 Oct 2021, 07:08

@thqby ,@kczx3 -

Thanks both for you guidance! Learning alot and quite enjoying playing with this library and V2!!

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

Re: 2.0-beta.1 - WebView2

Post by kczx3 » 21 Oct 2021, 07:30

thqby wrote:
21 Oct 2021, 02:45
And the second parameter type of ExecuteScript is WebView2.handler object.
Ah, didn't realize that. Would be helpful if that was mentioned early on in the code.

Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

Re: 2.0-beta.1 - WebView2

Post by Tre4shunter » 23 Oct 2021, 10:33

I see the constant is there in WebView2.ahk to disable CORS...but i dont see how to actually implement it.

is this something that isnt possible yet -- im sure i just dont know how to implement it..
https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2hostresourceaccesskind?view=webview2-dotnet-1.0.992.28

Im loading a local html file, and have some js which is trying to load some json files.

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

Re: 2.0-beta.1 - WebView2

Post by thqby » 24 Oct 2021, 00:56

ICoreWebView2_3::SetVirtualHostNameToFolderMapping(/* [in] */ LPCWSTR hostName, /* [in] */ LPCWSTR folderPath, /* [in] */ COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND accessKind)

Code: Select all

enum COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND
    {
        COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY	= 0,
        COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_ALLOW	= ( COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY + 1 ) ,
        COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY_CORS	= ( COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_ALLOW + 1 ) 
    } 	COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND;
This method?

Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

Re: 2.0-beta.1 - WebView2

Post by Tre4shunter » 24 Oct 2021, 07:13

Yep!

i guess my question is how do i actually -- use it?

Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

Re: 2.0-beta.1 - WebView2

Post by Tre4shunter » 25 Oct 2021, 10:43

Is there a way to 'wait' for the ExecuteScript() call to finish? I'd like to send messages to the page and then return a result, but do it synchronously.

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

Re: 2.0-beta.1 - WebView2

Post by thqby » 26 Oct 2021, 10:38

You can wait a value that changed in asynchronous callback.

Code: Select all

wait := 1
web.ExecuteScript(code, handler := WebView2.Handler((*) => wait := 0))
while wait
  sleep -1
And I haven't used ICoreWebView2_3::SetVirtualHostNameToFolderMapping method and don't know the application scenario.
Last edited by thqby on 26 Oct 2021, 18:54, edited 1 time in total.

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

Re: 2.0-beta.1 - WebView2

Post by kczx3 » 26 Oct 2021, 12:00

I don't think there's a way to do ExecuteScript synchronously. You will have to use a callback.

For SetVirtualHostNameToFolderMapping, you just have to pass the third argument as WebView2.HOST_RESOURCE_ACCESS_KIND.DENY.

HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: 2.0-beta.1 - WebView2

Post by HotKeyIt » 26 Oct 2021, 13:14

Very nice, thanks for sharing.
On my machine I had multiple runtimes installed and it did not work, to fix it had to pick up the latest, so I removed the break and all worked:

Code: Select all

if (!edgeruntime) {
				loop files "C:\Program Files (x86)\Microsoft\EdgeWebView\Application\*", "D"
					if (A_LoopFilePath ~= "\\[\d.]+$") {
						edgeruntime := A_LoopFileFullPath
						;~ break
					}
			}

Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

Re: 2.0-beta.1 - WebView2

Post by Tre4shunter » 28 Oct 2021, 15:18

Hello!

I'm attempting to launch a new 'window' with another Webview2 control in it...but i cant seem to create more than one. I'm sure i need to use the NewWindowRequested method to do this, but i cant seem to make it work properly.

could you point me in the right direction?

Thanks so much again for this class!

[EDIT]
Ended up getting it "working" like so...but I'm certain there's a better, cleaner method.

Code: Select all

main := Gui('+Resize'), main.MarginX := main.MarginY := 0
main.Title := WBName
main.OnEvent('Size', gui_size)
main.Show(Format('w{} h{}', A_ScreenWidth * 0.6, A_ScreenHeight * 0.6))


svc := Gui(), svc.MarginX := svc.MarginY := 0
svc.OnEvent('Size', gui_size)

;Load WebView2 Interface
If !DirExist(A_Temp "\wv2\")
	DirCreate(A_Temp "\wv2\")

wvc := WebView2.create(main.Hwnd, Main_Invoke, 0, A_Temp "\wv2\")
Service()


return

Main_Invoke(wvc){
	global
	main.GetClientPos(,,&w,&h)
	ctl := main.AddText('x0 y25 w' w ' h' (h - 25))
	wv := wvc.CoreWebView2
	wv.Navigate('file:///' A_ScriptDir '\index.html')
	return(wvc)
}

svc_Invoke(wvc1){
	global
	svc.GetClientPos(,,&w,&h)
	ctl1 := main.AddText('x0 y25 w' w ' h' (h - 25))
	svc.Show(Format('w{} h{}', A_ScreenWidth * 0.6, A_ScreenHeight * 0.6))
	wv1 := wvc1.CoreWebView2
	wv1.Navigate('file:///' A_ScriptDir '\service\main.html')	
	return(wvc1)
}
Service(){
	global
	wvc1 := WebView2.create(svc.Hwnd, svc_Invoke, 0, A_Temp "\wv2_ss\")
	svc.Show(Format('w{} h{}', A_ScreenWidth * 0.6, A_ScreenHeight * 0.6))
}
gui_size(GuiObj, MinMax, Width, Height) {
	if (MinMax != -1) {
		try ctl.Move(, , Width, Height - 23)
        try wvc.Fill()

		try ctl1.Move(, , Width, Height - 23)
        try wvc1.Fill()
	}
}


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

Re: 2.0-beta.1 - WebView2

Post by thqby » 29 Oct 2021, 07:06

Code: Select all

g1 := Gui(), g2 := Gui()
g1.Show('w800 h600')
g2.Show('w800 h600')
wv1 := WebView2.create(g1.Hwnd, main)
wv2 := WebView2.create(g2.Hwnd, main)

main(ww) {
    ww.CoreWebView2.Navigate('https://autohotkey.com')
}
What do you mean? These are two separate controls.
NewWindowRequest event is triggered when a link is clicked to bring up a new window.

Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

Re: 2.0-beta.1 - WebView2

Post by Tre4shunter » 29 Oct 2021, 07:52

Hi @thqby ,

Thanks for clarifying - I think that's what i ended up with, I was just frustrating myself for a bit and I think overcomplicating things.
Again, this library is excellent and thank you very much for assembling what I'm sure was a load of work on your end. I'm endlessly appreciative!

got a paypal or something? I'd definitely donate to you.

-Tre4

------[EDIT]-------
I would also like to add that, although V2 is still in 'Beta' and Webview2 is as well(I think) the current stability and functionality ive had with it is outstanding. I'm in the process of converting over apps ive written over the last several years to use the WV2 interface, and ive not had a single issue that i wasn't able to resolve, or that has stopped me or given me any pause or reasons not to move forward. I feel like I'm beating a dead horse saying how much I love this class/V2 but....I just really think its awesome. Hope more people start digging in!!

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

Re: 2.0-beta.1 - WebView2

Post by thqby » 01 Nov 2021, 07:39

@Tre4shunter
The latest version is ready to print pdf.

Paypal is the same as this ID.

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

Re: 2.0-beta.1 - WebView2

Post by kczx3 » 01 Nov 2021, 09:16

@Tre4shunter
WebView2 is stable now and has been for some time actually.

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

Re: 2.0-beta.1 - WebView2

Post by thqby » 01 Nov 2021, 09:39

I can't think of an easy way to access host objects such as array, map, etc. :?

Post Reply

Return to “Scripts and Functions (v2)”