How to tell QWebPage not to load specific type of resources?
Asked Answered
P

3

17

How to tell QWebPage not to load specific type of resources like js, css or png?

Piscine answered 1/1, 2011 at 18:54 Comment(0)
P
31

The solution is to extend QNetworkAccessManager class and override it's virtual method QNetworkAccessManager::createRequest In our implementation we check the path of the requested url and if it's the one we don't want to download we create and hand over an empty request instead of the real one. Below is a complete, working example.

#include <QApplication>
#include <QUrl>

#include <QtWebKit/QWebPage>
#include <QtWebKit/QWebFrame>

#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QDebug>


class NAM : public QNetworkAccessManager {

    Q_OBJECT

protected:

    virtual QNetworkReply * createRequest(Operation op,
                                          const QNetworkRequest & req,
                                          QIODevice * outgoingData = 0) {

        if (req.url().path().endsWith("css")) {
            qDebug() << "skipping " << req.url();
            return QNetworkAccessManager::createRequest(QNetworkAccessManager::GetOperation,
                                                        QNetworkRequest(QUrl()));
        } else {
            return QNetworkAccessManager::createRequest(op, req, outgoingData);
        }
    }
};


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QWebPage page;
    NAM nam;

    page.setNetworkAccessManager(&nam);
    page.mainFrame()->load(QUrl("http://google.com"));

    app.exec();
}

#include "main.moc"
Piscine answered 3/1, 2011 at 20:32 Comment(3)
Is there a way to check not by extension but by content type?Jdavie
@Jdavie Yes. See QNetworkRequest::header() and QNetworkReply::header()Piscine
Since Qt droped webkit and now uses chromium, QtWebEngine and QtWebView do not interact with QNetworkAccessManager any more: doc.qt.io/qt-5/…Florance
H
0

I am actually struggling with the same problem, Piotr solution is assuming urls with file extensions, unfortunately this is not always the case.

it is possible toe get mime-type but only after we get the response' and this is offcore to late.

we tried to get the element context requesting the resources, say if it is an <img> element or <link> to get CSS, but req.originatingObject() only gives us a QWebFrame. i know for example that this was possible in mozilla code.

BTW, turning off javascript and auto load images will prevent loading of images and scripts.

Haplosis answered 23/3, 2014 at 10:39 Comment(1)
You could try making a HEAD request just to get headers so that you can check Content-Type header and make decision based on its value.Piscine
L
-1

If your goal is to prevent the Webpage from changing, you can take a look at

virtual bool acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type);

in QWebPage. You can inspect the request and return false if you want to prevent the request from being sent.

Lopeared answered 30/1, 2012 at 16:11 Comment(1)
This is wrong for the same reason hmuelner's answer is wrong. From the docs This function is called whenever WebKit requests to navigate frame to the resource specified by request by means of the specified navigation type type.Piscine

© 2022 - 2024 — McMap. All rights reserved.