Helpviewer/AdBlock/AdBlockPage.py

Sun, 04 Jul 2010 17:52:29 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 04 Jul 2010 17:52:29 +0200
changeset 381
64ae9e09d8bc
parent 96
9624a110667d
child 791
9ec2ac20e54e
permissions
-rw-r--r--

Extended the Ad-Blocker of the web browser and added capability to configure the text encoding of the web browser (needs Qt >= 4.6).

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

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

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

from PyQt4.QtCore import *
from PyQt4 import QtWebKit

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 hasattr(QtWebKit, 'QWebElement'):
            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 hasattr(QtWebKit, 'QWebElement'):
            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