Helpviewer/AdBlock/AdBlockPage.py

Fri, 11 Mar 2011 16:51:57 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 11 Mar 2011 16:51:57 +0100
changeset 945
8cd4d08fa9f6
parent 791
9ec2ac20e54e
child 1112
8a7d1b9d18db
permissions
-rw-r--r--

Made code mostly PEP 8 compliant (except all whitespace and line length).

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
791
9ec2ac20e54e Updated copyright notice.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 381
diff changeset
3 # Copyright (c) 2009 - 2011 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
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 from PyQt4.QtCore import *
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 from PyQt4 import QtWebKit
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
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
13 import Helpviewer.HelpWindow
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 791
diff changeset
15
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 class AdBlockPage(QObject):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 Class to apply AdBlock rules to a web page.
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 def __checkRule(self, rule, page, host):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 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
23
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 @param rule reference to the rule to check (AdBlockRule)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 @param page reference to the web page (QWebPage)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 @param host host name (string)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 """
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
28 if hasattr(QtWebKit, 'QWebElement'):
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
29 if not rule.isEnabled():
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
30 return
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
31
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
32 filter = rule.filter()
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
33 offset = filter.find("##")
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
34 if offset == -1:
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
35 return
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
36
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
37 selectorQuery = ""
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
38 if offset > 0:
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
39 domainRules = filter[:offset]
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 selectorQuery = filter[offset + 2:]
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
41 domains = domainRules.split(",")
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
42
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
43 match = False
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
44 for domain in domains:
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
45 reverse = domain[0] == '~'
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
46 if reverse:
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
47 xdomain = domain[1:]
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
48 if host.endswith(xdomain):
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
49 return
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
50 match = True
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
51 if host.endswith(domain):
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
52 match = True
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
53 if not match:
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
54 return
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
55
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
56 if offset == 0:
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
57 selectorQuery = filter[2:]
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
58
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
59 document = page.mainFrame().documentElement()
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
60 elements = document.findAll(selectorQuery)
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
61 for element in elements.toList():
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
62 element.setStyleProperty("visibility", "hidden")
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
63 element.removeFromDocument()
0
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 def applyRulesToPage(self, 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 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
68
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 @param page reference to the web page (QWebPage)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 """
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
71 if hasattr(QtWebKit, 'QWebElement'):
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
72 if page is None or page.mainFrame() is None:
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
73 return
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
74
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
75 manager = Helpviewer.HelpWindow.HelpWindow.adblockManager()
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
76 if not manager.isEnabled():
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
77 return
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
78
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
79 host = page.mainFrame().url().host()
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
80 subscriptions = manager.subscriptions()
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
81 for subscription in subscriptions:
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
82 rules = subscription.pageRules()
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
83 for rule in rules:
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
84 self.__checkRule(rule, page, host)

eric ide

mercurial