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).

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
13
1af94a91f439 Changed copyright for 2010.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 0
diff changeset
3 # Copyright (c) 2009 - 2010 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
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 """
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
27 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
28 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
29 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
30
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 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
32 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
33 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
34 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
35
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 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
37 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
38 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
39 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
40 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
41
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 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
43 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
44 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
45 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
46 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
47 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
48 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
49 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
50 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
51 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
52 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
53 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
54
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 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
56 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
57
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 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
59 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
60 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
61 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
62 element.removeFromDocument()
0
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 def applyRulesToPage(self, page):
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 """
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 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
67
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 @param page reference to the web page (QWebPage)
de9c2efb9d02 Started porting eric4 to Python3
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 """
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
70 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
71 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
72 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
73
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 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
75 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
76 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
77
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 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
79 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
80 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
81 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
82 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
83 self.__checkRule(rule, page, host)

eric ide

mercurial