eric6/WebBrowser/AdBlock/AdBlockMatcher.py

Sat, 27 Feb 2021 12:08:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Feb 2021 12:08:23 +0100
changeset 8138
169e65a6787c
parent 7923
91e843545d9a
child 8218
7c09585bd960
permissions
-rw-r--r--

Shell: added functionality to show a prompt when the main client process has exited (e.g. a script ended).

6028
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
7923
91e843545d9a Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7781
diff changeset
3 # Copyright (c) 2017 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
6028
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing the AdBlock matcher.
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 from PyQt5.QtCore import QObject
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 from .AdBlockSearchTree import AdBlockSearchTree
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 from .AdBlockRule import AdBlockRule, AdBlockRuleOption
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 class AdBlockMatcher(QObject):
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 Class implementing the AdBlock matcher.
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 def __init__(self, manager):
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 Constructor
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 @param manager reference to the AdBlock manager object
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 @type AdBlockManager
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 super(AdBlockMatcher, self).__init__(manager)
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 self.__manager = manager
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 self.__createdRules = []
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 self.__networkExceptionRules = []
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 self.__networkBlockRules = []
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 self.__domainRestrictedCssRules = []
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 self.__documentRules = []
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 self.__elemhideRules = []
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 self.__elementHidingRules = ""
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 self.__networkBlockTree = AdBlockSearchTree()
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 self.__networkExceptionTree = AdBlockSearchTree()
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 def match(self, request, urlDomain, urlString):
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 Public method to match a request.
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 @param request URL request to be matched
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 @type QWebEngineUrlRequestInfo
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 @param urlDomain domain of the URL
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 @type str
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 @param urlString requested URL as a lowercase string
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 @type str
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 @return reference to the matched rule
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 @rtype AdBlockRule
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 # exception rules
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 if self.__networkExceptionTree.find(request, urlDomain, urlString):
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 return None
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 for rule in self.__networkExceptionRules:
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 if rule.networkMatch(request, urlDomain, urlString):
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 return None
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 # block rules
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 rule = self.__networkBlockTree.find(request, urlDomain, urlString)
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 if rule:
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 return rule
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 for rule in self.__networkBlockRules:
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 if rule.networkMatch(request, urlDomain, urlString):
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 return rule
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 return None
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 def adBlockDisabledForUrl(self, url):
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 Public method to check, if AdBlock is disabled for the given URL.
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 @param url URL to check
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 @type QUrl
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 @return flag indicating disabled state
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 @rtype bool
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 for rule in self.__documentRules:
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 if rule.urlMatch(url):
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 return True
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 return False
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 def elemHideDisabledForUrl(self, url):
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 Public method to check, if element hiding is disabled for the given
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 URL.
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 @param url URL to check
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 @type QUrl
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 @return flag indicating disabled state
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 @rtype bool
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 if self.adBlockDisabledForUrl(url):
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 return True
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 for rule in self.__elemhideRules:
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 if rule.urlMatch(url):
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 return True
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 return False
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 def elementHidingRules(self):
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 Public method to get the element hiding rules.
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 @return element hiding rules
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 @rtype str
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 return self.__elementHidingRules
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 def elementHidingRulesForDomain(self, domain):
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 Public method to get the element hiding rules for the given domain.
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 @param domain domain name
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 @type str
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 @return element hiding rules
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 @rtype str
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 rules = ""
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 addedRulesCount = 0
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 for rule in self.__domainRestrictedCssRules:
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 if not rule.matchDomain(domain):
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 continue
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 if addedRulesCount == 1000:
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 rules += rule.cssSelector()
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 rules += "{display:none !important;}\n"
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 addedRulesCount = 0
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 else:
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 rules += rule.cssSelector() + ","
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 addedRulesCount += 1
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 if addedRulesCount != 0:
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 rules = rules[:-1]
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 rules += "{display:none !important;}\n"
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 return rules
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 def update(self):
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 Public slot to update the internal state.
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 self.clear()
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 cssRulesDict = {}
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 exceptionCssRules = []
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 for subscription in self.__manager.subscriptions():
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 if subscription.isEnabled():
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 for rule in subscription.allRules():
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 # Don't add internally disabled rules to the cache
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 if rule.isInternalDisabled():
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 continue
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 if rule.isCSSRule():
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 # Only enabled CSS rules are added to the cache because
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 # there is no enabled/disabled check on match. They are
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 # directly embedded to pages.
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 if not rule.isEnabled():
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 continue
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 if rule.isException():
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 exceptionCssRules.append(rule)
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 else:
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 cssRulesDict[rule.cssSelector()] = rule
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 elif rule.isDocument():
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 self.__documentRules.append(rule)
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 elif rule.isElementHiding():
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 self.__elemhideRules.append(rule)
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 elif rule.isException():
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 if not self.__networkExceptionTree.add(rule):
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 self.__networkBlockRules.append(rule)
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 else:
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 if not self.__networkBlockTree.add(rule):
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 self.__networkBlockRules.append(rule)
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 for rule in exceptionCssRules:
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 try:
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 originalRule = cssRulesDict[rule.cssSelector()]
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 except KeyError:
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 # If there is no such selector, the exception does nothing.
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 continue
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 copiedRule = AdBlockRule()
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193 copiedRule.copyFrom(originalRule)
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 copiedRule.setOption(AdBlockRuleOption.DomainRestrictedOption)
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 copiedRule.addBlockedDomains(rule.allowedDomains())
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 cssRulesDict[rule.cssSelector()] = copiedRule
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 self.__createdRules.append(copiedRule)
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 # Excessive amount of selectors for one CSS rule is not what the
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 # rendering engine likes. So split them up by 1.000 selectors.
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 hidingRulesCount = 0
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 for key in cssRulesDict:
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 rule = cssRulesDict[key]
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 if rule.isDomainRestricted():
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 self.__domainRestrictedCssRules.append(rule)
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 elif hidingRulesCount == 1000:
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 self.__elementHidingRules += rule.cssSelector()
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 self.__elementHidingRules += "{display:none !important;} "
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 hidingRulesCount = 0
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 else:
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 self.__elementHidingRules += rule.cssSelector() + ","
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 hidingRulesCount += 1
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 if hidingRulesCount != 0:
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 self.__elementHidingRules = self.__elementHidingRules[:-1]
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 self.__elementHidingRules += "{display:none !important;} "
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 def clear(self):
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 Public slot to clear the internal structures.
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 """
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 self.__createdRules = []
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 self.__networkExceptionRules = []
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 self.__networkBlockRules = []
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 self.__domainRestrictedCssRules = []
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 self.__documentRules = []
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 self.__elemhideRules = []
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 self.__elementHidingRules = ""
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 self.__networkBlockTree.clear()
859f6894eed9 Reworked the AdBlock stuff of web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 self.__networkExceptionTree.clear()

eric ide

mercurial