1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-19 10:16:34 +01:00

AdBlock: Better and faster hiding placeholders of blocked elements

- also Ctrl+C in AdBlock tree now copies url filter into clipboard
This commit is contained in:
nowrep 2012-07-04 10:08:55 +02:00
parent 2b7b28db37
commit 138ffdfe93
7 changed files with 67 additions and 40 deletions

View File

@ -1,6 +1,9 @@
Version 1.3.0
* not released yet
* added animated tab previews
* new Ukrainian translation
* can now open .xhtml files from open file dialog
* added animated tab previews with option to turn animations off
* possibility to change icon of bookmarks
* ssl manager now can import own certificate
* clear recent history now remembers last checked options
* new urlbar completion widget can now show also entries from bookmarks
@ -8,13 +11,18 @@ Version 1.3.0
* support for 3rd party subscriptions in AdBlock
* improved performance of AdBlock rules matching
* possibility to add subscriptions with loading abp: links
* private browsing is now opened in new window and new process
* improved AdBlock dialog distinguish rule types with colors
* popup windows now have loading animation in urlbar
* new gif for loading animation (spinner)
* possibility to add RSS feed into external reader
* option to specify preferred behaviour when opening new tab
* inverting preferred new tab behaviour with shift modifier (eg. shift+middle click on link)
* X11: middle clicking on add tab button will open new tab with global mouse selection's contents
* better support for Content-Disposition header (downloads)
* Linux: middle clicking on add tab button will open new tab with global mouse selection's contents
* Linux: generating backtrace and saving it into file upon application crash
* fixed saving passwords on some sites (parsing WebKit's data format)
* fixed "go to web address" action when newlines were in string
* fixed excessive ssl warnings when rejecting untrusted certificate
* fixed dragging the whole text from some labels
* fixed handling special characters when searching with shortcuts in urlbar

View File

@ -79,8 +79,7 @@ QNetworkReply* AdBlockManager::block(const QNetworkRequest &request)
const QString &urlDomain = request.url().host();
const QString &urlScheme = request.url().scheme();
if (!isEnabled() || urlScheme == "data" || urlScheme == "qrc" ||
urlScheme == "file" || urlScheme == "qupzilla" || urlScheme == "abp") {
if (!isEnabled() || !canRunOnScheme(urlScheme)) {
return 0;
}
@ -265,6 +264,11 @@ bool AdBlockManager::isEnabled()
return m_enabled;
}
bool AdBlockManager::canRunOnScheme(const QString &scheme) const
{
return !(scheme == "file" || scheme == "qrc" || scheme == "qupzilla" || scheme == "data" || scheme == "abp");
}
QString AdBlockManager::elementHidingRules() const
{
QString rules;

View File

@ -43,6 +43,7 @@ public:
void save();
bool isEnabled();
bool canRunOnScheme(const QString &scheme) const;
QString elementHidingRules() const;
QString elementHidingRulesForDomain(const QString &domain) const;

View File

@ -362,7 +362,7 @@ void AdBlockRule::parseFilter()
parseDomains(option.mid(7), '|');
++handledOptions;
}
else if (option.endsWith("match-case")) {
else if (option == "match-case") {
m_caseSensitivity = Qt::CaseSensitive;
++handledOptions;
}
@ -386,6 +386,10 @@ void AdBlockRule::parseFilter()
m_xmlhttprequestException = option.startsWith('~');
++handledOptions;
}
else if (option == "collapse") {
// Hiding placeholders of blocked elements
++handledOptions;
}
}
// If we don't handle all options, it's safer to just disable this rule

View File

