1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

AdBlock: Fixed $third-party option when Referer header is empty

- should fix a lot of false positives with $third-party rules

see #444
This commit is contained in:
nowrep 2012-07-13 11:04:14 +02:00
parent b0e741447d
commit 14e0040a9f
11 changed files with 51 additions and 16 deletions

View File

@ -302,7 +302,8 @@ bool AdBlockRule::matchDomain(const QString &domain) const
bool AdBlockRule::matchThirdParty(const QNetworkRequest &request) const bool AdBlockRule::matchThirdParty(const QNetworkRequest &request) const
{ {
const QString &referer = request.rawHeader("Referer"); const QString &referer = request.attribute(QNetworkRequest::Attribute(QNetworkRequest::User + 151), QString()).toString();
if (referer.isEmpty()) { if (referer.isEmpty()) {
return false; return false;
} }

View File

@ -193,7 +193,10 @@ void QupZilla::postLaunch()
} }
if (addTab) { if (addTab) {
m_tabWidget->addView(startUrl, Qz::NT_CleanSelectedTabAtTheEnd); QNetworkRequest request(startUrl);
request.setRawHeader("X-QupZilla-UserLoadAction", QByteArray("1"));
m_tabWidget->addView(request, Qz::NT_CleanSelectedTabAtTheEnd);
if (startUrl.isEmpty() || startUrl.toString() == "qupzilla:speeddial") { if (startUrl.isEmpty() || startUrl.toString() == "qupzilla:speeddial") {
locationBar()->setFocus(); locationBar()->setFocus();
@ -202,7 +205,10 @@ void QupZilla::postLaunch()
if (m_tabWidget->getTabBar()->normalTabsCount() <= 0 && m_startBehaviour != Qz::BW_OtherRestoredWindow) { if (m_tabWidget->getTabBar()->normalTabsCount() <= 0 && m_startBehaviour != Qz::BW_OtherRestoredWindow) {
//Something went really wrong .. add one tab //Something went really wrong .. add one tab
m_tabWidget->addView(m_homepage, Qz::NT_SelectedTabAtTheEnd); QNetworkRequest request(m_homepage);
request.setRawHeader("X-QupZilla-UserLoadAction", QByteArray("1"));
m_tabWidget->addView(request, Qz::NT_SelectedTabAtTheEnd);
} }
aboutToHideEditMenu(); aboutToHideEditMenu();

View File

@ -361,6 +361,14 @@ QNetworkReply* NetworkManager::createRequest(QNetworkAccessManager::Operation op
return reply; return reply;
} }
if (req.rawHeader("X-QupZilla-UserLoadAction") == QByteArray("1")) {
req.setRawHeader("X-QupZilla-UserLoadAction", QByteArray());
req.setAttribute(QNetworkRequest::Attribute(QNetworkRequest::User + 151), QString());
}
else {
req.setAttribute(QNetworkRequest::Attribute(QNetworkRequest::User + 151), req.rawHeader("Referer"));
}
if (m_doNotTrack) { if (m_doNotTrack) {
req.setRawHeader("DNT", QByteArray("1")); req.setRawHeader("DNT", QByteArray("1"));
} }
@ -372,9 +380,9 @@ QNetworkReply* NetworkManager::createRequest(QNetworkAccessManager::Operation op
req.setRawHeader("Accept-Language", m_acceptLanguage); req.setRawHeader("Accept-Language", m_acceptLanguage);
req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
if (req.attribute(QNetworkRequest::CacheLoadControlAttribute).toInt() == QNetworkRequest::PreferNetwork) { // if (req.attribute(QNetworkRequest::CacheLoadControlAttribute).toInt() == QNetworkRequest::PreferNetwork) {
req.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); // req.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
} // }
// Adblock // Adblock
if (op == QNetworkAccessManager::GetOperation) { if (op == QNetworkAccessManager::GetOperation) {

View File

@ -67,6 +67,7 @@ void PopupWebView::openUrlInNewTab(const QUrl &urla, Qz::NewTabPositionFlag posi
if (window) { if (window) {
QNetworkRequest req(urla); QNetworkRequest req(urla);
req.setRawHeader("Referer", url().toEncoded()); req.setRawHeader("Referer", url().toEncoded());
req.setRawHeader("X-QupZilla-UserLoadAction", QByteArray("1"));
window->tabWidget()->addView(req, Qz::NT_SelectedTab); window->tabWidget()->addView(req, Qz::NT_SelectedTab);
window->raise(); window->raise();

View File

@ -138,6 +138,14 @@ void TabbedWebView::loadProgress(int prog)
} }
} }
void TabbedWebView::userLoadAction(const QUrl &url)
{
QNetworkRequest request(url);
request.setRawHeader("X-QupZilla-UserLoadAction", QByteArray("1"));
load(request);
}
void TabbedWebView::slotLoadStarted() void TabbedWebView::slotLoadStarted()
{ {
m_rssChecked = false; m_rssChecked = false;
@ -280,6 +288,7 @@ void TabbedWebView::openUrlInNewTab(const QUrl &urla, Qz::NewTabPositionFlag pos
{ {
QNetworkRequest req(urla); QNetworkRequest req(urla);
req.setRawHeader("Referer", url().toEncoded()); req.setRawHeader("Referer", url().toEncoded());
req.setRawHeader("X-QupZilla-UserLoadAction", QByteArray("1"));
m_tabWidget->addView(req, position); m_tabWidget->addView(req, position);
} }

View File

@ -65,6 +65,8 @@ public slots:
void slotLoadStarted(); void slotLoadStarted();
void loadProgress(int prog); void loadProgress(int prog);
void userLoadAction(const QUrl &url);
void closeView(); void closeView();
void openUrlInNewTab(const QUrl &urla, Qz::NewTabPositionFlag position); void openUrlInNewTab(const QUrl &urla, Qz::NewTabPositionFlag position);
void openNewTab(); void openNewTab();

View File

@ -314,7 +314,6 @@ int TabWidget::addView(QNetworkRequest req, const QString &title, const Qz::NewT
connect(webView, SIGNAL(ipChanged(QString)), p_QupZilla->ipLabel(), SLOT(setText(QString))); connect(webView, SIGNAL(ipChanged(QString)), p_QupZilla->ipLabel(), SLOT(setText(QString)));
if (url.isValid()) { if (url.isValid()) {
req.setUrl(url);
webView->load(req); webView->load(req);
} }
@ -553,6 +552,7 @@ int TabWidget::duplicateTab(int index)
QNetworkRequest req(url); QNetworkRequest req(url);
req.setRawHeader("Referer", url.toEncoded()); req.setRawHeader("Referer", url.toEncoded());
req.setRawHeader("X-QupZilla-UserLoadAction", QByteArray("1"));
int id = addView(req, title, Qz::NT_CleanNotSelectedTab); int id = addView(req, title, Qz::NT_CleanNotSelectedTab);
weTab(id)->setHistoryData(history); weTab(id)->setHistoryData(history);

View File

@ -453,6 +453,8 @@ QSslCertificate WebPage::sslCertificate()
bool WebPage::acceptNavigationRequest(QWebFrame* frame, const QNetworkRequest &request, NavigationType type) bool WebPage::acceptNavigationRequest(QWebFrame* frame, const QNetworkRequest &request, NavigationType type)
{ {
m_lastRequestType = type; m_lastRequestType = type;
m_lastRequestUrl = request.url();
const QString &scheme = request.url().scheme(); const QString &scheme = request.url().scheme();
if (scheme == "mailto" || scheme == "ftp") { if (scheme == "mailto" || scheme == "ftp") {
@ -480,7 +482,13 @@ void WebPage::populateNetworkRequest(QNetworkRequest &request)
QVariant variant = qVariantFromValue((void*) pagePointer); QVariant variant = qVariantFromValue((void*) pagePointer);
request.setAttribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 100), variant); request.setAttribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 100), variant);
request.setAttribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 101), m_lastRequestType);
if (m_lastRequestUrl == request.url()) {
request.setAttribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 101), m_lastRequestType);
if (m_lastRequestType == NavigationTypeLinkClicked) {
request.setRawHeader("X-QupZilla-UserLoadAction", QByteArray("1"));
}
}
} }
QWebPage* WebPage::createWindow(QWebPage::WebWindowType type) QWebPage* WebPage::createWindow(QWebPage::WebWindowType type)

View File

@ -124,15 +124,17 @@ private:
QupZilla* p_QupZilla; QupZilla* p_QupZilla;
NetworkManagerProxy* m_networkProxy; NetworkManagerProxy* m_networkProxy;
QWebPage::NavigationType m_lastRequestType;
TabbedWebView* m_view; TabbedWebView* m_view;
SpeedDial* m_speedDial; SpeedDial* m_speedDial;
QFileSystemWatcher* m_fileWatcher;
QEventLoop* m_runningLoop;
QSslCertificate m_sslCert; QSslCertificate m_sslCert;
QList<QSslCertificate> m_rejectedSslCerts; QList<QSslCertificate> m_rejectedSslCerts;
QList<AdBlockedEntry> m_adBlockedEntries; QList<AdBlockedEntry> m_adBlockedEntries;
QFileSystemWatcher* m_fileWatcher;
QEventLoop* m_runningLoop; QWebPage::NavigationType m_lastRequestType;
QUrl m_lastRequestUrl;
int m_loadProgress; int m_loadProgress;
bool m_blockAlerts; bool m_blockAlerts;

View File

@ -93,7 +93,7 @@ WebTab::WebTab(QupZilla* mainClass, LocationBar* locationBar)
connect(m_view, SIGNAL(urlChanged(QUrl)), m_locationBar.data(), SLOT(showUrl(QUrl))); connect(m_view, SIGNAL(urlChanged(QUrl)), m_locationBar.data(), SLOT(showUrl(QUrl)));
connect(m_view, SIGNAL(rssChanged(bool)), m_locationBar.data(), SLOT(showRSSIcon(bool))); connect(m_view, SIGNAL(rssChanged(bool)), m_locationBar.data(), SLOT(showRSSIcon(bool)));
connect(m_view, SIGNAL(privacyChanged(bool)), m_locationBar.data(), SLOT(setPrivacy(bool))); connect(m_view, SIGNAL(privacyChanged(bool)), m_locationBar.data(), SLOT(setPrivacy(bool)));
connect(m_locationBar.data(), SIGNAL(loadUrl(QUrl)), m_view, SLOT(load(QUrl))); connect(m_locationBar.data(), SIGNAL(loadUrl(QUrl)), m_view, SLOT(userLoadAction(QUrl)));
} }
TabbedWebView* WebTab::view() const TabbedWebView* WebTab::view() const

