WebBrowser/AdBlock/AdBlockPage.py

Sat, 12 Mar 2016 20:05:01 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 12 Mar 2016 20:05:01 +0100
branch
QtWebEngine
changeset 4847
a1a8eac81b54
parent 4631
Helpviewer/AdBlock/AdBlockPage.py@5c1a96925da4
child 4860
0a44aff88bfa
permissions
-rw-r--r--

Continued porting the web browser.

- started porting the AdBlock code

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

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

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

from __future__ import unicode_literals

from PyQt5.QtCore import QObject, QUrl

from ..Tools import Scripts


class AdBlockPage(QObject):
    """
    Class to apply AdBlock rules to a web page.
    """
    def hideBlockedPageEntries(self, page):
        """
        Public method to apply AdBlock rules to a web page.
        
        @param page reference to the web page (HelpWebPage)
        """
        if page is None:
            return
        
        from WebBrowser.WebBrowserWindow import WebBrowserWindow
        manager = WebBrowserWindow.adBlockManager()
        if not manager.isEnabled():
            return
        
        # apply domain specific element hiding rules
        elementHiding = manager.elementHidingRulesForDomain(page.url())
        if elementHiding:
            script = Scripts.setCss(elementHiding)
            page.runJavaScript(script)
##        docElement = page.mainFrame().documentElement()
##        
##        for entry in page.getAdBlockedPageEntries():
##            urlString = entry.urlString()
##            if urlString.endswith((".js", ".css")):
##                continue
##            
##            urlEnd = ""
##            pos = urlString.rfind("/")
##            if pos >= 0:
##                urlEnd = urlString[pos + 1:]
##            if urlString.endswith("/"):
##                urlEnd = urlString[:-1]
##            
##            selector = \
##                'img[src$="{0}"], iframe[src$="{0}"], embed[src$="{0}"]'\
##                .format(urlEnd)
##            elements = docElement.findAll(selector)
##            
##            for element in elements:
##                src = element.attribute("src")
##                src = src.replace("../", "")
##                if src in urlString:
##                    element.setStyleProperty("display", "none")
##        
##        
##        elementHiding += "{display: none !important;}\n</style>"
##        
##        bodyElement = docElement.findFirst("body")
##        bodyElement.appendInside(
##            '<style type="text/css">\n/* AdBlock for eric */\n' +
##            elementHiding)
##
##
##class AdBlockedPageEntry(object):
##    """
##    Class implementing a data structure for web page rules.
##    """
##    def __init__(self, rule, url):
##        """
##        Constructor
##        
##        @param rule AdBlock rule to add (AdBlockRule)
##        @param url URL that matched the rule (QUrl)
##        """
##        self.rule = rule
##        self.url = QUrl(url)
##    
##    def __eq__(self, other):
##        """
##        Special method to test equality.
##        
##        @param other reference to the other entry (AdBlockedPageEntry)
##        @return flag indicating equality (boolean)
##        """
##        return self.rule == other.rule and self.url == other.url
##    
##    def urlString(self):
##        """
##        Public method to get the URL as a string.
##        
##        @return URL as a string (string)
##        """
##        return self.url.toString()

eric ide

mercurial