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

- 41,292
- 40
- 236
- 366
3 Answers
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"

- 41,292
- 40
- 236
- 366
-
Is there a way to check not by extension but by content type? – Zelid Mar 25 '11 at 14:14
-
1@Zelid Yes. See `QNetworkRequest::header()` and `QNetworkReply::header()` – Piotr Dobrogost Mar 11 '12 at 18:46
-
Since Qt droped webkit and now uses chromium, QtWebEngine and QtWebView do not interact with QNetworkAccessManager any more: https://doc.qt.io/qt-5/qtwebenginewidgets-qtwebkitportingguide.html#qt-webengine-does-not-interact-with-qnetworkaccessmanager – cydoc Apr 24 '18 at 06:17
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.

- 460
- 3
- 14
-
1You 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. – Piotr Dobrogost Mar 23 '14 at 19:15
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.

- 5,722
- 1
- 32
- 26
-
2This is wrong for the same reason hmuelner's answer is wrong. From the [docs](http://developer.qt.nokia.com/doc/qt-4.8/qwebpage.html#acceptNavigationRequest) *This function is called whenever WebKit requests to **navigate** frame to the resource specified by request by means of the specified navigation type type.* – Piotr Dobrogost Jan 31 '12 at 12:14