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.

0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
1509
c0b5e693b0eb Updated copyright for 2012.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1112
diff changeset
3 # Copyright (c) 2009 - 2012 Detlev Offenbach <detlev@die-offenbachs.de>
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a class to apply AdBlock rules to a web page.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
1112
8a7d1b9d18db Improved code quality by getting rid of star imports. That way pyflakes can do its job. A few bugs fixed found by flakes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 945
diff changeset
10 from PyQt4.QtCore import QObject
381
64ae9e09d8bc Extended the Ad-Blocker of the web browser and added capability to configure the text encoding of the web browser (needs Qt >= 4.6).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 96
diff changeset
11
64ae9e09d8bc Extended the Ad-Blocker of the web browser and added capability to configure the text encoding of the web browser (needs Qt >= 4.6).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 96
diff changeset
12 import Helpviewer.HelpWindow
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 791
diff changeset
14
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 class AdBlockPage(QObject):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 Class to apply AdBlock rules to a web page.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 def __checkRule(self, rule, page, host):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 Private method to check, if a rule applies to the given web page and host.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 @param rule reference to the rule to check (AdBlockRule)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 @param page reference to the web page (QWebPage)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 @param host host name (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 """
1947
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
27 if not rule.isEnabled():
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
28 return
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
29
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
30 filter = rule.filter()
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
31 offset = filter.find("##")
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
32 if offset == -1:
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
33 return
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
34
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
35 selectorQuery = ""
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
36 if offset > 0:
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
37 domainRules = filter[:offset]
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
38 selectorQuery = filter[offset + 2:]
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
39 domains = domainRules.split(",")
381
64ae9e09d8bc Extended the Ad-Blocker of the web browser and added capability to configure the text encoding of the web browser (needs Qt >= 4.6).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 96
diff changeset
40
1947
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
41 match = False
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
42 for domain in domains:
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
43 reverse = domain[0] == '~'
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
44 if reverse:
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
45 xdomain = domain[1:]
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
46 if host.endswith(xdomain):
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
47 return
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
48 match = True
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
49 if host.endswith(domain):
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
50 match = True
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
51 if not match:
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
52 return
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
53
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
54 if offset == 0:
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
55 selectorQuery = filter[2:]
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
56
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
57 document = page.mainFrame().documentElement()
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
58 elements = document.findAll(selectorQuery)
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
59 for element in elements.toList():
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
60 element.setStyleProperty("visibility", "hidden")
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
61 element.removeFromDocument()
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 def applyRulesToPage(self, page):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 Public method to applay AdBlock rules to a web page.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 @param page reference to the web page (QWebPage)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 """
1947
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
69 if page is None or page.mainFrame() is None:
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
70 return
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
71
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
72 manager = Helpviewer.HelpWindow.HelpWindow.adblockManager()
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
73 if not manager.isEnabled():
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
74 return
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
75
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
76 host = page.mainFrame().url().host()
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
77 subscriptions = manager.subscriptions()
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
78 for subscription in subscriptions:
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
79 rules = subscription.pageRules()
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
80 for rule in rules:
84626f18f2c3 Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1509
diff changeset
81 self.__checkRule(rule, page, host)

eric ide

mercurial