2

We're using a UWP Webview on Windows 10 for one of our pages; in order to communicate with the application we want to use window.external.notify().

As documented here https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.webview.allowedscriptnotifyuris#Windows_UI_Xaml_Controls_WebView_AllowedScriptNotifyUris we have added all permutations of our sites domain to the content uri's section in Package.appxmanifest

However, window.external.notify is still undefined, has anyone experienced this. We have tried permutations as follows...

https://example.com

https://example.com/*

https://*.example.com/

https://*.example.com/*

But nothing has worked, any suggestions very much appreciated. (We know from debugging in the JS console that it is undefined)

As an alternative approach, we also tried deeplinking to ourselves instead, but that prompts every time 'AppFoo is trying to open AppFoo'

Many Thanks

JayTee
  • 1,209
  • 9
  • 15
  • Please try to test the [XamlWebView/cs/Scenario4_ScriptNotify.xaml](https://github.com/microsoft/Windows-universal-samples/blob/fe8567faf2efdea3672c2ba642ba7b925ff6467e/Samples/XamlWebView/cs/Scenario4_ScriptNotify.xaml) to see if it does works. – Xie Steven Jul 17 '19 at 14:37
  • Sorry that doesn't help as the example isn't using whitelisting https://github.com/microsoft/Windows-universal-samples/blob/fe8567faf2efdea3672c2ba642ba7b925ff6467e/Samples/XamlWebView/cs/Scenario4_ScriptNotify.xaml.cs#L26 my use case is for an external domain. – JayTee Jul 17 '19 at 17:13

2 Answers2

1

So the solution can be found here.

But TL;DR - We had to swap:

if (window.external && window.external.notify)

for

if (typeof (window.external) !== 'undefined' && ('notify' in window.external))

to correctly detect if this could be invoked. In a msft webview, the first statement evaluates to false and the second to true.

JayTee
  • 1,209
  • 9
  • 15
0

that doesn't help as the example isn't using whitelisting github.com/microsoft/Windows-universal-samples/blob/… my use case is for an external domain

I can reproduce your issue. You could try to use WebView.InvokeScriptAsync to inject the jsvascript code. For example, you could inject js code in DOMContentLoaded event handler like the following:

private async void WebView_DOMContentLoaded(WebView sender, WebViewDOMContentLoadedEventArgs args)
{
    string[] parameters = new string[] { "window.addEventListener('load', function() {window.external.notify('abc');})" };
    await sender.InvokeScriptAsync("eval", parameters);
}
Xie Steven
  • 8,544
  • 1
  • 9
  • 23