Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScriptNotify in WPF WebView does not fire as expected #2346

Closed
rjmurillo opened this issue Aug 2, 2018 · 2 comments
Closed

ScriptNotify in WPF WebView does not fire as expected #2346

rjmurillo opened this issue Aug 2, 2018 · 2 comments
Assignees
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior controls 🎛️ in progress 🚧 WebView 🖥️ WPF and WinForms WebView win 3️⃣2️⃣
Milestone

Comments

@rjmurillo
Copy link
Contributor

I'm submitting a...

  • Bug report (I searched for similar issues and did not find one)

Current behavior

Given the following code

MainWindow.xaml

<WPF:WebView
  x:Name="WebView"
  DOMContentLoaded="WebView_DOMContentLoaded"
  IsScriptNotifyAllowed="True"
  ScriptNotify="WebView_ScriptNotify"
  Source="http://www.example.com" />

MainWindow.xaml.cs

private void WebView_DOMContentLoaded(object sender, Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.WebViewControlDOMContentLoadedEventArgs e)
{
  WebView.InvokeScriptAsync("eval", "window.external.notify('Hello world!');");
}

private void WebView_ScriptNotify(object sender, Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.WebViewControlScriptNotifyEventArgs e)
{
  MessageBox.Show(e.Value, e.Uri?.ToString() ?? "Message from WebView", MessageBoxButton.OK);
}

The event handler WebView_ScriptNotify is never fired.

Expected behavior

The WebView_ScriptNotify event handler is fired

Minimal reproduction of the problem with instructions

Environment

Nuget Package(s): Microsoft.Toolkit.Win32.UI.Controls

Package Version(s): 3.0.0

Windows 10 Build Number:
- [ ] Creators Update (15063)
- [ ] Fall Creators Update (16299)
- [x April 2018 Update (17134)
- [x Insider Build (build number: 18209

App min and target version:
- [ ] Creators Update (15063)
- [ ] Fall Creators Update (16299)
- [x] April 2018 Update (17134)
- [ ] Insider Build (18209)

Device form factor:
- [x] Desktop
- [ ] Mobile
- [ ] Xbox
- [ ] Surface Hub
- [ ] IoT

Visual Studio 
- [x] 2017 (version: 15.7.8)
- [x] 2017 Preview (version: 15.8)

Filed on behalf of question on Stack Overflow: How to add a list of allowed URI's for WebView's “ScriptNotify” in a WPF app?

@rjmurillo rjmurillo added bug 🐛 An unexpected issue that highlights incorrect behavior controls 🎛️ win 3️⃣2️⃣ WebView 🖥️ WPF and WinForms WebView labels Aug 2, 2018
@rjmurillo rjmurillo self-assigned this Aug 2, 2018
@rjmurillo
Copy link
Contributor Author

There is a timing issue here where a number of methods can be invoked before initialization is completed. In this case, properties are read from the control via the dispatcher; however, navigation occurs and DOMLoadEvent is fired before initialization finishes setting properties for enabling JavaScript, IndexedDb, and ScriptNotify.

rjmurillo added a commit to rjmurillo/UWPCommunityToolkit that referenced this issue Aug 2, 2018
An issue may occur where methods, such as InvokeScript/InvokeScriptAsync, are permitted to be invoked prior to the WebView completing initialization. As such, properties may not yet be set for permitting JavaScript or ScriptNotify, causing inconsistencies with application behavior.

Additionally, the Source property is set, causing an immediate navigation before other properties are also set. The code has been updated for WPF to follow a similar order for WinForms.

See CommunityToolkit#2346, CommunityToolkit#2347
@rjmurillo rjmurillo added this to the 4.0 milestone Aug 2, 2018
@rjmurillo
Copy link
Contributor Author

Closed via #2347 and #2349

@ghost ghost locked as resolved and limited conversation to collaborators Nov 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior controls 🎛️ in progress 🚧 WebView 🖥️ WPF and WinForms WebView win 3️⃣2️⃣
Projects
None yet
Development

No branches or pull requests

1 participant