Skip to content

Commit e769f87

Browse files
authored
Merge pull request #98 from ioa747/neo
MappedFolder
2 parents 0a200a1 + 5645db5 commit e769f87

5 files changed

Lines changed: 212 additions & 54 deletions

File tree

NetWebView2Lib.au3

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#Tidy_Parameters=/tcb=-1
88

9-
; NetWebView2Lib.au3 - Script Version: 2026.2.16.3 🚩
9+
; NetWebView2Lib.au3 - Script Version: 2026.2.16.20 🚩
1010

1111
#include <Array.au3>
1212
#include <GUIConstantsEx.au3>
@@ -822,7 +822,7 @@ EndFunc ;==>_NetWebView2_PrintToPdfStream
822822
; Description ...: Adds a JavaScript to be executed before any other script when a new page is loaded.
823823
; Syntax.........: _NetWebView2_AddInitializationScript($oWeb, $sScript)
824824
; Parameters ....: $oWeb - The NetWebView2 Manager object.
825-
; $sScript - The JavaScript code to inject.
825+
; $vScript - The JavaScript code to inject (String) OR the full path to a JavaScript file.
826826
; Return values .: Success - Returns a Script ID (string).
827827
; Failure - Returns the error message and sets @error.
828828
; Author ........: ioa747, mLipok
@@ -832,12 +832,16 @@ EndFunc ;==>_NetWebView2_PrintToPdfStream
832832
; Link ..........:
833833
; Example .......: No
834834
; ===============================================================================================================================
835-
Func _NetWebView2_AddInitializationScript($oWebV2M, $sScript)
835+
Func _NetWebView2_AddInitializationScript($oWebV2M, $vScript)
836836
If (Not IsObj($oWebV2M)) Or ObjName($oWebV2M, $OBJ_PROGID) <> 'NetWebView2.Manager' Then Return SetError(1, 0, "ERROR: Invalid Object")
837-
Local $sScriptId = $oWebV2M.AddInitializationScript($sScript)
837+
838+
; Smart Detection
839+
If FileExists($vScript) Then $vScript = FileRead($vScript)
840+
841+
Local $sScriptId = $oWebV2M.AddInitializationScript($vScript)
838842
If StringInStr($sScriptId, "ERROR:") Then Return SetError(2, 0, $sScriptId)
839843
Return SetError(0, 0, $sScriptId)
840-
EndFunc ;==>_NetWebView2_AddInitializationScript
844+
EndFunc ;==>New_NetWebView2_AddInitializationScript
841845

842846
; #FUNCTION# ====================================================================================================================
843847
; Name...........: _NetWebView2_RemoveInitializationScript
@@ -2149,3 +2153,4 @@ EndFunc ;==>__NetWebView2_Events__OnScreenCaptureStarting
21492153
#EndRegion ; NetWebView2Lib UDF - === EVENT HANDLERS ===
21502154

21512155

2156+

README.md

Lines changed: 116 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ https://www.autoitscript.com/forum/topic/213375-webview2autoit-autoit-webview2-c
2323
### 🛠 Prerequisites
2424

2525
1. **.NET Framework 4.8** or higher.
26-
2. **Microsoft Edge WebView2 Runtime**.
26+
2. **Microsoft Edge WebView2 Runtime version 128.0.2739.15 or higher**.
2727

2828
* *The registration script will check for this and provide a download link if missing.*
2929

@@ -62,54 +62,105 @@ This project is provided "as-is". You are free to use, modify, and distribute it
6262
<p align="center">
6363
<img src="https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/rainbow.png" width="100%">
6464
</p>
65+
## 🚀 What's New in v2.0.0-stable - Professional Naming & Absolute Stability
6566

66-
## 🚀 What's New in v2.0.0-beta.3 - AcceleratorKey Detail & Refactoring
67-
68-
This update focuses on deep keyboard interception and code modularity.
67+
This stable release marks the transition to a professional, standardized COM architecture and introduces high-performance infrastructure for production-grade applications.
6968

7069
### ⚡ Key Features & Enhancements
7170

