WebView2

Post your working scripts, libraries and tools.
ZeroWidth
Posts: 20
Joined: 14 Apr 2023, 02:37

Re: WebView2

Post by ZeroWidth » 30 Oct 2024, 18:26

@idkwhattowrite
Yes, you can open local html files, just use the URI: file:///
E.G.:
file:///C:/.../index.html

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

Re: WebView2

Post by thqby » 30 Oct 2024, 23:03

Tvlao wrote:
30 Oct 2024, 09:09
Do I need to create some data directory for each WebView?
Why don't you try it first?
I have not had this issue. You also did not provide code to reproduce the issue.
ZeroWidth wrote:
23 Oct 2024, 20:51
thqby wrote:
29 Oct 2024, 19:07
Also, this issue has been mentioned in previous posts. I gave a solution based on that situation, and guessed that your situation was similar to that one.

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

Re: WebView2

Post by thqby » 30 Oct 2024, 23:14

@leuhmanu

Code: Select all

wvc := WebView2.CreateControllerAsync(main.Hwnd,, A_ScriptDir "\data\1" ).await2()
wv := wvc.CoreWebView2
wv.Profile.IsPasswordAutosaveEnabled := true
wv.Profile.IsGeneralAutofillEnabled := true

idkwhattowrite
Posts: 13
Joined: 30 Oct 2024, 03:12
Contact:

Re: WebView2

Post by idkwhattowrite » 31 Oct 2024, 04:45

ZeroWidth wrote:
30 Oct 2024, 18:26
@idkwhattowrite
Yes, you can open local html files, just use the URI: file:///
E.G.:
file:///C:/.../index.html
And is there a way to open index file from the same directory?
I mean ahk file and index file are in one directory, is there an easy wasy? Because if i move this directory, for example: From Folder1 to Folder2, i will need to write new URL.

ZeroWidth
Posts: 20
Joined: 14 Apr 2023, 02:37

Re: WebView2

Post by ZeroWidth » 01 Nov 2024, 00:30

@idkwhattowrite
If you have your html file on the same directory as the script, you can do this:

Code: Select all

wv.Navigate("file:///" A_ScriptDir "\index.html")

idkwhattowrite
Posts: 13
Joined: 30 Oct 2024, 03:12
Contact:

Re: WebView2

Post by idkwhattowrite » 01 Nov 2024, 01:29

ZeroWidth wrote:
01 Nov 2024, 00:30
@idkwhattowrite
If you have your html file on the same directory as the script, you can do this:

Code: Select all

wv.Navigate("file:///" A_ScriptDir "\index.html")
TY

noticz
Posts: 3
Joined: 21 Oct 2014, 13:16

Re: WebView2

Post by noticz » 08 Nov 2024, 07:27

I noticed a couple people mention printing to pdf problems and I read through every reply. I didn't see any answers to those questions and I'm wondering if this is still a problem?

Panaku
Posts: 72
Joined: 02 Apr 2022, 17:24

Re: WebView2

Post by Panaku » 16 Nov 2024, 06:11

noticz wrote:
08 Nov 2024, 07:27
I noticed a couple people mention printing to pdf problems and I read through every reply. I didn't see any answers to those questions and I'm wondering if this is still a problem?
Which problem are you concerned about? Printing to PDF is capable on my end. I know one user had some issues with scaling, but it's hard to say what may have been causing that particular issue as I assume it could be a number of things.

Panaku
Posts: 72
Joined: 02 Apr 2022, 17:24

Re: WebView2

Post by Panaku » 16 Nov 2024, 06:44

@thqby, back on August 16th, you pushed an update for the library (upgrate to 1.0.2651.64 => Commit 39861f3) in which you removed the __Enum(n) method from the class Base and then added a new class List. I'm trying to figure out how to make use of this new class to iterate through properties that I had previously been able to do and I'm not having much luck. The furthest I've gotten with the new class is encountering the following error: Error: This value of type "WebView2.List" has no property named "Count". Could you provide an example of how to get a list of property values for things such as the wv.Settings

I did test adding back in the removed method, and the iteration works and does not seem to cause any issues. Below is the code that I had been using prior to this update to get output for properties.

Code: Select all

MsgBox(ForEach(wv.Settings, "wv.Settings"))
ForEach(Obj, Parent := "Default") {
	Output := ""
	try {
		for Key, Value in Obj {
			try {
				Output .= Parent " >> " Key ": " Value "`n"
			} catch {
				Output .= ForEach(Value, Parent " >> " Key)
			}
		}
	}
	return Trim(Output, "`n")
}

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

Re: WebView2

Post by thqby » 17 Nov 2024, 03:45

Is this for debugging? The debugger can output objects directly. In addition, in v2.1, you can also enumerate the properties of the base class through for k, v in obj.Props().

Code: Select all

base_props(obj) {
	iter := obj.Base.OwnProps(), iter()	; skip `__Class`
	return next
	next(&k, &v, *) {
		while iter(&k)
			try if !((v := obj.%k%) is Func)
				return true
		return false
	}
}
for k, v in base_props(Map()) {
	msgbox k ' ' v
}
Subclasses of WebView2.List can be enumerated by for item in list.

squadjot
Posts: 39
Joined: 17 Nov 2024, 08:55

Re: WebView2

Post by squadjot » 28 Nov 2024, 18:34

Nice work thqby
Question: Did you implement access to Windows COM objects ( which has a jscript interface )

So i can do something similar to:
var shell = ActiveXObject("WScript.Shell"); etc etc..

