Browse Source

fix(windows): Fixes ScriptNotify and InvokeScript (#1354 by @benhamlin)

[skip ci]
Ben Hamlin 4 years ago
parent
commit
81e0360ede
No account linked to committer's email address

+ 15
- 15
windows/ReactNativeWebView/ReactWebView.cpp View File

97
                 eventDataWriter.WriteObjectEnd();
97
                 eventDataWriter.WriteObjectEnd();
98
             });
98
             });
99
 
99
 
100
-        winrt::hstring windowAlert = L"window.alert = function (msg) {window.external.notify(`{\"type\":\"alert\",\"message\":\"${msg}\"}`)};";
100
+        winrt::hstring windowAlert = L"window.alert = function (msg) {window.external.notify(`{\"type\":\"__alert\",\"message\":\"${msg}\"}`)};";
101
         winrt::hstring postMessage = L"window.ReactNativeWebView = {postMessage: function (data) {window.external.notify(String(data))}};";
101
         winrt::hstring postMessage = L"window.ReactNativeWebView = {postMessage: function (data) {window.external.notify(String(data))}};";
102
         m_webView.InvokeScriptAsync(L"eval", { windowAlert + postMessage });
102
         m_webView.InvokeScriptAsync(L"eval", { windowAlert + postMessage });
103
     }
103
     }
119
 
119
 
120
     void ReactWebView::OnScriptNotify(winrt::IInspectable const& /*sender*/, winrt::Windows::UI::Xaml::Controls::NotifyEventArgs const& args) {
120
     void ReactWebView::OnScriptNotify(winrt::IInspectable const& /*sender*/, winrt::Windows::UI::Xaml::Controls::NotifyEventArgs const& args) {
121
         winrt::JsonObject jsonObject;
121
         winrt::JsonObject jsonObject;
122
-        if (winrt::JsonObject::TryParse(args.Value(), jsonObject)) {
122
+        if (winrt::JsonObject::TryParse(args.Value(), jsonObject) && jsonObject.HasKey(L"type")) {
123
             auto type = jsonObject.GetNamedString(L"type");
123
             auto type = jsonObject.GetNamedString(L"type");
124
-            if (type == L"alert") {
124
+            if (type == L"__alert") {
125
                 auto dialog = winrt::MessageDialog(jsonObject.GetNamedString(L"message"));
125
                 auto dialog = winrt::MessageDialog(jsonObject.GetNamedString(L"message"));
126
                 dialog.Commands().Append(winrt::UICommand(L"OK"));
126
                 dialog.Commands().Append(winrt::UICommand(L"OK"));
127
                 dialog.ShowAsync();
127
                 dialog.ShowAsync();
128
+                return;
128
             }
129
             }
129
         }
130
         }
130
-        else {
131
-            m_reactContext.DispatchEvent(
132
-                m_webView,
133
-                L"topMessage",
134
-                [&](winrt::Microsoft::ReactNative::IJSValueWriter const& eventDataWriter) noexcept {
135
-                    eventDataWriter.WriteObjectBegin();
136
-                    {
137
-                        WriteProperty(eventDataWriter, L"data", winrt::to_string(args.Value()));
138
-                    }
139
-                    eventDataWriter.WriteObjectEnd();
140
-                });
141
-        }
131
+
132
+        m_reactContext.DispatchEvent(
133
+            m_webView,
134
+            L"topMessage",
135
+            [&](winrt::Microsoft::ReactNative::IJSValueWriter const& eventDataWriter) noexcept {
136
+                eventDataWriter.WriteObjectBegin();
137
+                {
138
+                    WriteProperty(eventDataWriter, L"data", winrt::to_string(args.Value()));
139
+                }
140
+                eventDataWriter.WriteObjectEnd();
141
+            });
142
     }
142
     }
143
 
143
 
144
 } // namespace winrt::ReactNativeWebView::implementation
144
 } // namespace winrt::ReactNativeWebView::implementation

+ 2
- 1
windows/ReactNativeWebView/ReactWebViewManager.cpp View File

116
         FrameworkElement const& view,
116
         FrameworkElement const& view,
117
         int64_t commandId,
117
         int64_t commandId,
118
         winrt::IJSValueReader const& commandArgsReader) noexcept {
118
         winrt::IJSValueReader const& commandArgsReader) noexcept {
119
+        auto commandArgs = JSValue::ReadArrayFrom(commandArgsReader);
119
         if (auto webView = view.try_as<winrt::WebView>()) {
120
         if (auto webView = view.try_as<winrt::WebView>()) {
120
             switch (commandId) {
121
             switch (commandId) {
121
                 case static_cast<int64_t>(WebViewCommands::GoForward) :
122
                 case static_cast<int64_t>(WebViewCommands::GoForward) :
135
                     webView.Stop();
136
                     webView.Stop();
136
                     break;
137
                     break;
137
                 case static_cast<int64_t>(WebViewCommands::InjectJavaScript) :
138
                 case static_cast<int64_t>(WebViewCommands::InjectJavaScript) :
138
-                    webView.InvokeScriptAsync(L"eval", { commandArgsReader.GetString() });
139
+                    webView.InvokeScriptAsync(L"eval", { winrt::to_hstring(commandArgs[0].AsString()) });
139
                     break;
140
                     break;
140
             }
141
             }
141
         }
142
         }