72-
#### **1. PhysicalKeyStatus Expansion**
73-
The `OnAcceleratorKeyPressed` event now provides 1:1 access to the underlying Windows keyboard state.
74-
- **New Properties**: `RepeatCount`, `ScanCode`, `IsExtendedKey`, `IsMenuKeyDown`, `WasKeyDown`, and `IsKeyReleased`.
75-
- **Use Case**: Detect held keys, distinguish between left/right Alt/Ctrl, and implement complex hotkey logic without Win32 API calls.
71+
#### **1. COM Standardizing (NetWebView2Lib)**
72+
The library has transitioned to a consistent naming convention for all subsystems, improving discoverability and professional alignment.
73+
- **New ProgIDs**: `NetWebView2Lib.WebView2Manager`, `NetWebView2Lib.WebView2Bridge`, and `NetWebView2Lib.WebView2Parser`.
74+
- **Full Backward Compatibility**: Legacy ProgIDs (e.g., `NetWebView2.Manager`) continue to function via an inheritance-based compatibility layer.
7675

77-
#### **2. Performance & Modularity**
78-
- **Standalone Argument Logic**: Argument wrappers have been moved to dedicated files (e.g., `WebView2AcceleratorKeyPressedEventArgs.cs`).
79-
- **Lean Core**: Reduced `WebViewManager.cs` complexity by outsourcing event data structures.
76+
#### **2. Threading & Stability Mastery**
77+
Identified and resolved complex threading deadlocks in asynchronous handlers.
78+
- **UI-Thread Marshalling**: Explicit synchronization ensure that events like Context Menus, Downloads, and IFrame HTML extraction always safely interact with the AutoIt UI thread.
79+
- **SDK Resilience**: Reflection-based URI retrieval for IFrames ensures compatibility across different versions of the WebView2 SDK.
8080

81-
#### **3. Process Stability & Authentication**
82-
- **Robust Crash Handling**: `OnProcessFailed` provides deep insights into process crashes, allowing for automated recovery or logging.
83-
- **Crash Dump Management**: `FailureReportFolderPath` allows redirection of diagnostic files.
84-
- **Native Authentication**: `OnBasicAuthenticationRequested` brings support for server-level auth prompts, including asynchronous credential entry via the `.Complete()` method.
81+
#### **3. Standardized SDK Locking (v1.0.2739.15)**
82+
To ensure maximum reliability across the broadest possible range of Windows installations, we have standardized the core engine on a specific, high-stability SDK version.
83+
- **Fixed Version**: Locked to **WebView2 SDK 1.0.2739.15** (Released August 26, 2024).
84+
- **Runtime Requirement**: Requires **WebView2 Runtime 128.0.2739.15** or newer.
85+
- **Strategic Choice**: This version provides a "Sweet Spot" of features—including full support for IFrame scraping, Basic Authentication, and Process Failure events—while maintaining compatibility with the vast majority of current "Evergreen" browser installations.
86+
- **Strict Validation**: The registration utility (`Register_web2.au3`) has been updated to perform a mandatory check for this minimum runtime version, preventing obscure startup errors.
8587

86-
---
88+
#### **4. Enriched Verbose Logging**
89+
A redesigned diagnostic system for easier debugging of complex workflows.
90+
- **Branded & Traceable**: Every log line now includes the `+++[NetWebView2Lib]` prefix and the instance-specific `[HANDLE:0x...]`.
91+
- **Comprehensive Event Tracing**: Detailed logs for Navigation, Resource Requests (with HTTP codes), Focus changes, and Web Messages.
92+
- **Default Folder Sync**: Renamed the default crash folder to `FailureReportFolder` for better parity with internal WebView2 parameters.
8793

88-
## 🚀 What's New in v2.0.0-beta.2 - COM Versioning & Handle Alignment
94+
#### **4. High-Performance IFrame Scraping**
95+
Bulk extraction methods (`GetFrameUrls`, `GetFrameNames`) allow for high-speed metadata retrieval from complex multi-frame layouts, essential for advanced web scraping.
8996

90-
This update introduces the ability to query the DLL version directly and aligns window handles with AutoIt's native format.
97+
---
98+
#### 🖼️ IFrame HTML Extraction
9199

