// This file is part of RSS Guard. // // Copyright (C) 2011-2015 by Martin Rotter // // RSS Guard is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // RSS Guard is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with RSS Guard. If not, see . #include "network-web/webpage.h" #include "network-web/webbrowsernetworkaccessmanager.h" #include "network-web/webbrowser.h" #include "miscellaneous/application.h" #include "network-web/adblock/adblockmanager.h" #include #include #include QList WebPage::livingPages_; WebPage::WebPage(QObject *parent) : QWebPage(parent), loadProgress_(-1) { // Setup global network access manager. // NOTE: This makes network settings easy for all web browsers. setNetworkAccessManager(new WebBrowserNetworkAccessManager(this, this)); setForwardUnsupportedContent(true); connect(this, SIGNAL(unsupportedContent(QNetworkReply*)), this, SLOT(handleUnsupportedContent(QNetworkReply*))); connect(this, SIGNAL(loadProgress(int)), this, SLOT(progress(int))); connect(this, SIGNAL(loadFinished(bool)), this, SLOT(finished())); livingPages_.append(this); } WebPage::~WebPage() { livingPages_.removeOne(this); } bool WebPage::isLoading() const { return loadProgress_ < 100; } void WebPage::progress(int prog) { loadProgress_ = prog; } void WebPage::finished() { progress(100); // AdBlock cleanBlockedObjects(); } void WebPage::cleanBlockedObjects() { AdBlockManager* manager = AdBlockManager::instance(); if (!manager->isEnabled()) { return; } const QWebElement docElement = mainFrame()->documentElement(); foreach (const AdBlockedEntry &entry, adBlockedEntries_) { const QString urlString = entry.url.toString(); if (urlString.endsWith(QLatin1String(".js")) || urlString.endsWith(QLatin1String(".css"))) { continue; } QString urlEnd; int pos = urlString.lastIndexOf(QLatin1Char('/')); if (pos > 8) { urlEnd = urlString.mid(pos + 1); } if (urlString.endsWith(QLatin1Char('/'))) { urlEnd = urlString.left(urlString.size() - 1); } QString selector("img[src$=\"%1\"], iframe[src$=\"%1\"],embed[src$=\"%1\"]"); QWebElementCollection elements = docElement.findAll(selector.arg(urlEnd)); foreach (QWebElement element, elements) { QString src = element.attribute("src"); src.remove(QLatin1String("../")); if (urlString.contains(src)) { element.setStyleProperty("display", "none"); } } } // Apply domain-specific element hiding rules QString elementHiding = manager->elementHidingRulesForDomain(mainFrame()->url()); if (elementHiding.isEmpty()) { return; } elementHiding.append(QLatin1String("\n")); QWebElement bodyElement = docElement.findFirst("body"); bodyElement.appendInside("