|
1 | 1 | window.HybridWebView = {
|
2 |
| - "Init": function () { |
| 2 | + "Init": function Init() { |
3 | 3 | function DispatchHybridWebViewMessage(message) {
|
4 | 4 | const event = new CustomEvent("HybridWebViewMessageReceived", { detail: { message: message } });
|
5 | 5 | window.dispatchEvent(event);
|
|
27 | 27 | }
|
28 | 28 | },
|
29 | 29 |
|
30 |
| - "SendRawMessage": function (message) { |
31 |
| - window.HybridWebView.__SendMessageInternal('RawMessage', message); |
| 30 | + "SendRawMessage": function SendRawMessage(message) { |
| 31 | + window.HybridWebView.__SendMessageInternal('__RawMessage', message); |
32 | 32 | },
|
33 | 33 |
|
34 |
| - "__SendMessageInternal": function (type, message) { |
| 34 | + "InvokeDotNet": async function InvokeDotNetAsync(methodName, paramValues) { |
| 35 | + const body = { |
| 36 | + MethodName: methodName |
| 37 | + }; |
| 38 | + |
| 39 | + if (typeof paramValues !== 'undefined') { |
| 40 | + if (!Array.isArray(paramValues)) { |
| 41 | + paramValues = [paramValues]; |
| 42 | + } |
| 43 | + |
| 44 | + for (var i = 0; i < paramValues.length; i++) { |
| 45 | + paramValues[i] = JSON.stringify(paramValues[i]); |
| 46 | + } |
| 47 | + |
| 48 | + if (paramValues.length > 0) { |
| 49 | + body.ParamValues = paramValues; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + const message = JSON.stringify(body); |
| 54 | + |
| 55 | + var requestUrl = `${window.location.origin}/__hwvInvokeDotNet?data=${encodeURIComponent(message)}`; |
| 56 | + |
| 57 | + const rawResponse = await fetch(requestUrl, { |
| 58 | + method: 'GET', |
| 59 | + headers: { |
| 60 | + 'Accept': 'application/json' |
| 61 | + } |
| 62 | + }); |
| 63 | + const response = await rawResponse.json(); |
| 64 | + |
| 65 | + if (response) { |
| 66 | + if (response.IsJson) { |
| 67 | + return JSON.parse(response.Result); |
| 68 | + } |
| 69 | + |
| 70 | + return response.Result; |
| 71 | + } |
| 72 | + |
| 73 | + return null; |
| 74 | + }, |
| 75 | + |
| 76 | + "__SendMessageInternal": function __SendMessageInternal(type, message) { |
35 | 77 |
|
36 | 78 | const messageToSend = type + '|' + message;
|
37 | 79 |
|
|
49 | 91 | }
|
50 | 92 | },
|
51 | 93 |
|
52 |
| - "InvokeMethod": function (taskId, methodName, args) { |
| 94 | + "__InvokeJavaScript": function __InvokeJavaScript(taskId, methodName, args) { |
53 | 95 | if (methodName[Symbol.toStringTag] === 'AsyncFunction') {
|
54 | 96 | // For async methods, we need to call the method and then trigger the callback when it's done
|
55 | 97 | const asyncPromise = methodName(...args);
|
|
65 | 107 | }
|
66 | 108 | },
|
67 | 109 |
|
68 |
| - "__TriggerAsyncCallback": function (taskId, result) { |
| 110 | + "__TriggerAsyncCallback": function __TriggerAsyncCallback(taskId, result) { |
69 | 111 | // Make sure the result is a string
|
70 | 112 | if (result && typeof (result) !== 'string') {
|
71 | 113 | result = JSON.stringify(result);
|
72 | 114 | }
|
73 | 115 |
|
74 |
| - window.HybridWebView.__SendMessageInternal('InvokeMethodCompleted', taskId + '|' + result); |
| 116 | + window.HybridWebView.__SendMessageInternal('__InvokeJavaScriptCompleted', taskId + '|' + result); |
75 | 117 | }
|
76 | 118 | }
|
77 | 119 |
|
|
0 commit comments