Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. QWebKit Setting Up User Agent
Forum Update on Monday, May 27th 2025

QWebKit Setting Up User Agent

Scheduled Pinned Locked Moved Unsolved Qt WebKit
9 Posts 3 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    anshah
    wrote on last edited by anshah
    #1

    My application uses QWebKit on Ubuntu Linux. I need to setup a User Agent with a prefix and then add an emulated browser agent string.

    I figured out how to setup the User Agent by overloading QWebPage::userAgentForUrl. I have the User Agent prefix setup already but now I want to add the emulated browser agent string to this prefix.

    I found that in MAC using WebKit::WebView the emulated browser agent is setup by the following:

    WebView myWebView;
    ...
    NSString *uagnt = [myWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    

    I know the QWebKit uses WebKit as a backend so this must be possible in Qt WebKit. How can I go about doing this in QWebView / QWebPage?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      From the looks of it, you can create a QWebPage subclass and re-implement QWebPage::userAgentForUrl.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        anshah
        wrote on last edited by anshah
        #3

        @sgaist Thanks for the reply! I have already created a subclass and reimplemented QWebPage::userAgentForUrl so I got that part covered.

        The main question I had was what is the Qt equivalent of:

        WebView:stringByEvaluatingJavaScriptFromString
        

        In Qt is there a way I can extract the "navigator.userAgent" JavaScript as seen in this code example using WebView on MAC:

        WebView myWebView;
        ...
        NSString *uagnt = [myWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I may have misunderstood your problem.

          Can you detail a bit more what you want to do with that information and where you would like to extract it ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          0
          • SGaistS SGaist

            I may have misunderstood your problem.

            Can you detail a bit more what you want to do with that information and where you would like to extract it ?

            A Offline
            A Offline
            anshah
            wrote on last edited by anshah
            #5

            @sgaist Sorry I should have been more clear to begin with. Basically all I want to do is extract the following string in QtWebKit by evaluating JavaScript:

            navigator.userAgent
            

            I want to set up our user agent with a predefined prefix plus the JavaScript evaluated "navigator.userAgent".

            So I have already subclassed QWebPage::userAgentForUrl and have the prefix set. Now the last piece of the puzzle is adding "navigator.userAgent" by evaluating JavaScript.

            I need to do this for SAML authentication testing.

            As I illustrated before in MAC this is done by:

            WebView myWebView;
            ...
            NSString *uagnt = [myWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
            

            How can I achieve this in QtWebKit? Do I achieve this using QWebView, QWebPage, or QWebFrame?

            Hope I was clearer this time around.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              Konstantin Tokarev
              wrote on last edited by
              #6

              Evaluating navigator.userAgent returns whatever string is returned by QWebPage::userAgentForUrl. If you want to evaluate arbitrary JS expressions, use QWebFrame::evaluateJavaScript

              A 1 Reply Last reply
              0
              • K Konstantin Tokarev

                Evaluating navigator.userAgent returns whatever string is returned by QWebPage::userAgentForUrl. If you want to evaluate arbitrary JS expressions, use QWebFrame::evaluateJavaScript

                A Offline
                A Offline
                anshah
                wrote on last edited by
                #7

                @konstantin-tokarev
                @SGaist
                Is there a way to get the parent's userAgentForUrl value? Such as below?

                // User Agent Web Page
                class UserAgentWebPage : public QWebPage
                {
                    QString userAgentForUrl(const QUrl &url) const
                    {
                        // UserAgent: Prefix
                        QString userAgent = QWebPage::userAgentForUrl();
                
                        // Modify userAgent with custom prefixes
                
                        return userAgent;
                    }
                };
                

                My objective is to first obtain QtWebKit's User-Agent and then prefix that with my own custom fields.

                So let me summarize my questions:

                1. How do I obtain the default browser User-Agent string using QtWebKit?

                2. How can I call QWebPage::userAgentForUrl() from it's child?

                Any help would be most appreciated guys as I'm at a roadblock at the moment.

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  Konstantin Tokarev
                  wrote on last edited by
                  #8

                  Is there a way to get the parent's userAgentForUrl value? Such as below?

                  Yes it's possible

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    Konstantin Tokarev
                    wrote on last edited by
                    #9

                    However it might be enough for your purposes to set application name and version on your QApplication, this information will be displayed in UA string

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved