Quellcode durchsuchen

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

[skip ci]
Ben Hamlin vor 4 Jahren
Ursprung
Commit
81e0360ede
Es ist kein Benutzerkonto mit dieser Commiter-Email verbunden

+ 15
- 15
windows/ReactNativeWebView/ReactWebView.cpp Datei anzeigen

@@ -97,7 +97,7 @@ namespace winrt::ReactNativeWebView::implementation {
97 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 101
         winrt::hstring postMessage = L"window.ReactNativeWebView = {postMessage: function (data) {window.external.notify(String(data))}};";
102 102
         m_webView.InvokeScriptAsync(L"eval", { windowAlert + postMessage });
103 103
     }
@@ -119,26 +119,26 @@ namespace winrt::ReactNativeWebView::implementation {
119 119
 
120 120
     void ReactWebView::OnScriptNotify(winrt::IInspectable const& /*sender*/, winrt::Windows::UI::Xaml::Controls::NotifyEventArgs const& args) {
121 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 123
             auto type = jsonObject.GetNamedString(L"type");
124
-            if (type == L"alert") {
124
+            if (type == L"__alert") {
125 125
                 auto dialog = winrt::MessageDialog(jsonObject.GetNamedString(L"message"));
126 126
                 dialog.Commands().Append(winrt::UICommand(L"OK"));
127 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 144
 } // namespace winrt::ReactNativeWebView::implementation

+ 2
- 1
windows/ReactNativeWebView/ReactWebViewManager.cpp Datei anzeigen

@@ -116,6 +116,7 @@ namespace winrt::ReactNativeWebView::implementation {
116 116
         FrameworkElement const& view,
117 117
         int64_t commandId,
118 118
         winrt::IJSValueReader const& commandArgsReader) noexcept {
119
+        auto commandArgs = JSValue::ReadArrayFrom(commandArgsReader);
119 120
         if (auto webView = view.try_as<winrt::WebView>()) {
120 121
             switch (commandId) {
121 122
                 case static_cast<int64_t>(WebViewCommands::GoForward) :
@@ -135,7 +136,7 @@ namespace winrt::ReactNativeWebView::implementation {
135 136
                     webView.Stop();
136 137
                     break;
137 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 140
                     break;
140 141
             }
141 142
         }