92-
### ⚡ Key Features & Enhancements
100+
We implemented a robust system to track and extract HTML from iframes, including cross-origin ones.
101+
102+
##### 1. Frame Tracking System
103+
104+
A dynamic registration system was added to 
105+
106+
WebView2Manager.cs to track the lifecycle of all iframes.
107+
108+
- **Initialization**: Automatically subscribes to `CoreWebView2.FrameCreated`.
109+
- **Lifecycle Management**: Tracks `CoreWebView2Frame` objects in a thread-safe list and automatically removes them on `Destroyed`.
110+
111+
##### 2. New IFrame API
112+
113+
Six new COM methods were added to the 
114+
115+
IWebViewActions interface (Standardized & Compatibility Layers):
116+
117+
|Method|Description|
118+
|---|---|
119+
|GetFrameCount()|Returns the number of tracked iframes.|
120+
|GetFrameUrl(index)|Returns the URL of a specific iframe.|
121+
|GetFrameName(index)|Returns the Name of a specific iframe.|
122+
|GetFrameUrls()|Returns the **entire array** of frame URLs as a pipe-separated string.|
123+
|GetFrameNames()|Returns the **entire array** of frame names as a pipe-separated string.|
124+
|GetFrameHtmlSource(index)|Asynchronously initiates HTML extraction for a frame.|
125+
126+
#### 🩺Verbose Diagnostic Mode
127+
128+
A new `Verbose` property was added to allow real-time diagnostic logging to the SciTE console (or any stdout listener).
129+
The diagnostic logs use a distinctive prefix and include the instance handle for easier filtering in multi-window applications.
130+
131+
```autoit
132+
$oWebV2M.Verbose = True ; Enable diagnostic logging
133+
```
93134

94-
#### **1. Advanced Handle Formatting (`[HANDLE:0x...]`)**
95-
All window handles returned by the library (via properties or events) are now formatted as strings compatible with AutoIt's Advanced Window Descriptions.
96-
- **Direct Compatibility**: Handles like `[HANDLE:0x00010203]` can be passed directly to `WinExists`, `WinSetTitle`, etc., without `HWnd()` conversion.
135+
When enabled, the console will show entries like: 
136+
`+++[NetWebView2Lib][HANDLE:0x...][HH:mm:ss.fff] Message`
137+
138+
`+++[NetWebView2Lib][HANDLE:0x004F1128][14:20:33.456] Initialize request: parent=[HANDLE:0x...], x=0, y=0, w=1024, h=768` 
139+
`+++[NetWebView2Lib][HANDLE:0x004F1128][14:20:34.123] WebView2 Initialized successfully.` 
140+
`+++[NetWebView2Lib][HANDLE:0x004F1128][14:20:35.789] Navigate: https://www.google.com`
141+
142+
#### 🔄 Backward Compatibility (Legacy ProgIDs)
143+
144+
To ensure stability for existing scripts not using the UDF, the following legacy ProgIDs are still supported through a compatibility layer:
145+
146+
| Legacy Object | Legacy ProgID | v2.0 Standardized ProgID |
147+
| :--------------- | :----------------------------- | :------------------------------- |
148+
| `WebViewManager` | `NetWebView2.Manager` | `NetWebView2Lib.WebView2Manager` |
149+
| `WebViewBridge` | `NetWebView2Lib.WebViewBridge` | `NetWebView2Lib.WebView2Bridge` |
150+
| `JsonParser` | `NetJson.Parser` | `NetWebView2Lib.WebView2Parser` |
151+
152+
> [!NOTE]
153+
> Objects created with legacy ProgIDs will function exactly the same but will report their legacy class names via `ObjName()`.
97154
98-
#### **2. COM Version Exposure**
99-
You can now access the `.version` property on all primary COM objects.
100-
- **`Manager.version`**, **`Bridge.version`**, **`Parser.version`**.
101155

102-
#### **3. Stabilized Infrastructure**
103-
- **GUID Collision Resolution**: Fixed internal interface IDs that caused registration issues in v2.0.0-beta.1.
104-
- **`ParentWindowHandle`**: New property to retrieve the handle passed during initialization.
105156

106157
<p align="center">
107158
<img src="https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/rainbow.png" width="100%">
108159
</p>
109160

110-
## 📖 NetWebView2Lib Version 2.0.0-beta.3 (2026-02-11) (Quick Reference)
161+
## 📖 NetWebView2Lib Version 2.0.0-stable (Quick Reference)
111162

112-
### NetWebView2Lib (ProgId: NetWebView2.Manager)
163+
### WebView2Manager (ProgId: NetWebView2Lib.WebView2Manager)
113164

114165
#### ===Properties===
115166

@@ -181,6 +232,10 @@ Enables or disables the `OnWebResourceResponseReceived` event entirely.
181232
Determines whether `OnWebResourceResponseReceived` triggers for all resources (False) or only for the main document (True). Essential for preventing GUI deadlocks in AutoIt.
182233
`object.HttpStatusCodeDocumentOnly = Value`
183234

