Helpviewer/AdBlock/AdBlockPage.py

Sat, 14 Jul 2012 17:56:26 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 14 Jul 2012 17:56:26 +0200
changeset 1947
84626f18f2c3
parent 1509
c0b5e693b0eb
child 1960
d8c45fe8a1b9
permissions
-rw-r--r--

Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.

# -*- coding: utf-8 -*-

# Copyright (c) 2009 - 2012 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a class to apply AdBlock rules to a web page.
"""

from PyQt4.QtCore import QObject

import Helpviewer.HelpWindow


class AdBlockPage(QObject):
    """
    Class to apply AdBlock rules to a web page.
    """
    def __checkRule(self, rule, page, host):
        """
        Private method to check, if a rule applies to the given web page and host.
        
        @param rule reference to the rule to check (AdBlockRule)
        @param page reference to the web page (QWebPage)
        @param host host name (string)
        """
        if not rule.isEnabled():
            return
        
        filter = rule.filter()
        offset = filter.find("##")
        if offset == -1:
            return
        
        selectorQuery = ""
        if offset > 0:
            domainRules = filter[:offset]
            selectorQuery = filter[offset + 2:]
            domains = domainRules.split(",")
            
            match = False
            for domain in domains:
                reverse = domain[0] == '~'
                if reverse:
                    xdomain = domain[1:]
                    if host.endswith(xdomain):
                        return
                    match = True
                if host.endswith(domain):
                    match = True
            if not match:
                return
        
        if offset == 0:
            selectorQuery = filter[2:]
        
        document = page.mainFrame().documentElement()
        elements = document.findAll(selectorQuery)
        for element in elements.toList():
            element.setStyleProperty("visibility", "hidden")
            element.removeFromDocument()
    
    def applyRulesToPage(self, page):
        """
        Public method to applay AdBlock rules to a web page.
        
        @param page reference to the web page (QWebPage)
        """
        if page is None or page.mainFrame() is None:
            return
        
        manager = Helpviewer.HelpWindow.HelpWindow.adblockManager()
        if not manager.isEnabled():
            return
        
        host = page.mainFrame().url().host()
        subscriptions = manager.subscriptions()
        for subscription in subscriptions:
            rules = subscription.pageRules()
            for rule in rules:
                self.__checkRule(rule, page, host)

eric ide

mercurial