WebView2

Post your working scripts, libraries and tools.
func
Posts: 47
Joined: 20 May 2016, 20:52

Re: 2.0-beta.1 - WebView2

16 Apr 2022, 02:37

How do I get this to work?
First I downloaded AutoHotkey_2.0-beta.1.zip from https://www.autohotkey.com/download/2.0/
Then I extracted that to a folder.
I extracted WebView2.ahk to the same folder, along with the subfolders 32bit and 64bit which contain WebView2Loader.dll to the same folder.
I also downloaded ComVar.ahk and put it in the same folder, edited WebView2.ahk to have a same-folder path rather than requiring it to be in a parent folder. WebView2.ahk runs fine (drag WebView2.ahk over AutoHotkey64.exe, which is v2.0-beta.1, and it runs without complaint)
Then I made Example1.ahk which is a copy paste from the second post by the OP here, except I changed the Include line to be #Include 'WebView2.ahk', which is the same syntax that's used in WebView2.ahk when including ComVar.ahk, but then when dragging Example1.ahk over the Ahk-v2 exe, it throws an error that the include doesn't work - even though the path and syntax are correct.
I'm completely baffled how this is supposed to work.

Here is a repo with all of the files and dependencies, bundled with exe's for AHK v2.0-beta.1, as the title states, but it doesn't work for me

https://github.com/supersprink/webview2-example1

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

Re: 2.0-beta.1 - WebView2

16 Apr 2022, 04:57

Do you have the Edge runtime installed?@func
The code in your repository works.
image.png
image.png (10.51 KiB) Viewed 5215 times
func
Posts: 47
Joined: 20 May 2016, 20:52

Re: 2.0-beta.1 - WebView2

16 Apr 2022, 18:11

Thank you thqby, that was the problem. I thought since I had windows 10 and edge it was already installed but it wasn't
For others, you may need to download the webview2 runtime installer from here
https://developer.microsoft.com/en-us/microsoft-edge/webview2/#download-section
userXeo1
Posts: 42
Joined: 26 Feb 2020, 09:12

Re: 2.0-beta.1 - WebView2

19 Apr 2022, 22:47

I am still trying to figure out how to manipulate webpages with AHK-->WebVIew2 and after research discovered that DOM can be manipulated with ExecuteScriptAsync(). However, I don't see any reference to this method in WebView2.ahk The only something similar to this that I have seen is wv.ExecuteScript() which includes using myHandler(wv,arg), which is really confusing to me. I am sorry, I am not a programmer by trade, but love improving my programming skills to simplify tasks.

Thank you. Any help appreciated.
User avatar
thqby
Posts: 560
Joined: 16 Apr 2021, 11:18
Contact:

Re: 2.0-beta.1 - WebView2

22 Apr 2022, 08:33

@userXeo1

Code: Select all

#Include <WebView2\WebView2>

g := Gui()
g.Show('w600 h500')
wv := WebView2.create(g.Hwnd)
wv.CoreWebView2.add_NavigationCompleted(WebView2.Handler(NavigationCompletedEventHandler))
wv.CoreWebView2.Navigate('https://autohotkey.com')

NavigationCompletedEventHandler(handler, ICoreWebView2, NavigationCompletedEventArgs) {
	; WebView2.Core(ICoreWebView2) is same as wv.CoreWebView2
	wv.CoreWebView2.ExecuteScript('alert("hello")', 0)
	wv.CoreWebView2.ExecuteScript('1+2+3+4', WebView2.Handler(ExecuteScriptCompletedHandler))

	ExecuteScriptCompletedHandler(handler, errorCode, resultObjectAsJson) {
		MsgBox 'errorCode: ' errorCode '`nresult: ' StrGet(resultObjectAsJson)
	}
}
userXeo1
Posts: 42
Joined: 26 Feb 2020, 09:12

Re: 2.0-beta.1 - WebView2

22 Apr 2022, 21:50

Thank you so much.. This works wonderful!!

Code: Select all

#SingleInstance Force
#Include WebView2.ahk



g := Gui()
g.Show('w600 h500')
wv := WebView2.create(g.Hwnd)
wv.CoreWebView2.add_NavigationCompleted(WebView2.Handler(NavigationCompletedEventHandler))
wv.CoreWebView2.Navigate('https://autohotkey.com')

