Skip to content

Commit 38bea85

Browse files
committed
FEATURE AddHostObject and RemoveHostObject for integration
1 parent 6c4680a commit 38bea85

6 files changed

Lines changed: 45149 additions & 1 deletion

File tree

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,4 @@ Before `08/07/2021` a change log was not kept. We have retrospectively gone back
366366
- 2025-10-14 `stdJSON` FIX - Fix for negative number serialization.
367367
- 2025-11-21 `stdCallback` FIX - Fixing typos. See #146.
368368
- 2026-01-02 `stdHTTP` BREAKING - Mac compatibility
369+
- 2026-03-23 `stdWebView` FEATURE - Added `AddHostObject(name, hostObject)` and `RemoveHostObject(name)` to inject/remove VBA `IDispatch` objects in JavaScript via `chrome.webview.hostObjects`.

docs/stdWebView.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,33 @@ Queues script execution without blocking. If `callback` is set, it is invoked wi
8181
wv.JavaScriptRun "console.log(1)", stdLambda.Create("Debug.Print $1, $2") ' errorCode, resultJson
8282
```
8383

84+
#### `AddHostObject(ByVal name As String, ByVal hostObject As Object)`
85+
86+
Injects a VBA COM object into JavaScript as `chrome.webview.hostObjects.<name>`.
87+
88+
* Requires `IsReady`.
89+
* `name` must be non-empty.
90+
* `hostObject` must be a non-`Nothing` object that is dispatchable (`IDispatch`).
91+
92+
```vb
93+
' Class cBridge
94+
' Public Function Echo(ByVal s As String) As String: Echo = "VBA:" & s: End Function
95+
96+
Dim bridge As New cBridge
97+
wv.AddHostObject "bridge", bridge
98+
```
99+
100+
JavaScript usage (async host object proxy):
101+
102+
```js
103+
const value = await chrome.webview.hostObjects.bridge.Echo("hello");
104+
console.log(value); // "VBA:hello"
105+
```
106+
107+
#### `RemoveHostObject(ByVal name As String)`
108+
109+
Removes a previously injected object so `chrome.webview.hostObjects.<name>` is no longer available.
110+
84111
### Checklist (quick reference)
85112

86113
**Constructors**
@@ -97,6 +124,8 @@ wv.JavaScriptRun "console.log(1)", stdLambda.Create("Debug.Print $1, $2") ' err
97124
* [X] `Html` Get/Let
98125
* [X] `JavaScriptRunSync(script)`
99126
* [X] `JavaScriptRun(script, callback?)`
127+
* [X] `AddHostObject(name, hostObject)`
128+
* [X] `RemoveHostObject(name)`
100129

101130
### Protected / Friend API
102131

@@ -107,3 +136,4 @@ wv.JavaScriptRun "console.log(1)", stdLambda.Create("Debug.Print $1, $2") ' err
107136
* A per-instance user data folder is created under `%TEMP%` (`stdWebView_*`) for the WebView2 profile.
108137
* If `CreateCoreWebView2EnvironmentWithOptions` fails, the error is raised with the HRESULT from the loader.
109138
* `zzProtWebView_*` entry points must remain `Public` so thunk code can dispatch into the instance; they are not user APIs.
139+
* Vtable offsets used in `stdWebView.cls` are verified against `ICoreWebView2Vtbl` in `WebView2.h` from the WebView2 SDK (`build/native/include/WebView2.h` in the NuGet package). Official header reference: [WebView2.h](https://github.com/MicrosoftEdge/WebView2Browser/blob/main/packages/Microsoft.Web.WebView2.1.0.2903.40/build/native/include/WebView2.h).

0 commit comments

Comments
 (0)