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 » 01 Nov 2021, 10:34

Here's my history on the topic (along with the real answers by Lexikos) - viewtopic.php?f=82&t=80037&hilit=webview

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

Re: 2.0-beta.1 - WebView2

Post by thqby » 01 Nov 2021, 10:45

I've seen this, but it's not so convenient to use. And it will affect other objects.

User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: 2.0-beta.1 - WebView2

Post by haichen » 03 Nov 2021, 09:35

Hi,
I have tried example one and get this error message:
Warning in #include file "C:\Users\xx\Desktop\webview\Lib\WebView2\WebView2.ahk":
This variable appears to never be assigned a value.

Specifically: local VerCompare

Line#
..
031: Loop Files "C:\Program Files (x86)\Microsoft\EdgeWebView\Application\*", "D"
---> 032: If RegExMatch(A_LoopFilePath, '\\([\d.]+)$', &m) && VerCompare(m[1], ver) > 0
033: edgeruntime := A_LoopFileFullPath, ver := m[1]
..

I can just keep going and it works.
I can't find the vercompare function anywhere.
Did I miss something?

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: 2.0-beta.1 - WebView2

Post by swagfag » 03 Nov 2021, 09:39

u missed updating to a137 or later

User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: 2.0-beta.1 - WebView2

Post by haichen » 03 Nov 2021, 10:08

Oh thanks, the path in my editor still pointed to an old version.
It works now.

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

Re: 2.0-beta.1 - WebView2

Post by Tre4shunter » 04 Nov 2021, 12:10

thqby wrote:
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.

I've tried every which way i can think of to get the above working in order to 'wait', but cant seem to get it right. should the above be a fully functional example, or am i missing something perhaps? in my testing...it just waits forever.

Thanks so much!

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

Re: 2.0-beta.1 - WebView2

Post by thqby » 04 Nov 2021, 19:36

@Tre4shunter

Code: Select all

wait:=1
tset:=(*)=>wait:=0  ; local var
test()
msgbox wait ; 1

outfunc() {
  wait:=1
  test:=(*)=>wait:=0 ; closure var
  test()
  msgbox wait  ; 0
}
outfunc()

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

Re: 2.0-beta.1 - WebView2

Post by Tre4shunter » 28 Dec 2021, 10:47

Hi @thqby -

I am still unable to get the printtopdf function to be a 'blocking' action. Could you possible update your example to reflect that?

