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

Turning JavaScript on in acceptNavigationRequest doesn't always work

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P3: Somewhat important
    • 5.11.1
    • 5.10.1
    • WebEngine
    • None
    • Archlinux
    • 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

              pvarga Peter Varga
              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