NavigationCompletedEventHandler(handler, ICoreWebView2, NavigationCompletedEventArgs) {
	; WebView2.Core(ICoreWebView2) is same as wv.CoreWebView2
	text:= "document.querySelector('#MainTitle').innerText = 'Hello from AHK'"
	wv.CoreWebView2.ExecuteScript('alert(text)', 0)
	wv.CoreWebView2.ExecuteScript('alert("hello")', 0)
	wv.CoreWebView2.ExecuteScript(text, WebView2.Handler(ExecuteScriptCompletedHandler))
	
	ExecuteScriptCompletedHandler(handler, errorCode, resultObjectAsJson) {
		MsgBox 'errorCode: ' errorCode '`nresult: ' StrGet(resultObjectAsJson)
	}
}

Just bit confused why wv.CoreWebView2.ExecuteScript('alert(text)', 0) does not work, but I believe because there is no handler?

Thank you very much.
User avatar
thqby
Posts: 560
Joined: 16 Apr 2021, 11:18
Contact:

Re: 2.0-beta.1 - WebView2

23 Apr 2022, 09:06

userXeo1 wrote:
22 Apr 2022, 21:50
Just bit confused why wv.CoreWebView2.ExecuteScript('alert(text)', 0) does not work, but I believe because there is no handler?
wv.CoreWebView2.ExecuteScript('alert(' text ')', 0)
Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

Re: 2.0-beta.1 - WebView2

23 Apr 2022, 20:30

Is there a way to create the wv2 control and have it be completely hidden, untill activated at a later time?

This is the only way ive been able to get it to work....and its not great.

Code: Select all

g := Gui()
g.Show('w0 h0')
wv := WebView2.create(g.Hwnd)
g.Hide()
wv.CoreWebView2.Navigate('https://autohotkey.com')

return

F4::
{
g.Move(,,500,500)
wv.Fill()
g.Show('w500 h500')

}
return
Is there a cleaner/more correct way to do this? If i create the Gui hidden...then i cant seem to get the wv2 control to occupy it properly after i show it. Seems like the gui has to have a width and height and be show atleast initially for a brief moment.

Thanks!
userXeo1
Posts: 42
Joined: 26 Feb 2020, 09:12

Re: 2.0-beta.1 - WebView2

25 Apr 2022, 09:33

thqby wrote:
23 Apr 2022, 09:06
userXeo1 wrote:
22 Apr 2022, 21:50
Just bit confused why wv.CoreWebView2.ExecuteScript('alert(text)', 0) does not work, but I believe because there is no handler?
wv.CoreWebView2.ExecuteScript('alert(' text ')', 0)
Back to basics. .. :superhappy: Sweet. Thank you..
userXeo1
Posts: 42
Joined: 26 Feb 2020, 09:12

Re: 2.0-beta.1 - WebView2

25 Apr 2022, 09:46

@Tre4shunter

