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
WebView2
Re: 2.0-beta.1 - WebView2
Do you have the Edge runtime installed?@func
The code in your repository works.
The code in your repository works.
Re: 2.0-beta.1 - WebView2
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
For others, you may need to download the webview2 runtime installer from here
https://developer.microsoft.com/en-us/microsoft-edge/webview2/#download-section
Re: 2.0-beta.1 - WebView2
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.
Thank you. Any help appreciated.
Re: 2.0-beta.1 - WebView2
@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)
}
}
Re: 2.0-beta.1 - WebView2
Thank you so much.. This works wonderful!!
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.
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)
}
}
Thank you very much.
-
- Posts: 139
- Joined: 26 Jan 2016, 16:05
Re: 2.0-beta.1 - WebView2
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.
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!
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
Thanks!
Re: 2.0-beta.1 - WebView2
@Tre4shunter
This works for me and seems as a clean way (unless I didn't understood the question correctly):
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()
-
- Posts: 139
- Joined: 26 Jan 2016, 16:05
Re: 2.0-beta.1 - WebView2
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()
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()
Re: 2.0-beta.1 - WebView2
@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..
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.
Re: 2.0-beta.1 - WebView2
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?
Re: 2.0-beta.1 - WebView2
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?
Any help is very appreciated. Thank you..
P.
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)
}
}
P.
Re: 2.0-beta.1 - WebView2
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. 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) {
}
}
Re: 2.0-beta.1 - WebView2
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()
Re: 2.0-beta.1 - WebView2
@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.
Thank you.
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()
Last edited by userXeo1 on 03 May 2022, 23:14, edited 1 time in total.
-
- Posts: 139
- Joined: 26 Jan 2016, 16:05
Re: 2.0-beta.1 - WebView2
Has anyone sorted out how to use 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!
Code: Select all
SetVirtualHostNameToFolderMapping("host","path", 1)
Any help is appreciated! Thanks!
Re: 2.0-beta.1 - WebView2
I’m not sure it works for network shares although I haven’t tried. I know I’ve used that method before though
-
- Posts: 139
- Joined: 26 Jan 2016, 16:05
Re: 2.0-beta.1 - WebView2
@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!
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