Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get CefSharp to hide both the vertical and horizontal scrollbars when a webpage is loaded?

Tags:

c#

wpf

cefsharp

I'm using CefSharp 57 in a C# WPF application.
I want to hide both the vertical and horizontal scrollbars on any given webpage.

I've been unable to find a way to do this directly with CefSharp.
It doesn't seem to have a property similar to ScrollBarsEnabled that I'm used to with the native WinForms browser.

I thought I might be able to inject some CSS/JS after the page has rendered to set the body overflow to hidden but this has no effect.

private void OnBrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs args)
{
    if (args.Frame.IsMain)
    {
        // Shows an alert after page is loaded so it definitely works
        args
            .Browser
            .MainFrame
            .ExecuteJavaScriptAsync("alert('HELLO!');

        // Scrollbars are still visible after this fires
        args
            .Browser
            .MainFrame
            .ExecuteJavaScriptAsync(
            "(function() { document.body.style.overflow = 'hidden'; });");
    }
}

I looked to see if there was a way to draw the scrollbars myself and make them transparent but I was unable to find a way to do this.

Is there a way to hide the vertical and horizontal scrollbars on any given page using CefSharp?

like image 700
Equalsk Avatar asked Sep 19 '25 12:09

Equalsk


1 Answers

The code I originally showed is correct, I just needed to simplify the script I supplied:

WebBrowser.FrameLoadEnd += OnBrowserFrameLoadEnd;

private void OnBrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs args)
{
    if (args.Frame.IsMain)
    {
        args
            .Browser
            .MainFrame
            .ExecuteJavaScriptAsync(
            "document.body.style.overflow = 'hidden'");
    }
}

The scrollbars are visible for just a second before disappearing which suits my purposes.

like image 166
Equalsk Avatar answered Sep 22 '25 10:09

Equalsk