@@ -68,12 +68,12 @@ interface DotNetInvokeResult {
68
68
}
69
69
70
70
// Determine the mechanism to receive messages from the host application.
71
- if ( window . chrome ?. webview ? .addEventListener ) {
71
+ if ( window . chrome && window . chrome . webview && window . chrome . webview . addEventListener ) {
72
72
// Windows WebView2
73
73
window . chrome . webview . addEventListener ( 'message' , ( arg : any ) => {
74
74
dispatchHybridWebViewMessage ( arg . data ) ;
75
75
} ) ;
76
- } else if ( window . webkit ?. messageHandlers ? .webwindowinterop ) {
76
+ } else if ( window . webkit && window . webkit . messageHandlers && window . webkit . messageHandlers . webwindowinterop ) {
77
77
// iOS and MacCatalyst WKWebView
78
78
// @ts -ignore - We are extending the global object here
79
79
window . external = {
@@ -89,10 +89,10 @@ interface DotNetInvokeResult {
89
89
}
90
90
91
91
// Determine the function to use to send messages to the host application.
92
- if ( window . chrome ? .webview ) {
92
+ if ( window . chrome && window . chrome . webview ) {
93
93
// Windows WebView2
94
94
sendMessageFunction = msg => window . chrome . webview . postMessage ( msg ) ;
95
- } else if ( window . webkit ?. messageHandlers ? .webwindowinterop ) {
95
+ } else if ( window . webkit && window . webkit . messageHandlers && window . webkit . messageHandlers . webwindowinterop ) {
96
96
// iOS and MacCatalyst WKWebView
97
97
sendMessageFunction = msg => window . webkit . messageHandlers . webwindowinterop . postMessage ( msg ) ;
98
98
} else if ( window . hybridWebViewHost ) {
@@ -160,89 +160,93 @@ interface DotNetInvokeResult {
160
160
sendMessageToDotNet ( '__InvokeJavaScriptFailed' , taskId + '|' + json ) ;
161
161
}
162
162
163
- const HybridWebView = {
164
-
165
- /*
166
- * Send a raw message to the .NET host application.
167
- * The message is sent directly and not processed or serialized.
168
- *
169
- * @param message The message to send to the .NET host application.
170
- */
171
- SendRawMessage : function ( message : string ) {
172
- sendMessageToDotNet ( '__RawMessage' , message ) ;
173
- } ,
174
-
175
- /*
176
- * Invoke a .NET method on the InvokeJavaScriptTarget instance.
177
- * The method name and parameters are serialized and sent to the .NET host application.
178
- *
179
- * @param methodName The name of the .NET method to invoke.
180
- * @param paramValues The parameters to pass to the .NET method. If the method takes no parameters, this can be omitted.
181
- *
182
- * @returns A promise that resolves with the result of the .NET method invocation.
183
- */
184
- InvokeDotNet : async function ( methodName : string , paramValues ?: any ) {
185
- const body : JSInvokeMethodData = {
186
- MethodName : methodName
187
- } ;
163
+ /*
164
+ * Send a raw message to the .NET host application.
165
+ * The message is sent directly and not processed or serialized.
166
+ *
167
+ * @param message The message to send to the .NET host application.
168
+ */
169
+ function sendRawMessage ( message : string ) {
170
+ sendMessageToDotNet ( '__RawMessage' , message ) ;
171
+ }
188
172
189
- // if parameters were provided, serialize them first
190
- if ( paramValues !== undefined ) {
191
- if ( ! Array . isArray ( paramValues ) ) {
192
- paramValues = [ paramValues ] ;
193
- }
173
+ /*
174
+ * Invoke a .NET method on the InvokeJavaScriptTarget instance.
175
+ * The method name and parameters are serialized and sent to the .NET host application.
176
+ *
177
+ * @param methodName The name of the .NET method to invoke.
178
+ * @param paramValues The parameters to pass to the .NET method. If the method takes no parameters, this can be omitted.
179
+ *
180
+ * @returns A promise that resolves with the result of the .NET method invocation.
181
+ */
182
+ async function invokeDotNet ( methodName : string , paramValues ?: any ) {
183
+ const body : JSInvokeMethodData = {
184
+ MethodName : methodName
185
+ } ;
194
186
195
- for ( let i = 0 ; i < paramValues . length ; i ++ ) {
196
- paramValues [ i ] = JSON . stringify ( paramValues [ i ] ) ;
197
- }
187
+ // if parameters were provided, serialize them first
188
+ if ( paramValues !== undefined ) {
189
+ if ( ! Array . isArray ( paramValues ) ) {
190
+ paramValues = [ paramValues ] ;
191
+ }
198
192
199
- if ( paramValues . length > 0 ) {
200
- body . ParamValues = paramValues ;
201
- }
193
+ for ( let i = 0 ; i < paramValues . length ; i ++ ) {
194
+ paramValues [ i ] = JSON . stringify ( paramValues [ i ] ) ;
202
195
}
203
196
204
- const message = JSON . stringify ( body ) ;
197
+ if ( paramValues . length > 0 ) {
198
+ body . ParamValues = paramValues ;
199
+ }
200
+ }
205
201
206
- const requestUrl = ` ${ window . location . origin } /__hwvInvokeDotNet?data= ${ encodeURIComponent ( message ) } ` ;
202
+ const message = JSON . stringify ( body ) ;
207
203
208
- const rawResponse = await fetch ( requestUrl , {
209
- method : 'GET' ,
210
- headers : {
211
- 'Accept' : 'application/json'
212
- }
213
- } ) ;
214
- const response : DotNetInvokeResult = await rawResponse . json ( ) ;
204
+ const requestUrl = `${ window . location . origin } /__hwvInvokeDotNet?data=${ encodeURIComponent ( message ) } ` ;
215
205
216
- if ( ! response ) {
217
- return null ;
206
+ const rawResponse = await fetch ( requestUrl , {
207
+ method : 'GET' ,
208
+ headers : {
209
+ 'Accept' : 'application/json'
218
210
}
211
+ } ) ;
212
+ const response : DotNetInvokeResult = await rawResponse . json ( ) ;
219
213
220
- if ( response . IsJson ) {
221
- return JSON . parse ( response . Result ) ;
222
- }
214
+ if ( ! response ) {
215
+ return null ;
216
+ }
223
217
224
- return response . Result ;
225
- } ,
226
-
227
- /*
228
- * Invoke a JavaScript method from the .NET host application.
229
- * This method is called from the HybridWebViewHandler and is not intended to be used by user applications.
230
- *
231
- * @param taskId The task ID that was provided by the .NET host application.
232
- * @param methodName The JavaScript method to invoke in the global scope.
233
- * @param args The arguments to pass to the JavaScript method.
234
- *
235
- * @returns A promise.
236
- */
237
- __InvokeJavaScript : async function ( taskId : string , methodName : Function , args : any [ ] ) {
238
- try {
239
- const result = await methodName ( ...args ) ;
240
- invokeJavaScriptCallbackInDotNet ( taskId , result ) ;
241
- } catch ( ex ) {
242
- console . error ( ex ) ;
243
- invokeJavaScriptFailedInDotNet ( taskId , ex ) ;
244
- }
218
+ if ( response . IsJson ) {
219
+ return JSON . parse ( response . Result ) ;
220
+ }
221
+
222
+ return response . Result ;
223
+ }
224
+
225
+ /*
226
+ * Invoke a JavaScript method from the .NET host application.
227
+ * This method is called from the HybridWebViewHandler and is not intended to be used by user applications.
228
+ *
229
+ * @param taskId The task ID that was provided by the .NET host application.
230
+ * @param methodName The JavaScript method to invoke in the global scope.
231
+ * @param args The arguments to pass to the JavaScript method.
232
+ *
233
+ * @returns A promise.
234
+ */
235
+ async function invokeJavaScript ( taskId : string , methodName : Function , args : any [ ] ) {
236
+ try {
237
+ const result = await methodName ( ...args ) ;
238
+ invokeJavaScriptCallbackInDotNet ( taskId , result ) ;
239
+ } catch ( ex ) {
240
+ console . error ( ex ) ;
241
+ invokeJavaScriptFailedInDotNet ( taskId , ex ) ;
245
242
}
243
+ }
244
+
245
+ // Define the public API of the HybridWebView control.
246
+ const HybridWebView = {
247
+ SendRawMessage : sendRawMessage ,
248
+ InvokeDotNet : invokeDotNet ,
249
+ __InvokeJavaScript : invokeJavaScript
246
250
} ;
247
251
248
252
// Make the following APIs available in global scope for invocation from JS
0 commit comments