Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-66656

Turning JavaScript on in acceptNavigationRequest doesn't always work

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3: Somewhat important
    • Resolution: Done
    • Affects Version/s: 5.10.1
    • Fix Version/s: 5.11.1
    • Component/s: WebEngine
    • Labels:
      None
    • Environment:
      Archlinux
    • Commits:
      bab4ababd097fd90b227a5a1bca63343b97af091

      Description

      I'd expect it to be possible to turn off the JavaScriptEnabled setting and then turn it on selectively in acceptNavigationRequest. This works with QtWebKit, but with QtWebEngine, another refresh is needed until the page loads properly (with the example of Travis CI). Example:

      #include <QApplication>
      #include <QWebEngineView>
      #include <QWebEnginePage>
      #include <QWebEngineSettings>
      #include <QUrl>
      #include <QTimer>
      #include <QDebug>
      
      class WebPage : public QWebEnginePage
      {
          bool acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame);
      };
      
      bool WebPage::acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame)
      {
          qDebug() << url;
          if (isMainFrame && url.toString().contains("travis-ci")) {
              settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
          }
          return true;
      }
      
      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
          QWebEngineView view{};
          WebPage page{};
          view.setPage(&page);
          QWebEngineSettings::globalSettings()->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
      
          view.load(QUrl("https://www.example.com/"));
          view.show();
      
          QTimer::singleShot(2000, [&](){ view.load(QUrl("https://www.travis-ci.com/")); });
          QTimer::singleShot(4000, [&](){ view.reload(); });
      
          return app.exec();
      }
      

        Attachments

          Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            Activity

              People

              Assignee:
              pvarga Peter Varga
              Reporter:
              the compiler Florian Bruhin
              Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:

                  Gerrit Reviews

                  There are no open Gerrit changes