How can I stop the webpage address of saying about:srcdoc #3916
Replies: 5 comments
-
Hey @Green-Ten-Tech - What property or function are you using to get that url? That doesn't seem to be a valid "about:" url in the browser, and I'm unable to reproduce the problem. When I navigate to "https://bing.com", the Source property reflects that. |
Beta Was this translation helpful? Give feedback.
-
This is my code using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Web.WebView2.Core;
using EasyTabs;
namespace MicrosoftWebViewTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeEdge();
InitializeAsync();
}
async void InitializeEdge()
{
await webView21.EnsureCoreWebView2Async(null);
webView21.CoreWebView2.Navigate(Properties.Settings.Default.Homepage);
}
async void InitializeAsync()
{
await webView21.EnsureCoreWebView2Async(null);
webView21.CoreWebView2.WebMessageReceived += UpdateAddressBar;
await webView21.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("window.chrome.webview.postMessage(window.document.URL);");
}
private void UpdateAddressBar(object sender, CoreWebView2WebMessageReceivedEventArgs args)
{
String uri = args.TryGetWebMessageAsString();
textBox1.Text = uri;
webView21.CoreWebView2.PostWebMessageAsString(uri);
}
private void webView21_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
if (e.IsSuccess)
{
this.webView21.NavigationStarting += NavigationStarting;
this.webView21.NavigationCompleted += NavigationCompleted;
this.webView21.CoreWebView2.WebMessageReceived += WebMessageReceived;
this.webView21.CoreWebView2.DocumentTitleChanged += DocumentTitleChanged;
this.webView21.CoreWebView2.HistoryChanged += HistoryChanged;
this.webView21.KeyDown += WebViewKeyDown;
this.webView21.KeyUp += WebViewKeyUp;
}
}
private void NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
{
}
protected TitleBarTabs ParentTabs
{
get
{
return (ParentForm as TitleBarTabs);
}
}
private void NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
{
String uri = e.Uri;
if (!uri.StartsWith("https://"))
{
webView21.CoreWebView2.ExecuteScriptAsync($"alert('{uri} may not be safe, try an https link')");
e.Cancel = true;
}
}
private void WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e)
{
}
private void HistoryChanged(object sender, object e)
{
}
private void WebViewKeyUp(object sender, KeyEventArgs e)
{
}
private void WebViewKeyDown(object sender, KeyEventArgs e)
{
}
private void DocumentTitleChanged(object sender, object e)
{
this.Invoke((MethodInvoker)delegate
{
Text = this.webView21.CoreWebView2.DocumentTitle;
});
}
private void button1_Click(object sender, EventArgs e)
{
if (webView21 != null && webView21.CoreWebView2 != null)
{
if (textBox1.Text.Contains(".com"))
{
if (!textBox1.Text.StartsWith("https://"))
{
if (textBox1.Text.StartsWith("http://"))
{
webView21.CoreWebView2.Navigate(textBox1.Text);
}
else
{
textBox1.Text = "https://" + textBox1.Text;
webView21.CoreWebView2.Navigate(textBox1.Text);
}
}
else
{
webView21.CoreWebView2.Navigate(textBox1.Text);
}
}
else if (textBox1.Text.Contains(".net"))
{
if (!textBox1.Text.StartsWith("https://"))
{
textBox1.Text = "https://" + textBox1.Text;
webView21.CoreWebView2.Navigate(textBox1.Text);
}
else
{
webView21.CoreWebView2.Navigate(textBox1.Text);
}
}
else if (textBox1.Text.Contains(".org"))
{
if (!textBox1.Text.StartsWith("https://"))
{
textBox1.Text = "https://" + textBox1.Text;
webView21.CoreWebView2.Navigate(textBox1.Text);
}
else
{
webView21.CoreWebView2.Navigate(textBox1.Text);
}
}
else if (textBox1.Text.Contains(".gov"))
{
if (!textBox1.Text.StartsWith("https://"))
{
textBox1.Text = "https://" + textBox1.Text;
webView21.CoreWebView2.Navigate(textBox1.Text);
}
else
{
webView21.CoreWebView2.Navigate(textBox1.Text);
}
}
else
{
webView21.CoreWebView2.Navigate(Properties.Settings.Default.Search + textBox1.Text);
}
}
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
e.Handled = true;
if (webView21 != null && webView21.CoreWebView2 != null)
{
if (textBox1.Text.Contains(".com"))
{
if (!textBox1.Text.StartsWith("https://"))
{
if (textBox1.Text.StartsWith("http://"))
{
webView21.CoreWebView2.Navigate(textBox1.Text);
}
else
{
textBox1.Text = "https://" + textBox1.Text;
webView21.CoreWebView2.Navigate(textBox1.Text);
}
}
else
{
webView21.CoreWebView2.Navigate(textBox1.Text);
}
}
else if (textBox1.Text.Contains(".net"))
{
if (!textBox1.Text.StartsWith("https://"))
{
textBox1.Text = "https://" + textBox1.Text;
webView21.CoreWebView2.Navigate(textBox1.Text);
}
else
{
webView21.CoreWebView2.Navigate(textBox1.Text);
}
}
else if (textBox1.Text.Contains(".org"))
{
if (!textBox1.Text.StartsWith("https://"))
{
textBox1.Text = "https://" + textBox1.Text;
webView21.CoreWebView2.Navigate(textBox1.Text);
}
else
{
webView21.CoreWebView2.Navigate(textBox1.Text);
}
}
else if (textBox1.Text.Contains(".gov"))
{
if (!textBox1.Text.StartsWith("https://"))
{
textBox1.Text = "https://" + textBox1.Text;
webView21.CoreWebView2.Navigate(textBox1.Text);
}
else
{
webView21.CoreWebView2.Navigate(textBox1.Text);
}
}
else
{
webView21.CoreWebView2.Navigate(Properties.Settings.Default.Search + textBox1.Text);
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
webView21.CoreWebView2.Reload();
}
catch
{
}
}
private void button3_Click(object sender, EventArgs e)
{
try
{
webView21.CoreWebView2.Navigate(Properties.Settings.Default.Homepage);
}
catch
{
}
}
private void button5_Click(object sender, EventArgs e)
{
try
{
if (webView21.CoreWebView2.CanGoForward)
{
webView21.CoreWebView2.GoForward();
}
}
catch
{
}
}
private void button4_Click(object sender, EventArgs e)
{
try
{
if (webView21.CoreWebView2.CanGoBack)
{
webView21.CoreWebView2.GoBack();
}
}
catch
{
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Looks like you are setting the url to the result of a web message you are sending from javascript. Can you share what your script is doing? |
Beta Was this translation helpful? Give feedback.
-
What do you mean by that? |
Beta Was this translation helpful? Give feedback.
-
It looks like the url you are using to display in the address bar is being sent from script using a web message: webView21.CoreWebView2.WebMessageReceived += UpdateAddressBar;
...
private void UpdateAddressBar(object sender, CoreWebView2WebMessageReceivedEventArgs args)
{
String uri = args.TryGetWebMessageAsString();
textBox1.Text = uri; Can you share the script that's calling something like If you use the |
Beta Was this translation helpful? Give feedback.
-
2021-10-12.14-15-03.mp4
For some reason the webview2 displays about:srcdoc rather on CefSharp, it displays the full URL
Beta Was this translation helpful? Give feedback.
All reactions