0
votes
Need to get callback from webpage to my "WebView" in C# using scriptnotify. But it's not working. 

I hosted a webpage locally using node js. Webpage is as below.

<!DOCTYPE html>
<html>
<body>
<h1>The onclick Event</h1>
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
<script> 
function myFunction() {
  document.getElementById("demo").innerHTML = "latest";
  window.external.notify('The script says the doubled value is ' );
}
</script>
</body>
</html>

After that created a UWP app to open the webpage using webview. Added window.external.notify in the JS to get the call back. But not getting the same. //C# code to get the callback public sealed partial class MainPage : Page { public MainPage() {
this.InitializeComponent(); //Added the notification handler here webView.ScriptNotify += webView_ScriptNotify; //Navigating to the local page webView.Navigate(new Uri("http://localhost:8080/")); }

        async void webView_ScriptNotify(object sender, NotifyEventArgs e)
        {
            //Kept break point here , but it's not getting hit any time. 
            var jsScriptValue = e.Value;
            Debug.WriteLine("Send to debug output.");
        }
    }   

 //Added a web view control in xaml file
 <Grid>
    <WebView x:Name="webView" Height="500"> </WebView>
</Grid>
1

1 Answers

0
votes

UWP Webview ScriptNotify is not getting trigerred

Please check WebView document.

To enable an external web page to fire the ScriptNotify event when calling window.external.notify, you must include the page's URI in the ApplicationContentUriRules section of the app manifest. (You can do this in Microsoft Visual Studio on the Content URIs tab of the Package.appxmanifest designer.) The URIs in this list must use HTTPS, and may contain subdomain wildcards (for example, https://.microsoft.com) but they cannot contain domain wildcards (for example, https://.com and https://.). The manifest requirement does not apply to content that originates from the app package, uses an ms-local-stream:// URI, or is loaded using NavigateToString.

So, you need add your uri to the content uri option.

enter image description here