Anyone know how to change juce web browser component user agent header?

Is this possible, to change the user agent of the web browser component?

The ability to change the user agent of the WebBrowserComponent was added with commit 5423122 to the develop branch.

See the new WebBrowserComponent::Options::withUserAgent method.

It seems the webview2 browser options have broken since this update.
I’m using:

CmakeLists.txt

target_compile_definitions(${PLUGIN_TARGET_NAME}
        PRIVATE
       JUCE_USE_WIN_WEBVIEW2=1)

app.cpp

juce::WebBrowserComponent(juce::WebBrowserComponent::Options()
                                                         .withKeepPageLoadedWhenBrowserIsHidden()
                                                         .withBackend(
                                                                 juce::WebBrowserComponent::Options::Backend::webview2)
                                                         .withUserAgent(
                                                                 "...")
                                                         .withWinWebView2Options(
                                                                 juce::WebBrowserComponent::Options::WinWebView2()
                                                                         .withUserDataFolder(...)
                                                                         .withBuiltInErrorPageDisabled()
                                                                         .withBackgroundColour(PLUGIN_BACKGROUND_COLOUR)
                                                                         .withStatusBarDisabled()))

I seem to be getting the internet old explorer browser - rahter than webview2

1 Like

Hmmm works for me. I did the following:

  1. open the demorunner jucer file in the projucer file and enable JUCE_USE_WIN_WEBVIEW2 in the juce_gui_extra module configurations. Re-save.
  2. Copy&paste your code in the constructor of the WebBrowserDemo code:
DemoBrowserComponent (TextEditor& addressBox)
        : juce::WebBrowserComponent(juce::WebBrowserComponent::Options()
                                                         .withKeepPageLoadedWhenBrowserIsHidden()
                                                         .withBackend(
                                                                 juce::WebBrowserComponent::Options::Backend::webview2)
                                                         .withUserAgent(
                                                                 "...")
                                                         .withWinWebView2Options(
                                                                 juce::WebBrowserComponent::Options::WinWebView2()
                                                                         .withUserDataFolder(File("C:\\Users\\JUCE\\Documents\\Crazy"))
                                                                         .withBuiltInErrorPageDisabled()
                                                                         .withBackgroundColour(Colour(0xffff0000))
                                                                         .withStatusBarDisabled())),
        addressTextBox (addressBox)
    {}

Running this code works like a charm for me. Note, that if the webview2 encounters any error during construction (such as the user data folder not being found) it will fallback to internet explorer. You can check this with WebBrowserComponent::areOptionsSupported.

1 Like