View File

@ -38,7 +38,7 @@ PluginSpec GM_Plugin::pluginSpec()
spec.name = "GreaseMonkey"; spec.name = "GreaseMonkey";
spec.info = "Userscripts for QupZilla"; spec.info = "Userscripts for QupZilla";
spec.description = "Provides support for userscripts (www.userscripts.org)"; spec.description = "Provides support for userscripts (www.userscripts.org)";
spec.version = "0.1.1"; spec.version = "0.2.1";
spec.author = "David Rosca <nowrep@gmail.com>"; spec.author = "David Rosca <nowrep@gmail.com>";
spec.icon = QPixmap(":gm/data/icon.png"); spec.icon = QPixmap(":gm/data/icon.png");
spec.hasSettings = true; spec.hasSettings = true;
@ -81,9 +81,7 @@ QNetworkReply* GM_Plugin::createRequest(QNetworkAccessManager::Operation op, con
{ {
Q_UNUSED(outgoingData) Q_UNUSED(outgoingData)
int type = request.attribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 101), -1).toInt(); if (op == QNetworkAccessManager::GetOperation && request.rawHeader("X-QupZilla-UserLoadAction") == QByteArray("1")) {
if (op == QNetworkAccessManager::GetOperation && QWebPage::NavigationType(type) == QWebPage::NavigationTypeLinkClicked) {
const QString &urlString = request.url().toString(QUrl::RemoveFragment | QUrl::RemoveQuery); const QString &urlString = request.url().toString(QUrl::RemoveFragment | QUrl::RemoveQuery);
if (urlString.endsWith(".user.js")) { if (urlString.endsWith(".user.js")) {