|
@@ -61,13 +61,6 @@ namespace winrt::ReactNativeWebView::implementation {
|
61
|
61
|
self->OnNavigationFailed(sender, args);
|
62
|
62
|
}
|
63
|
63
|
});
|
64
|
|
-
|
65
|
|
- m_scriptNotifyRevoker = m_webView.ScriptNotify(
|
66
|
|
- winrt::auto_revoke, [ref = get_weak()](auto const& sender, auto const& args) {
|
67
|
|
- if (auto self = ref.get()) {
|
68
|
|
- self->OnScriptNotify(sender, args);
|
69
|
|
- }
|
70
|
|
- });
|
71
|
64
|
}
|
72
|
65
|
|
73
|
66
|
void ReactWebView::WriteWebViewNavigationEventArg(winrt::WebView const& sender, winrt::IJSValueWriter const& eventDataWriter) {
|
|
@@ -118,15 +111,11 @@ namespace winrt::ReactNativeWebView::implementation {
|
118
|
111
|
eventDataWriter.WriteObjectEnd();
|
119
|
112
|
});
|
120
|
113
|
|
121
|
|
- winrt::hstring windowAlert = L"window.alert = function (msg) {window.external.notify(`{\"type\":\"__alert\",\"message\":\"${msg}\"}`)};";
|
122
|
114
|
if (m_messagingEnabled) {
|
|
115
|
+ winrt::hstring windowAlert = L"window.alert = function (msg) {__REACT_WEB_VIEW_BRIDGE.postMessage(`{\"type\":\"__alert\",\"message\":\"${msg}\"}`)};";
|
123
|
116
|
winrt::hstring postMessage = L"window.ReactNativeWebView = {postMessage: function (data) {__REACT_WEB_VIEW_BRIDGE.postMessage(String(data))}};";
|
124
|
117
|
webView.InvokeScriptAsync(L"eval", { windowAlert + postMessage });
|
125
|
118
|
}
|
126
|
|
- else {
|
127
|
|
- winrt::hstring postMessage = L"window.ReactNativeWebView = {postMessage: function (data) {window.external.notify(String(data))}};";
|
128
|
|
- webView.InvokeScriptAsync(L"eval", { windowAlert + postMessage });
|
129
|
|
- }
|
130
|
119
|
}
|
131
|
120
|
|
132
|
121
|
void ReactWebView::OnNavigationFailed(winrt::IInspectable const& /*sender*/, winrt::WebViewNavigationFailedEventArgs const& args) {
|
|
@@ -144,36 +133,32 @@ namespace winrt::ReactNativeWebView::implementation {
|
144
|
133
|
});
|
145
|
134
|
}
|
146
|
135
|
|
147
|
|
- void ReactWebView::OnScriptNotify(winrt::IInspectable const& /*sender*/, winrt::Windows::UI::Xaml::Controls::NotifyEventArgs const& args) {
|
148
|
|
- winrt::JsonObject jsonObject;
|
149
|
|
- if (winrt::JsonObject::TryParse(args.Value(), jsonObject) && jsonObject.HasKey(L"type")) {
|
150
|
|
- auto type = jsonObject.GetNamedString(L"type");
|
151
|
|
- if (type == L"__alert") {
|
152
|
|
- auto dialog = winrt::MessageDialog(jsonObject.GetNamedString(L"message"));
|
153
|
|
- dialog.Commands().Append(winrt::UICommand(L"OK"));
|
154
|
|
- dialog.ShowAsync();
|
155
|
|
- return;
|
156
|
|
- }
|
157
|
|
- }
|
158
|
|
-
|
159
|
|
- PostMessage(winrt::hstring(args.Value()));
|
160
|
|
- }
|
161
|
|
-
|
162
|
136
|
void ReactWebView::PostMessage(winrt::hstring const& message) {
|
|
137
|
+ winrt::JsonObject jsonObject;
|
|
138
|
+ if (winrt::JsonObject::TryParse(message, jsonObject) && jsonObject.HasKey(L"type")) {
|
|
139
|
+ auto type = jsonObject.GetNamedString(L"type");
|
|
140
|
+ if (type == L"__alert") {
|
|
141
|
+ auto dialog = winrt::MessageDialog(jsonObject.GetNamedString(L"message"));
|
|
142
|
+ dialog.Commands().Append(winrt::UICommand(L"OK"));
|
|
143
|
+ dialog.ShowAsync();
|
|
144
|
+ return;
|
|
145
|
+ }
|
|
146
|
+ }
|
|
147
|
+
|
163
|
148
|
m_reactContext.DispatchEvent(
|
164
|
|
- *this,
|
165
|
|
- L"topMessage",
|
166
|
|
- [&](winrt::Microsoft::ReactNative::IJSValueWriter const& eventDataWriter) noexcept {
|
167
|
|
- eventDataWriter.WriteObjectBegin();
|
168
|
|
- {
|
169
|
|
- WriteProperty(eventDataWriter, L"data", message);
|
170
|
|
- }
|
171
|
|
- eventDataWriter.WriteObjectEnd();
|
172
|
|
- });
|
|
149
|
+ *this,
|
|
150
|
+ L"topMessage",
|
|
151
|
+ [&](winrt::Microsoft::ReactNative::IJSValueWriter const& eventDataWriter) noexcept {
|
|
152
|
+ eventDataWriter.WriteObjectBegin();
|
|
153
|
+ {
|
|
154
|
+ WriteProperty(eventDataWriter, L"data", message);
|
|
155
|
+ }
|
|
156
|
+ eventDataWriter.WriteObjectEnd();
|
|
157
|
+ });
|
173
|
158
|
}
|
174
|
159
|
|
175
|
160
|
void ReactWebView::SetMessagingEnabled(bool enabled) {
|
176
|
|
- this->m_messagingEnabled = enabled;
|
|
161
|
+ m_messagingEnabled = enabled;
|
177
|
162
|
}
|
178
|
163
|
|
179
|
164
|
} // namespace winrt::ReactNativeWebView::implementation
|