Webbrowser does not load css c#

Asked

Viewed 307 times

1

I tried to create a Simple browser and when I finished I realized that it does not carry css the site gets horrible looks like a website outdated. :\

Can someone help me?

Photos from the site, if the code is useful of this small browser I will be happy to give you.

inserir a descrição da imagem aqui

the following code is the browser code.

public Form1()
{
    InitializeComponent();
    webBrowser1.AllowWebBrowserDrop = false;
    webBrowser1.ScriptErrorsSuppressed = true;
    webBrowser1.WebBrowserShortcutsEnabled = false;
    webBrowser1.IsWebBrowserContextMenuEnabled = false;
}

private void url_KeyPress(object sender, KeyPressEventArgs e)
{
    bool link = true;
    bool dom = true;
    if (e.KeyChar == (char)13)
    {
        for(int i =0;i<url.Text.Length;i++)
        {
            if(url.Text[i] == ' ' )
            {
                link = false;
            }

        }
        if(link == true)
        {
            for (int i = 0; i < url.Text.Length-1; i++)
            {
                if (url.Text[i] == '.')
                {
                    dom = false;
                }

            }
            if(dom == false)
            {
            }
            else
            {
                url.Text = "https://www.google.com/search?client=firefox-b-d&q=" + url.Text.Replace(" ", "+");
            }
        }
        else
        {
            url.Text = "https://www.google.com/search?client=firefox-b-d&q=" + url.Text.Replace(" ", "+");
        }
        webBrowser1.Navigate(url.Text);
    }
}

private void F5_Click(object sender, EventArgs e)
{
    webBrowser1.Refresh();
}

private void begin_Click(object sender, EventArgs e)
{
    webBrowser1.GoForward();
}

private void back_Click(object sender, EventArgs e)
{
    webBrowser1.GoBack();
}

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    url.Text = Convert.ToString(webBrowser1.Url);
}
  • Hello Gonçalo, could you show a little code?

  • 1

    of course I will already update the question

1 answer

3


CSS is working yes, what happens is that simply Google is carrying a "lighter" and "older" version of your site, this is because either it does not recognize your browser as a modern browser, or thinks it is Internet Explorer.

The component WebBrowser (https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.webbrowser?view=netframework-4.8) will use the functionality of the user’s operating system, through Activex, in case it depends on the version of the IE that the customer has installed, will be the support that will have your component, so if you are going to load a website designed with support for old browsers, will work perfectly, otherwise it is impossible.

If you want a better engine/engine, like the same one used in Google Chrome, called Chromium, you can try this project:

If using Winforms, install via Nuget:


An example from Cefsharp (https://ourcodeworld.com/articles/read/173/how-to-use-cefsharp-chromium-embedded-framework-csharp-in-a-winforms-application):

public partial class Form1 : Form
{
    public ChromiumWebBrowser chromeBrowser;

    public Form1()
    {
        InitializeComponent();

        // deve ser chamado depois de InitializeComponent
        InitializeChromium();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    public void InitializeChromium()
    {
        CefSettings settings = new CefSettings();

        // Inicia as configurações que você forneceu
        Cef.Initialize(settings);

        // Cria o componente
        chromeBrowser = new ChromiumWebBrowser("https://www.google.com");

        // Adiciona para o form (ajuste aqui)
        this.Controls.Add(chromeBrowser);
        chromeBrowser.Dock = DockStyle.Fill;
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        //Finaliza "quando fechar"
        Cef.Shutdown();
    }        
}
  • 1

    Use the Webview of Microsoft Edge is also interesting.

  • 1

    Good tip @Cypherpotato, unfortunately it is limited to Windows10, so this limit to the operating system can be a little uncomfortable, given that there are people who use even Win7, at home and in companies, but the tip is still great ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.