Sorry for being dense here :(

Thanks again for the effort on this.

-tre4

neogna2
Posts: 586
Joined: 15 Sep 2016, 15:44

Re: 2.0-beta.1 - WebView2

Post by neogna2 » 29 Dec 2021, 05:46

Thank you @thqby for this! I'm just getting started with it.

One thing I like is that on my old PC a WebView2 script can open the local AHK v2 help file (unpacked as .html files) 1-2 seconds faster than the time the Windows bundled CHM viewer hh.exe takes to open AutoHotkey.chm. I will play around more and try customizing things, like filtering text on help pages. Maybe in the future even the official v2 help could move from the old CHM format to WebView2.

Code: Select all

#Include WebView2.ahk

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

wvc := WebView2.create(main.Hwnd)
wv := wvc.CoreWebView2
;edit to your path to unpacked AutoHotkey.chm files
wv.Navigate('file:///C:/ahk2_chm/docs/AutoHotkey.htm')

;resize wvc with main gui window
gui_size(GuiObj, MinMax, Width, Height) {
    if (MinMax != -1) {
        try wvc.Fill()
    }
}

; release objects on exitapp
_exit_(*) {
    global wvc, wv
    wvc := wv := 0
    ExitApp()
}
One (perhaps confused) question: What browser settings are in use when we use WebView2? Is it whatever is default in Edge for the active user? How do we override that i.e. can we, like what is possible with Chrome.ahk, make single use custom profile with various configurations and use with WebView2?
edit: Found this, will experiment
https://github.com/thqby/ahk2_lib/blob/master/WebView2/WebView2.ahk#L877
https://docs.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2settings?view=webview2-1.0.1054.31

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

Re: 2.0-beta.1 - WebView2

Post by thqby » 31 Dec 2021, 08:25

@Tre4shunter
I have updated example 4.

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

Re: 2.0-beta.1 - WebView2

Post by thqby » 03 Jan 2022, 21:19

@neogna2
create(hwnd, callback := unset, createdEnvironment := 0, datadir := '', edgeruntime := '', dllPath := 'WebView2Loader.dll')
Do you mean this? datadir user configuration folder

The setting of edge is used by default, RegExReplace(A_AppData, 'Roaming$', 'Local\Microsoft\Edge\User Data')

Ashtefere
Posts: 14
Joined: 02 Dec 2017, 11:14

Re: 2.0-beta.1 - WebView2

Post by Ashtefere » 10 Mar 2022, 20:16

Could anyone give an example on how to send a message BACK to AHK from the webview?

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

Re: 2.0-beta.1 - WebView2

Post by thqby » 10 Mar 2022, 21:24

AddHostObjectToScript, call ahk objects from webview with js.

Ashtefere
Posts: 14
Joined: 02 Dec 2017, 11:14

Re: 2.0-beta.1 - WebView2

Post by Ashtefere » 10 Mar 2022, 22:17

Thanks but is there a way to do this without first sending an object, like for example using windowmessage/postmessage?

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

Re: 2.0-beta.1 - WebView2

Post by thqby » 11 Mar 2022, 02:24

The host and the web content can also communicate with each other through the postMessage method. The web content running within a WebView2 control can post to the host through the window.chrome.webview.postMessage method, and the message is handled by any registered ICoreWebView2WebMessageReceivedEventHandler event handler on the host.

Similarly, the host can message the web content through the ICoreWebView2::PostWebMessageAsString or ICoreWebView2::PostWebMessageAsJSON method, and the message is caught by handlers that are added from the window.chrome.webview.addEventListener listener. This communication mechanism allows the web content to use native capabilities by passing messages to ask the host to run native APIs.

Source: https://docs.microsoft.com/en-us/microsoft-edge/webview2/get-started/win32#step-16---communication-between-host-and-web-content

userXeo1
Posts: 39
Joined: 26 Feb 2020, 09:12

Re: 2.0-beta.1 - WebView2

Post by userXeo1 » 04 Apr 2022, 23:32

Hi all..Thank you @thqby

I am puzzled about Webview2 runtime. I'm trying to use Webview2 to help me with setting up ahk app and auto logins.. my environment is restricted that devtools (for automation) )in the edge are disabled..

We have an application that uses WebView2.. However, when I have run WebView2.Ahk I get same message that translates that Webview2 runtime is not installed..

Question.. how is this possible? Upon exploring C:\Program Files (x86)\Microsoft\Edge\Application\100.0.1185.29\ I see that msedgewebview2.exe is included , also updating path line 31 to : C:\Program Files (x86)\Microsoft\Edge\Application\ in WebView2.ahk gives me COM error.
image.png
image.png (17.53 KiB) Viewed 3938 times

It is possible that working app with WebView2 has a built-in Webview2? or WebView2 is installed in different directory? AM I missing something,?

Thank you.

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

Re: 2.0-beta.1 - WebView2

Post by thqby » 06 Apr 2022, 01:53

The official version of edge is not available.
You need a beta or dev version of edge or edge runtime to use webview2.
@userXeo1

userXeo1
Posts: 39
Joined: 26 Feb 2020, 09:12

Re: 2.0-beta.1 - WebView2

Post by userXeo1 » 08 Apr 2022, 09:40

thqby wrote:
06 Apr 2022, 01:53
The official version of edge is not available.
You need a beta or dev version of edge or edge runtime to use webview2.
@userXeo1
@thqby Thank you for reply.. Looks like the app had a fixed version of WebView2 runtime installed. So, it didn't work for me until I have specified the path in the script to the WebView2 runtime.

Code: Select all

wvc := WebView2.create(main.Hwnd,,,,'C:\Program Files\xxx\xxx\Microsoft.WebView2.FixedVersionRuntime.98.0.1082.55.x64')
Going further, I was trying to connect to WebView2 via remote debugging port. I have found that it can be set via WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS to --remote-debugging-port=[portid], however, I am not sure how to implement this in AHK2 code. I am maybe headed in the wrong direction. I need to interact with webpages for automation purposes, log in etc.. I know that I need interaction with Java Script. In the past I have used Chrome.ahk library, which is not an option to anymore. I am thinking of similar approach with WebView2, but possibly I am wrong, there is a better way to interact with webpages that are loaded via WebView2.

Any suggestions?

Thank you.

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

Re: 2.0-beta.1 - WebView2

Post by thqby » 10 Apr 2022, 01:15

@userXeo1
https://docs.microsoft.com/zh-cn/dotnet ... prerelease

This parameter of CreateCoreWebView2EnvironmentWithOptions is not used by default. And Webview2 has many methods, which can be used without CDP.

userXeo1
Posts: 39
Joined: 26 Feb 2020, 09:12

Re: 2.0-beta.1 - WebView2

Post by userXeo1 » 11 Apr 2022, 23:19

Thank you for teply. it possible to post an example of wv interacting with external website? Like clicking a button and sending text to a external form (external meaning page was not created by self, eg google.com)

Post Reply

Return to “Scripts and Functions (v2)”