235+
##### Verbose
236+
Enable diagnostic logging to console. (before `$object.Initialize()`)
237+
`object.Verbose = Value`
238+
184239
##### IsDownloadHandled
185240
Determines whether the download is handled by the application. If set to **True** during `OnDownloadStarting`, the internal Edge download is cancelled.
186241
`object.IsDownloadHandled = Value`
@@ -211,14 +266,14 @@ A comma-separated list of Virtual Key codes to be blocked synchronously (e.g., "
211266

212267
##### FailureReportFolderPath
213268
Sets or gets the path where the WebView2 browser stores crash reports (dump files).
214-
- **Default**: If NOT set by the user, the system automatically uses the `Crashes` subfolder within the `UserDataFolder` (assigned during `.Initialize`).
269+
- **Default**: If NOT set by the user, the system automatically uses the `FailureReportFolderPath` subfolder within the `UserDataFolder` (assigned during `.Initialize`).
215270
- **Manual Override**: You can set a custom path **before** calling `.Initialize`.
216271
- **Example (Custom Path)**: `object.FailureReportFolderPath = "C:\MyCustomCrashDumps"`
217272
- **Example (Read current)**: `$sPath = object.FailureReportFolderPath`
218273

219274
##### Version
220-
Allows AutoIt to verify the DLL version at runtime for compatibility checks.
221-
`object.version
275+
Allows AutoIt to verify the DLL version at runtime for compatibility checks.
276+
`object.Version`
222277

223278
#### ===Method===
224279

@@ -316,6 +371,30 @@ Clears all active ad block rules.
316371
Asynchronously retrieves the full HTML source (sent via OnMessageReceived with 'HTML_SOURCE|').
317372
`object.GetHtmlSource()`
318373

374+
##### GetFrameCount
375+
Returns the number of currently tracked iframes.
376+
`object.GetFrameCount()`
377+
378+
##### GetFrameUrl
379+
Returns the URL of the specified frame index.
380+
`object.GetFrameUrl(Index As Integer)`
381+
382+
##### GetFrameName
383+
Returns the Name attribute of the specified frame index.
384+
`object.GetFrameName(Index As Integer)`
385+
386+
##### GetFrameUrls
387+
Returns a pipe-separated string of all tracked iframe URLs.
388+
`object.GetFrameUrls()`
389+
390+
##### GetFrameNames
391+
Returns a pipe-separated string of all tracked iframe names.
392+
`object.GetFrameNames()`
393+
394+
##### GetFrameHtmlSource
395+
Asynchronously retrieves the HTML of the frame at the specified index (sent via OnMessageReceived with 'FRAME_HTML_SOURCE|Index|').
396+
`object.GetFrameHtmlSource(Index As Integer)`
397+
319398
##### GetSelectedText
320399
Asynchronously retrieves the currently selected text (sent via OnMessageReceived with 'SELECTED_TEXT|').
321400
`object.GetSelectedText()`
@@ -622,12 +701,12 @@ Fired when the browser requires basic authentication credentials for a URI.
622701

623702
---
624703

625-
### JsonParser (ProgId: NetJson.Parser)
704+
### WebView2Parser (ProgId: NetWebView2Lib.WebView2Parser)
626705

627706
#### ===Properties===
628707
##### Version
629-
Allows AutoIt to verify the DLL version at runtime for compatibility checks.
630-
`object.version
708+
Allows AutoIt to verify the DLL version at runtime for compatibility checks.
709+
`object.Version`
631710

632711
#### ===Methods===
633712

@@ -751,3 +830,4 @@ Sorts a JSON array by a specific key.
751830
Removes duplicate objects from a JSON array based on a key's value.
752831
`bool SelectUnique(ArrayPath As String, Key As String)`
753832

833+
---

examples/014-pdfJS-Static_PDF_Viewer.au3

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Func _Example()
5252
#EndRegion ; GUI CREATION
5353

5454
; Adds a JavaScript library to be executed before any other script when a new page is loaded.
55-
Local $sScriptId = New_NetWebView2_AddInitializationScript($oWeb, @ScriptDir & "\JS_Lib\NetWebView2Lib_pdfjs_Tools.js")
55+
Local $sScriptId = _NetWebView2_AddInitializationScript($oWeb, @ScriptDir & "\JS_Lib\NetWebView2Lib_pdfjs_Tools.js")
5656
ConsoleWrite("$sScriptId=" & $sScriptId & @CRLF)
5757

5858
; navigate to the page
@@ -306,15 +306,3 @@ Func __SetupStaticPDF(ByRef $oWeb, $s_PDF_Path, $sExpectedTitle, $bBlockLinks =
306306
$oWeb.DisableBrowserFeatures()
307307
$oWeb.LockWebView()
308308
EndFunc ;==>__SetupStaticPDF
309-
310-
; New to replace _NetWebView2_AddInitializationScript in UDF
311-
Func New_NetWebView2_AddInitializationScript($oWebV2M, $vScript)
312-
If (Not IsObj($oWebV2M)) Or ObjName($oWebV2M, $OBJ_PROGID) <> 'NetWebView2.Manager' Then Return SetError(1, 0, "ERROR: Invalid Object")
313-
314-
; Smart Detection
315-
If FileExists($vScript) Then $vScript = FileRead($vScript)
316-
317-
Local $sScriptId = $oWebV2M.AddInitializationScript($vScript)
318-
If StringInStr($sScriptId, "ERROR:") Then Return SetError(2, 0, $sScriptId)
319-
Return SetError(0, 0, $sScriptId)
320-
EndFunc ;==>New_NetWebView2_AddInitializationScript

examples/017-MappedFolder.au3

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#AutoIt3Wrapper_UseX64=y
2+
#AutoIt3Wrapper_Run_AU3Check=Y
3+
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y
4+
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
5+
6+
; 017-MappedFolder.au3
7+
8+
#include <GUIConstantsEx.au3>
9+
#include <WindowsConstants.au3>
10+
11+
#include "..\NetWebView2Lib.au3"
12+
13+
_Example()
14+
15+
Func _Example()
16+
Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc)
17+
#forceref $oMyError
18+
19+
#Region ; GUI CREATION
20+
21+
; Create the GUI
22+
Local $hGUI = GUICreate("WebView2 .NET Manager - MappedFolder Demo", 1000, 800, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPCHILDREN))
23+
GUISetBkColor(0x1A1A1A, $hGUI)
24+
25+
; Initialize WebView2 Manager and register events
26+
Local $oWebV2M = _NetWebView2_CreateManager()
27+
If @error Then Return SetError(@error, @extended, $oWebV2M)
28+
29+
; initialize browser - put it on the GUI
30+
Local $sProfileDirectory = @ScriptDir & "\NetWebView2Lib-UserDataFolder"
31+
_NetWebView2_Initialize($oWebV2M, $hGUI, $sProfileDirectory, 0, 0, 0, 0, True, True, 1.2, "0x2B2B2B")
32+
33+
; Now that it is Ready, we define Mapping & Scripts
34+
Local $sLocalFolder = @ScriptDir & "\MappedFolder"
35+
_NetWebView2_SetVirtualHostNameToFolderMapping($oWebV2M, "myapp.local", $sLocalFolder, 0)
36+
37+
; Finish Navigate
38+
_NetWebView2_Navigate($oWebV2M, "https://myapp.local/index.html")
39+
40+
; show the GUI after browser was fully initialized
41+
GUISetState(@SW_SHOW)
42+
43+
#EndRegion ; GUI CREATION
44+
45+
46+
#Region ; GUI Loop
47+
; Main Loop
48+
While 1
49+
Switch GUIGetMsg()
50+
Case $GUI_EVENT_CLOSE
51+
ExitLoop
52+
EndSwitch
53+
WEnd
54+
55+
Local $oJSBridge
56+
_NetWebView2_CleanUp($oWebV2M, $oJSBridge)
57+
GUIDelete($hGUI)
58+
#EndRegion ; GUI Loop
59+
60+
EndFunc ;==>_Example
61+

examples/MappedFolder/index.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>1.5.1 Virtual Host Demo</title>
6+
<style>
7+
body { background: #1a1a1a; color: #00ff00; font-family: 'Courier New', Courier, monospace; text-align: center; padding-top: 50px; }
8+
.box { border: 2px solid #00ff00; display: inline-block; padding: 20px; border-radius: 10px; }
9+
</style>
10+
</head>
11+
<body>
12+
<div class="box">
13+
<h1>🏠 Virtual Host Mapping OK!</h1>
14+
<p>This file is served from a local folder via <b>https://myapp.local</b></p>
15+
<p id="script-status">Initialization Script Status: Waiting...</p>
16+
</div>
17+
<script>
18+
// This will be updated by the initialization script if it wasn't removed too fast
19+
window.setScriptStatus = function(status) {
20+
document.getElementById('script-status').innerText = "Initialization Script Status: " + status;
21+
};
22+
</script>
23+
</body>
24+
</html>

0 commit comments

Comments
 (0)