// This file is part of RSS Guard. // // Copyright (C) 2011-2016 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::s_livingPages; WebPage::WebPage(QObject *parent) : QWebPage(parent), m_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())); s_livingPages.append(this); } WebPage::~WebPage() { s_livingPages.removeOne(this); } bool WebPage::isLoading() const { return m_loadProgress < 100; } void WebPage::progress(int prog) { m_loadProgress = prog; } void WebPage::finished() { progress(100); cleanBlockedObjects(); } void WebPage::cleanBlockedObjects() { AdBlockManager *manager = AdBlockManager::instance(); if (!manager->isEnabled()) { return; } const QWebElement doc_element = mainFrame()->documentElement(); foreach (const AdBlockedEntry &entry, m_adBlockedEntries) { const QString url_string = entry.url.toString(); if (url_string.endsWith(QL1S(".js")) || url_string.endsWith(QL1S(".css"))) { continue; } QString url_end; int pos = url_string.lastIndexOf(QL1C('/')); if (pos > 8) { url_end = url_string.mid(pos + 1); } if (url_string.endsWith(QL1C('/'))) { url_end = url_string.left(url_string.size() - 1); } QString selector(QSL("img[src$=\"%1\"], iframe[src$=\"%1\"],embed[src$=\"%1\"]")); QWebElementCollection elements = doc_element.findAll(selector.arg(url_end)); foreach (QWebElement element, elements) { QString src = element.attribute(QSL("src")); src.remove(QL1S("../")); if (url_string.contains(src)) { element.setStyleProperty(QSL("display"), QSL("none")); } } } // Apply domain-specific element hiding rules QString element_hiding = manager->elementHidingRulesForDomain(mainFrame()->url()); if (element_hiding.isEmpty()) { return; } element_hiding.append(QL1S("\n")); QWebElement body_element = doc_element.findFirst(QSL("body")); body_element.appendInside(QSL("