To refactor that functionality with a WebView2 in a C# .NET App is done like this:

Code: Select all

// method belongs to the object you add with AddHostObjectToScript
public Object NewActiveXObject(String progId)
{
return (Activator.CreateInstance(Type.GetTypeFromProgID(progId)));
}

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

Re: WebView2

Post by thqby » 28 Nov 2024, 20:58

@squadjot
I don't know what you mean. Do you mean add comobject as a host object to webview2? That should be supported.

squadjot
Posts: 39
Joined: 17 Nov 2024, 08:55

Re: WebView2

Post by squadjot » 29 Nov 2024, 04:00

thqby wrote:
28 Nov 2024, 20:58
@squadjot
I don't know what you mean. Do you mean add comobject as a host object to webview2? That should be supported.
I mean so i can replicate the functionailty of for an example:

Code: Select all

var fso = new ActiveXObject("Scripting.FileSystem"); 
..in the WebView

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

Re: WebView2

Post by thqby » 29 Nov 2024, 05:03

By adding the ComObject class of ahk to webview2, you can create other com objects by calling it.

See Example 1

squadjot
Posts: 39
Joined: 17 Nov 2024, 08:55

Re: WebView2

Post by squadjot » 29 Nov 2024, 05:29

Thanks i will try later

squadjot
Posts: 39
Joined: 17 Nov 2024, 08:55

Re: WebView2

Post by squadjot » 02 Dec 2024, 04:33

Hi thqby

I am new to AHK script but I got it working! Very nice :)

Would you mind showing me how to use "AllowExternalDrop"?

My goal is; To be able register drag drop of files from within the webview/js

My first thought was to have a secondary (hidden/transparent) gui that would work as dedicated "droparea"
I would show/hide and set pos/size of the droparea from js, and when files were dropped, i would feed the paths in to the webview with executescript or postmessage.

The solution above actually works okay, but i'm wondering if this is a unnecessary "hack" and if there's a cleaner way to do this?

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

Re: WebView2

Post by thqby » 02 Dec 2024, 07:58

Code: Select all

#Include <WebView2\WebView2>

g := Gui()
g.Show('w800 h600')
wv := WebView2.CreateControllerAsync(g.Hwnd).await2()
wv.CoreWebView2.add_WebMessageReceived(handler)
handler(wv, args) {
	if args.TryGetWebMessageAsString() == 'drop' {
		paths := ''
		for obj in args.additionalObjects {
			try {
				obj := obj.as(WebView2.File)
				paths .= obj.Path '`n'
			}
		}
		ToolTip(paths)
	}
}
wv.CoreWebView2.NavigateToString('
(
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		* {
			margin: 0;
			padding: 0;
		}
		.box {
			width: 800px;
			height: 600px;
			border-radius: 5px;
			background-color: #ccc;
			font-size: 18px;
			font-weight: bold;
			color: #fff;
			text-align: center;
			line-height: 100px;
		}
	</style>
</head>
 
<body>
	<div class="box">drag the files here</div>
	<script>
		let box = document.querySelector('.box');
		box.ondragover = function (ev) {
			ev.preventDefault();
			ev.stopPropagation();
			return false;
		}
		box.ondrop = function (ev) {
			ev.preventDefault();
			ev.stopPropagation();
			chrome.webview.postMessageWithAdditionalObjects('drop', ev.dataTransfer.files);
			return false;
		}
	</script>
</body>
</html>
)')
Implemented through html, this is an example I found on the Internet.
Last edited by thqby on 02 Dec 2024, 20:46, edited 1 time in total.

squadjot
Posts: 39
Joined: 17 Nov 2024, 08:55

Re: WebView2

Post by squadjot » 02 Dec 2024, 11:15

thqby wrote:
02 Dec 2024, 07:58

Code: Select all

#Include <WebView2\WebView2>

g := Gui()
g.Show('w800 h600')
wv := WebView2.CreateControllerAsync(g.Hwnd).await2()
wv.CoreWebView2.OpenDevToolsWindow()
wv.CoreWebView2.NavigateToString('
(
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .box {
            width: 800px;
            height: 600px;
            border-radius: 5px;
            background-color: #ccc;
            font-size: 18px;
            font-weight: bold;
            color: #fff;
            text-align: center;
            line-height: 100px;
        }
    </style>
</head>
 
<body>
    <div class="box">drag the files here</div>
    <script>
        let box = document.querySelector('.box');
        box.ondragover = function (ev) {
            ev.preventDefault();
            ev.stopPropagation();
            return false;
        }
        box.ondrop = function (ev) {
            ev.preventDefault();
            ev.stopPropagation();
            console.dir(ev.dataTransfer.files);
            return false;
        }
    </script>
</body>
</html>
)')
Implemented through html, this is an example I found on the Internet.
Thanks, but it doesn't seem to give my filepaths, only filenames.

squadjot
Posts: 39
Joined: 17 Nov 2024, 08:55

Re: WebView2

Post by squadjot » 02 Dec 2024, 15:32

Hmm, seems like full paths is something that has been on the wish list for some time. If i read correctly it might be included in recent WebView2 update
https://github.com/MicrosoftEdge/WebView2Feedback/issues/501

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

Re: WebView2

Post by thqby » 02 Dec 2024, 20:47

@squadjot
I have updated WebView2.ahk and the above code.
The path can be obtained from the File object on the host side.

Post Reply

Return to “Scripts and Functions (v2)”