Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved QWebKit Setting Up User Agent

    Qt WebKit
    3
    9
    675
    Loading More Posts
    • 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
      anshah last edited by anshah

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • A
          anshah last edited by anshah

          @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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 0
            • A
              anshah @SGaist last edited by anshah

              @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 Reply Quote 0
              • K
                Konstantin Tokarev last edited by

                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 Reply Quote 0
                • A
                  anshah @Konstantin Tokarev last edited by

                  @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 Reply Quote 0
                  • K
                    Konstantin Tokarev last edited by

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

                    Yes it's possible

                    1 Reply Last reply Reply Quote 0
                    • K
                      Konstantin Tokarev last edited by

                      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 Reply Quote 0
                      • First post
                        Last post