@ -21,6 +21,8 @@
#include <QMenu>
#include <QTimer>
#include <QKeyEvent>
#include <QClipboard>
#include <QApplication>
#include <QInputDialog>
AdBlockTreeWidget::AdBlockTreeWidget(AdBlockSubscription* subscription, QWidget* parent)
@ -120,6 +122,16 @@ void AdBlockTreeWidget::itemChanged(QTreeWidgetItem* item)
m_itemChangingBlock = false;
}
void AdBlockTreeWidget::copyFilter()
{
QTreeWidgetItem* item = currentItem();
if (!item) {
return;
}
QApplication::clipboard()->setText(item->text(0));
}
void AdBlockTreeWidget::addRule()
{
if (!m_subscription->canEditRules()) {
@ -203,6 +215,10 @@ void AdBlockTreeWidget::adjustItemFeatures(QTreeWidgetItem* item, const AdBlockR
void AdBlockTreeWidget::keyPressEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_C && event->modifiers() & Qt::ControlModifier) {
copyFilter();
}
if (event->key() == Qt::Key_Delete) {
removeRule();
}

View File

@ -43,6 +43,7 @@ public slots:
private slots:
void contextMenuRequested(const QPoint &pos);
void itemChanged(QTreeWidgetItem* item);
void copyFilter();
void refresh();
void subscriptionUpdated();

View File

@ -507,54 +507,47 @@ void WebPage::addAdBlockRule(const AdBlockRule* rule, const QUrl &url)
void WebPage::cleanBlockedObjects()
{
if (!AdBlockManager::instance()->isEnabled()) {
return;
}
// Don't run on local schemes
AdBlockManager* manager = AdBlockManager::instance();
const QString &urlScheme = url().scheme();
if (urlScheme == "data" || urlScheme == "qrc" || urlScheme == "file" ||
urlScheme == "qupzilla" || urlScheme == "abp") {
if (!manager->isEnabled() || !manager->canRunOnScheme(urlScheme)) {
return;
}
QStringList findingStrings;
foreach(const AdBlockedEntry & entry, m_adBlockedEntries) {
if (entry.url.toString().endsWith(".js")) {
continue;
}
findingStrings.append(entry.url.toString());
const QUrl &mainFrameUrl = url();
if (entry.url.scheme() == mainFrameUrl.scheme() && entry.url.host() == mainFrameUrl.host()) {
//May be relative url
QString relativeUrl = qz_makeRelativeUrl(mainFrameUrl, entry.url).toString();
findingStrings.append(relativeUrl);
if (relativeUrl.startsWith('/')) {
findingStrings.append(relativeUrl.right(relativeUrl.size() - 1));
}
}
}
const QWebElement &docElement = mainFrame()->documentElement();
QWebElementCollection elements;
foreach(const QString & s, findingStrings) {
elements.append(docElement.findAll("*[src=\"" + s + "\"]"));
}
foreach(const AdBlockedEntry & entry, m_adBlockedEntries) {
const QString &urlString = entry.url.toString();
if (urlString.endsWith(".js") || urlString.endsWith(".css")) {
continue;
}
foreach(QWebElement element, elements) {
element.setStyleProperty("visibility", "hidden");
int pos = urlString.lastIndexOf('/');
if (pos < 0 || urlString.endsWith('/')) {
continue;
}
QString urlEnd = urlString.mid(pos + 1);
QString selector("img[src$=\"" + urlEnd + "\"], iframe[src$=\"" + urlEnd + "\"],"
"embed[src$=\"" + urlEnd + "\"]");
QWebElementCollection elements = docElement.findAll(selector);
foreach(QWebElement element, elements) {
QString src = element.attribute("src");
src.remove("../");
if (urlString.endsWith(src)) {
element.setStyleProperty("visibility", "hidden");
}
}
}
// Apply domain-specific element hiding rules
QString elementHiding = AdBlockManager::instance()->elementHidingRulesForDomain(url().host());
elementHiding.append("{display: none !important;}\n</style>");
QWebElement headElement = docElement.findFirst("body");
headElement.appendInside("<style type=\"text/css\">\n/* AdBlock for QupZilla */\n" + elementHiding);
QWebElement bodyElement = docElement.findFirst("body");
bodyElement.appendInside("<style type=\"text/css\">\n/* AdBlock for QupZilla */\n" + elementHiding);
}
QString WebPage::userAgentForUrl(const QUrl &url) const