This works for me and seems as a clean way (unless I didn't understood the question correctly):

Code: Select all

F4::g.Hide() 
F5::g.Show()
Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

Re: 2.0-beta.1 - WebView2

25 Apr 2022, 12:52

Hi @userXeo1

that certainly does work - but what im wanting is to fully create the instance BEFORE ever showing it. I cant seem to get that to happen unless i show it briefly if only for a second, before i call .Create()
userXeo1
Posts: 42
Joined: 26 Feb 2020, 09:12

Re: 2.0-beta.1 - WebView2

25 Apr 2022, 22:30

@Tre4shunter

Sorry, I misunderstood your question. Here is a bit better way, but its not 100% what you are looking for. Basically show window off screen. +ToolWindow will remove taskbar entry..

Code: Select all

g := Gui("+Resize +ToolWindow")
g.Show('w600 h500 x-700 y-1000')
Last edited by userXeo1 on 28 Apr 2022, 10:03, edited 1 time in total.
userXeo1
Posts: 42
Joined: 26 Feb 2020, 09:12

Re: 2.0-beta.1 - WebView2

25 Apr 2022, 22:35

It is possible to convert WebView2.ahk to AHK V1.1 (current version)? If yes, how hard it is convert from V2 to V1.1 AHK.. The reason why I am asking, ALL of my scripts and work are v1.1 Any ideas?
userXeo1
Posts: 42
Joined: 26 Feb 2020, 09:12

Re: 2.0-beta.1 - WebView2

28 Apr 2022, 09:59

I have made an auto-log in form with WebView2, but have trouble proceeding after log-in...

To simplify things, I made an example, this will load the page and clicks "documentation" on autohotkey.com.. How do I proceed after, if I would like to click on "search" or anything else? Do i have to call wv.CoreWebView2.Navigate again? how do I get url this time?

Code: Select all

#Include WebView2.ahk
g := Gui()
g.Show(Format("w{} h{}", A_ScreenWidth * 0.6, A_ScreenHeight * 0.6))
wv := WebView2.create(g.Hwnd)
wv.CoreWebView2.Navigate("https://www.autohotkey.com")
wv.CoreWebView2.add_NavigationCompleted(WebView2.Handler(NavigationCompletedEventHandler))

NavigationCompletedEventHandler(handler, ICoreWebView2, NavigationCompletedEventArgs) {
	script:= "document.querySelector('#menu-0 > div > div > div > div.mbr-navbar__column.mbr-navbar__menu > nav > div > ul > li:nth-child(2) > a').click()"
	wv.CoreWebView2.ExecuteScript(script, WebView2.Handler(ExecuteScriptCompletedHandler))
	ExecuteScriptCompletedHandler(handler, errorCode, resultObjectAsJson) {
		;MsgBox 'errorCode: ' errorCode '`nresult: ' StrGet(resultObjectAsJson)
	}
}
Any help is very appreciated. Thank you..

P.
userXeo1
Posts: 42
Joined: 26 Feb 2020, 09:12

Re: 2.0-beta.1 - WebView2

30 Apr 2022, 22:09

Update to my previous post: I have messed around with the code and was able to navigate to www.autohotkey.com click on "documentation" link and then on documentation page click on "search". Then set search text to "testing" . The script works. :dance: :dance: However, the code does not seem right to me.. Can anyone take a look and see what I am doing wrong? Thank you.

Code: Select all

g := Gui()
g.Show(Format("w{} h{}", A_ScreenWidth * 0.6, A_ScreenHeight * 0.6))
wv := WebView2.create(g.Hwnd')
wv.CoreWebView2.Navigate("https://www.autohotkey.com")
wv.CoreWebView2.add_NavigationCompleted(WebView2.Handler(NavigationCompletedEventHandler))

NavigationCompletedEventHandler(handler, ICoreWebView2, NavigationCompletedEventArgs) {
	script:= "document.querySelector('#menu-0 > div > div > div > div.mbr-navbar__column.mbr-navbar__menu > nav > div > ul > li:nth-child(2) > a').click()"
	wv.CoreWebView2.ExecuteScript(script, WebView2.Handler(ExecuteScriptCompletedHandler)) ;click on documentation
	
	ExecuteScriptCompletedHandler(handler, errorCode, resultObjectAsJson) {
		script:= "document.querySelector('#head > div > div.h-tabs > ul > li:nth-child(3) > button').click()" 
		wv.CoreWebView2.ExecuteScript(script, WebView2.Handler(ExecuteScriptCompletedHandler2)) ;click on "search"
	}
	ExecuteScriptCompletedHandler2(handler, errorCode, resultObjectAsJson) {
		script:= "document.querySelector('#left > div.tab.search.shrinked > div.input > input[type=search]').value='testing'"
		wv.CoreWebView2.ExecuteScript(script, WebView2.Handler(ExecuteScriptCompletedHandler3)) ;set search text to "testing"
	}
	ExecuteScriptCompletedHandler3(handler, errorCode, resultObjectAsJson) {
	}
}
User avatar
thqby
Posts: 560
Joined: 16 Apr 2021, 11:18
Contact:

Re: 2.0-beta.1 - WebView2

01 May 2022, 22:36

Code: Select all

class SyncHandler extends WebView2.Handler {
	__New(cb := 0) {
		this.obj := SyncHandler.CompletedEventHandler()
		this.obj.cb := cb
		super.__New(this.obj, 3)
	}
	wait() {
		o := this.obj
		while !o.status
			Sleep(10)
		o.status := 0, Sleep(100)
	}

	class CompletedEventHandler {
		status := 0, cb := 0
		call(handler, args*) {
			if this.cb
				(this.cb)(args)
			this.status := 1
		}
	}
}

g := Gui()
g.Show(Format("w{} h{}", A_ScreenWidth * 0.6, A_ScreenHeight * 0.6))
wv := WebView2.create(g.Hwnd)
nav_sync := SyncHandler()
exec_sync := SyncHandler((args) => OutputDebug(StrGet(args[2])))
wv.CoreWebView2.add_NavigationCompleted(nav_sync)
wv.CoreWebView2.Navigate("https://www.autohotkey.com")
nav_sync.wait()
wv.CoreWebView2.ExecuteScript(
	"document.querySelector('#menu-0 > div > div > div > div.mbr-navbar__column.mbr-navbar__menu > nav > div > ul > li:nth-child(2) > a').click()",
	exec_sync
)
exec_sync.wait()
wv.CoreWebView2.ExecuteScript(
	"document.querySelector('#head > div > div.h-tabs > ul > li:nth-child(3) > button').click()",
	exec_sync
)
exec_sync.wait()
wv.CoreWebView2.ExecuteScript(
	"document.querySelector('#left > div.tab.search.shrinked > div.input > input[type=search]').value='testing'",
	exec_sync
)
exec_sync.wait()
userXeo1
Posts: 42
Joined: 26 Feb 2020, 09:12

Re: 2.0-beta.1 - WebView2

02 May 2022, 23:07

@thqby

Thank you for great reply.. It will take me some time to dissect the code.... Need to learn more about classes..

However, just want to let you know your code seems much faster, but it fails at about at least 50% of the time (vs. 100% accuracy with my sample). The code fails on the last page where "testing" is populated in the search box. I believe it is timing issue, because I do notice "testing" word, placed, but it seems the page loads after and it removes it? Possibly have to add_NavigationCompleted, since its a new page?

UPDATE: Calling nav_sync.wait() before 2nd page loading fixed the issue.

Code: Select all

g := Gui()
g.Show(Format("w{} h{}", A_ScreenWidth * 0.6, A_ScreenHeight * 0.6))
wv := WebView2.create(g.Hwnd,,,,'C:\Program Files\ApplicationManagement\AppManager\Microsoft.WebView2.FixedVersionRuntime.97.0.1072.55.x64')
nav_sync := SyncHandler()
exec_sync := SyncHandler((args) => OutputDebug(StrGet(args[2])))
wv.CoreWebView2.add_NavigationCompleted(nav_sync)
wv.CoreWebView2.Navigate("https://www.autohotkey.com")
nav_sync.wait()
wv.CoreWebView2.ExecuteScript(
	"document.querySelector('#menu-0 > div > div > div > div.mbr-navbar__column.mbr-navbar__menu > nav > div > ul > li:nth-child(2) > a').click()",
exec_sync
)
--->nav_sync.wait()<----
exec_sync.wait()
wv.CoreWebView2.ExecuteScript(
	"document.querySelector('#head > div > div.h-tabs > ul > li:nth-child(3) > button').click()",
exec_sync
)
exec_sync.wait()
wv.CoreWebView2.ExecuteScript(
	"document.querySelector('#left > div.tab.search.shrinked > div.input > input[type=search]').value='testing'",
exec_sync
)
exec_sync.wait()
Thank you.
Last edited by userXeo1 on 03 May 2022, 23:14, edited 1 time in total.
Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

Re: 2.0-beta.1 - WebView2

03 May 2022, 10:58

Has anyone sorted out how to use

Code: Select all

SetVirtualHostNameToFolderMapping("host","path", 1)
yet? I'd like to add access to some local resources, but am not sure how to implement this function. the page im loading is a local file located on a network share. I just want to be able to access some resources located in the same directory as the Index.html file.

Any help is appreciated! Thanks!
User avatar
kczx3
Posts: 1677
Joined: 06 Oct 2015, 21:39

Re: 2.0-beta.1 - WebView2

03 May 2022, 17:36

I’m not sure it works for network shares although I haven’t tried. I know I’ve used that method before though
Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

Re: 2.0-beta.1 - WebView2

05 May 2022, 15:34

@userXeo1 -

I haven't come up with a better solution than you described above...however it seems to work perfect fine. I just bring the window in and out of view.

currently im fine just doing that, but if there is a more 'correct' method, id love to know what that would look like.

Thanks!

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: No registered users and 33 guests