Skip to content

Commit f93d132

Browse files
Merge pull request #95 from EnsembleUI/webview-javascriptChannels
Add documentation for JavaScript Channels in WebView Widget
2 parents c172f7a + 55b87a6 commit f93d132

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

pages/widgets/webview.mdx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ The WebView Widget enables seamless integration of web content within native app
1919
| headerOverrideRules | array | Rules for overriding headers based on URL patterns. See [Header Override Rules](#header-override-rules) for details. |
2020
| cookies | array | Array of cookie objects for native applications. See [Cookies](#cookies) for details. |
2121
| cookieHeader | string | Direct set-cookie header string for native applications. |
22+
| javascriptChannels | array | Define named JavaScript channels to receive messages from the loaded page. See [JavaScript Channels](#javascript-channels) for details. |
2223
| styles | object | [See properties](#styles) |
2324
| allowedLaunchSchemes | array of strings | Only available on iOS and Android native apps. Will not have any affect on the web. Optionally specify array of url schemes such as 'tel:', 'mailto:', 'geo:' that when tapped should launch the apps corresponding to the scheme in the system the app is running in.. See below for more details for this property. URL schemes such as http and https are included by default and cannot be overwritten. |
2425

@@ -55,6 +56,53 @@ For example - if you are displaying messages inside your webview and want the us
5556

5657
In order to add additional schemes to the default schemes, specify the default schemes and the additional schemes as an array value of allowedLaunchSchemes. URL schemes such as http and https are included by default and cannot be overwritten.
5758

59+
## JavaScript Channels
60+
61+
Use `javascriptChannels` to receive messages sent from the page loaded in the WebView. Each channel has a `name` and an `onMessageReceived` handler. Messages arrive as strings in `event.data.message`.
62+
63+
Example:
64+
65+
```yaml
66+
- WebView:
67+
id: testWebView
68+
url: https://10.0.2.2:5501/test_webview_channels.html
69+
styles:
70+
height: 300
71+
javascriptChannels:
72+
- name: AppBridge
73+
onMessageReceived:
74+
executeCode:
75+
body: |
76+
console.log("Received from AppBridge: " + event.data.message);
77+
myMessage.text = event.data.message;
78+
79+
- name: PaymentChannel
80+
onMessageReceived:
81+
executeCode:
82+
body: |
83+
console.log("Received from PaymentChannel: " + event.data.message);
84+
var data = JSON.parse(event.data.message);
85+
if (data.action === "process_payment") {
86+
paymentStatus.text = 'Processing payment...';
87+
}
88+
```
89+
90+
From the page, call `YourChannelName.postMessage(payload)` where `payload` is a string (use `JSON.stringify` for structured data).
91+
92+
Sending messages from the page:
93+
94+
```html
95+
<script>
96+
// Send a simple string
97+
window.AppBridge.postMessage('Hello from WebView!');
98+
99+
// Send structured data as JSON string
100+
window.PaymentChannel.postMessage(
101+
JSON.stringify({ action: 'process_payment', amount: 29.99, currency: 'USD' })
102+
);
103+
</script>
104+
```
105+
58106
## Header Override Rules
59107

60108
The `headerOverrideRules` property allows you to specify different headers for different URLs based on patterns. This is useful when you need to apply different headers for different domains or API endpoints. Each rule consists of:

0 commit comments

Comments
 (0)