Sun, 18 Dec 2016 18:46:43 +0100
Fixed an issue in AdBlockRule.py caused by a wrong NetworkAccessManager setting.
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

1  # * coding: utf8 * 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

2  
4631
5c1a96925da4
Updated copyright for 2016.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
4326
diff
changeset

3  # Copyright (c) 2009  2016 Detlev Offenbach <detlev@dieoffenbachs.de> 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

4  # 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

5  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

6  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

7  Module implementing the AdBlock rule class. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

8  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

9  
3145
a9de05d4a22f
# __IGNORE_WARNING__ added/ removed.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3060
diff
changeset

10  from __future__ import unicode_literals 
2525
8b507a9a2d40
Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
2302
diff
changeset

11  
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

12  import re 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

13  
3656
441956d8fce5
Started porting eric5 to PyQt5.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
3178
diff
changeset

14  from PyQt5.QtCore import Qt, QRegExp, QUrl, qVersion 
441956d8fce5
Started porting eric5 to PyQt5.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
3178
diff
changeset

15  from PyQt5.QtNetwork import QNetworkRequest 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

16  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

17  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

18  # Qt version < 4.8 has an issue; it will wrongly 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

19  # count .co.uk (and others) as secondlevel domains 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

20  def toSecondLevelDomain(url): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

21  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

22  Module function to get a second level domain from the given URL. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

23  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

24  @param url URL to extract domain from (QUrl) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

25  @return name of second level domain (string) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

26  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

27  if qVersion() >= "4.8.0": 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

28  topLevelDomain = url.topLevelDomain() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

29  urlHost = url.host() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

30  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

31  if not topLevelDomain or not urlHost: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

32  return "" 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

33  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

34  domain = urlHost[:len(urlHost)  len(topLevelDomain)] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

35  if domain.count(".") == 0: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

36  return urlHost 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

37  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

38  while domain.count(".") != 0: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

39  domain = domain[domain.find(".") + 1:] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

40  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

41  return domain + topLevelDomain 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

42  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

43  domain = url.host() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

44  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

45  if domain.count(".") == 0: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

46  return "" 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

47  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

48  while domain.count(".") != 1: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

49  domain = domain[domain.find(".") + 1:] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

50  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

51  return domain 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

52  
945
8cd4d08fa9f6
Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
791
diff
changeset

53  
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

54  class AdBlockRule(object): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

55  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

56  Class implementing the AdBlock rule. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

57  """ 
1963
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

58  def __init__(self, filter="", subscription=None): 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

59  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

60  Constructor 
2953
703452a2876f
Started correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2302
diff
changeset

61  
703452a2876f
Started correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2302
diff
changeset

62  @param filter filter string of the rule (string) 
703452a2876f
Started correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2302
diff
changeset

63  @param subscription reference to the subscription object 
703452a2876f
Started correcting doc strings by using the new doc string checker.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2302
diff
changeset

64  (AdBlockSubscription) 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

65  """ 
1963
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

66  self.__subscription = subscription 
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

67  
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

68  self.__regExp = QRegExp() 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

69  self.__options = [] 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

70  self.__blockedDomains = [] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

71  self.__allowedDomains = [] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

72  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

73  self.__enabled = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

74  self.__cssRule = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

75  self.__exception = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

76  self.__internalDisabled = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

77  self.__domainRestricted = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

78  self.__useRegExp = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

79  self.__useDomainMatch = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

80  self.__useEndsMatch = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

81  self.__thirdParty = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

82  self.__thirdPartyException = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

83  self.__object = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

84  self.__objectException = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

85  self.__subdocument = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

86  self.__subdocumentException = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

87  self.__xmlhttprequest = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

88  self.__xmlhttprequestException = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

89  self.__document = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

90  self.__elemhide = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

91  self.__caseSensitivity = Qt.CaseInsensitive 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

92  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

93  self.setFilter(filter) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

94  
1963
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

95  def subscription(self): 
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

96  """ 
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

97  Public method to get the subscription this rule belongs to. 
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

98  
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

99  @return subscription of the rule (AdBlockSubscription) 
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

100  """ 
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

101  return self.__subscription 
9c5b3235abf9
Added an icon to the status bar to give direct access to AdBlock.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1960
diff
changeset

102  
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

103  def filter(self): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

104  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

105  Public method to get the rule filter string. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

106  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

107  @return rule filter string (string) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

108  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

109  return self.__filter 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

110  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

111  def setFilter(self, filter): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

112  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

113  Public method to set the rule filter string. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

114  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

115  @param filter rule filter string (string) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

116  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

117  self.__filter = filter 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

118  self.__parseFilter() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

119  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

120  def __parseFilter(self): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

121  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

122  Private method to parse the filter pattern. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

123  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

124  parsedLine = self.__filter 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

125  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

126  # empty rule or just a comment 
1994
84d55b34ba5a
Added support for AdBlock checksum checking when downloading a subscription to the web browser.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1963
diff
changeset

127  if not parsedLine.strip() or parsedLine.startswith(("!", "[Adblock")): 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

128  self.__enabled = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

129  return 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

130  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

131  # CSS element hiding rule 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

132  if "##" in parsedLine: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

133  self.__cssRule = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

134  pos = parsedLine.find("##") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

135  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

136  # domain restricted rule 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

137  if not parsedLine.startswith("##"): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

138  domains = parsedLine[:pos] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

139  self.__parseDomains(domains, ",") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

140  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

141  self.__cssSelector = parsedLine[pos + 2:] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

142  # CSS rule cannot have more options > stop parsing 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

143  return 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

144  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

145  # Exception always starts with @@ 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

146  if parsedLine.startswith("@@"): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

147  self.__exception = True 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

148  parsedLine = parsedLine[2:] 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

149  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

150  # Parse all options following '$' character 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

151  optionsIndex = parsedLine.find("$") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

152  if optionsIndex >= 0: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

153  options = parsedLine[optionsIndex + 1:].split(",") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

154  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

155  handledOptions = 0 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

156  for option in options: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

157  if option.startswith("domain="): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

158  self.__parseDomains(option[7:], "") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

159  handledOptions += 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

160  elif option == "matchcase": 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

161  self.__caseSensitivity = Qt.CaseSensitive 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

162  handledOptions += 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

163  elif option.endswith("thirdparty"): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

164  self.__thirdParty = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

165  self.__thirdPartyException = option.startswith("~") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

166  handledOptions += 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

167  elif option.endswith("object"): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

168  self.__object = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

169  self.__objectException = option.startswith("~") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

170  handledOptions += 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

171  elif option.endswith("subdocument"): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

172  self.__subdocument = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

173  self.__subdocumentException = option.startswith("~") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

174  handledOptions += 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

175  elif option.endswith("xmlhttprequest"): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

176  self.__xmlhttprequest = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

177  self.__xmlhttprequestException = option.startswith("~") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

178  handledOptions += 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

179  elif option == "document" and self.__exception: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

180  self.__document = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

181  handledOptions += 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

182  elif option == "elemhide" and self.__exception: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

183  self.__elemhide = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

184  handledOptions += 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

185  elif option == "collapse": 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

186  # Hiding placeholders of blocked elements 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

187  handledOptions += 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

188  
3000
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

189  # If we don't handle all options, it's safer to just disable 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

190  # this rule 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

191  if handledOptions != len(options): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

192  self.__internalDisabled = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

193  return 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

194  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

195  parsedLine = parsedLine[:optionsIndex] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

196  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

197  # Rule is classic regexp 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

198  if parsedLine.startswith("/") and parsedLine.endswith("/"): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

199  parsedLine = parsedLine[1:1] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

200  self.__useRegExp = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

201  self.__regExp = QRegExp(parsedLine, self.__caseSensitivity, 
3035
36e9f388958b
Fixed a bunch of indentation issues.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
3022
diff
changeset

202  QRegExp.RegExp) 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

203  return 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

204  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

205  # Remove starting / ending wildcards 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

206  if parsedLine.startswith("*"): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

207  parsedLine = parsedLine[1:] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

208  if parsedLine.endswith("*"): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

209  parsedLine = parsedLine[:1] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

210  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

211  # Fast string matching for domain can be used 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

212  if parsedLine.startswith("") and \ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

213  parsedLine.endswith("^") and \ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

214  QRegExp("[/:?=&\\*]").indexIn(parsedLine) == 1: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

215  parsedLine = parsedLine[2:1] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

216  self.__useDomainMatch = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

217  self.__matchString = parsedLine 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

218  return 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

219  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

220  # If rule contains '' only at the end, string matching can be used 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

221  if parsedLine.endswith("") and \ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

222  QRegExp("[\\^\\*]").indexIn(parsedLine) == 1 and \ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

223  parsedLine.count("") == 1: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

224  parsedLine = parsedLine[:1] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

225  self.__useEndsMatch = True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

226  self.__matchString = parsedLine 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

227  return 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

228  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

229  # If there is still a wildcard (*) or separator (^) or (), 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

230  # the rule must be modified to comply with QRegExp. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

231  if "*" in parsedLine or "^" in parsedLine or "" in parsedLine: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

232  pattern = self.__convertPatternToRegExp(parsedLine) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

233  self.__useRegExp = True 
3000
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

234  self.__regExp = QRegExp(pattern, self.__caseSensitivity, 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

235  QRegExp.RegExp) 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

236  return 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

237  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

238  # no regexp required 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

239  self.__useRegExp = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

240  self.__matchString = parsedLine 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

241  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

242  def __parseDomains(self, domains, separator): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

243  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

244  Private method to parse a string with a domain list. 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

245  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

246  @param domains list of domains (string) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

247  @param separator separator character used by the list (string) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

248  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

249  domainsList = domains.split(separator) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

250  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

251  for domain in domainsList: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

252  if not domain: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

253  continue 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

254  if domain.startswith("~"): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

255  self.__blockedDomains.append(domain[1:]) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

256  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

257  self.__allowedDomains.append(domain) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

258  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

259  self.__domainRestricted = \ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

260  bool(self.__blockedDomains) or bool(self.__allowedDomains) 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

261  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

262  def networkMatch(self, request, domain, encodedUrl): 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

263  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

264  Public method to check the rule for a match. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

265  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

266  @param request reference to the network request (QNetworkRequest) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

267  @param domain domain name (string) 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

268  @param encodedUrl string encoded URL to be checked (string) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

269  @return flag indicating a match (boolean) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

270  """ 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

271  if self.__cssRule or not self.__enabled or self.__internalDisabled: 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

272  return False 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

273  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

274  matched = False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

275  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

276  if self.__useRegExp: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

277  matched = self.__regExp.indexIn(encodedUrl) != 1 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

278  elif self.__useDomainMatch: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

279  matched = domain.endswith(self.__matchString) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

280  elif self.__useEndsMatch: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

281  if self.__caseSensitivity == Qt.CaseInsensitive: 
3000
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

282  matched = encodedUrl.lower().endswith( 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

283  self.__matchString.lower()) 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

284  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

285  matched = encodedUrl.endswith(self.__matchString) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

286  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

287  if self.__caseSensitivity == Qt.CaseInsensitive: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

288  matched = self.__matchString.lower() in encodedUrl.lower() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

289  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

290  matched = self.__matchString in encodedUrl 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

291  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

292  if matched: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

293  # check domain restrictions 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

294  if self.__domainRestricted and not self.matchDomain(domain): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

295  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

296  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

297  # check thirdparty restrictions 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

298  if self.__thirdParty and not self.matchThirdParty(request): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

299  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

300  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

301  # check object restrictions 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

302  if self.__object and not self.matchObject(request): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

303  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

304  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

305  # check subdocument restrictions 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

306  if self.__subdocument and not self.matchSubdocument(request): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

307  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

308  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

309  # check xmlhttprequest restriction 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

310  if self.__xmlhttprequest and not self.matchXmlHttpRequest(request): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

311  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

312  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

313  return matched 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

314  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

315  def urlMatch(self, url): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

316  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

317  Public method to check an URL against the rule. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

318  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

319  @param url URL to check (QUrl) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

320  @return flag indicating a match (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

321  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

322  if not self.__document and not self.__elemhide: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

323  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

324  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

325  encodedUrl = bytes(url.toEncoded()).decode() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

326  domain = url.host() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

327  return self.networkMatch(QNetworkRequest(url), domain, encodedUrl) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

328  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

329  def matchDomain(self, domain): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

330  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

331  Public method to match a domain. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

332  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

333  @param domain domain name to check (string) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

334  @return flag indicating a match (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

335  """ 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

336  if not self.__enabled: 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

337  return False 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

338  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

339  if not self.__domainRestricted: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

340  return True 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

341  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

342  if len(self.__blockedDomains) == 0: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

343  for dom in self.__allowedDomains: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

344  if domain.endswith(dom): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

345  return True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

346  elif len(self.__allowedDomains) == 0: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

347  for dom in self.__blockedDomains: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

348  if domain.endswith(dom): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

349  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

350  return True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

351  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

352  for dom in self.__blockedDomains: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

353  if domain.endswith(dom): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

354  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

355  for dom in self.__allowedDomains: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

356  if domain.endswith(dom): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

357  return True 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

358  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

359  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

360  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

361  def matchThirdParty(self, req): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

362  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

363  Public slot to match a thirdparty rule. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

364  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

365  @param req request object to check (QNetworkRequest) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

366  @return flag indicating a match (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

367  """ 
5383
4bf49e0840f3
Fixed an issue in AdBlockRule.py caused by a wrong NetworkAccessManager setting.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
4631
diff
changeset

368  referer = \ 
4bf49e0840f3
Fixed an issue in AdBlockRule.py caused by a wrong NetworkAccessManager setting.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
4631
diff
changeset

369  bytes(req.attribute(QNetworkRequest.User + 200, b"")).decode() 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

370  if referer == "": 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

371  return False 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

372  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

373  # Thirdparty matching should be performed on secondlevel domains 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

374  refererHost = toSecondLevelDomain(QUrl(referer)) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

375  host = toSecondLevelDomain(req.url()) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

376  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

377  match = refererHost != host 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

378  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

379  if self.__thirdPartyException: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

380  return not match 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

381  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

382  return match 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

383  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

384  def matchObject(self, req): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

385  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

386  Public slot to match an object rule. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

387  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

388  @param req request object to check (QNetworkRequest) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

389  @return flag indicating a match (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

390  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

391  match = req.attribute(QNetworkRequest.User + 200) == "object" 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

392  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

393  if self.__objectException: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

394  return not match 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

395  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

396  return match 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

397  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

398  def matchSubdocument(self, req): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

399  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

400  Public slot to match a subdocument rule. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

401  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

402  @param req request object to check (QNetworkRequest) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

403  @return flag indicating a match (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

404  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

405  originatingFrame = req.originatingObject() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

406  if originatingFrame is None: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

407  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

408  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

409  page = originatingFrame.page() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

410  if page is None: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

411  return False 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

412  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

413  match = originatingFrame != page.mainFrame() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

414  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

415  if self.__subdocumentException: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

416  return not match 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

417  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

418  return match 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

419  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

420  def matchXmlHttpRequest(self, req): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

421  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

422  Public slot to match a XmlHttpRequest rule. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

423  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

424  @param req request object to check (QNetworkRequest) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

425  @return flag indicating a match (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

426  """ 
4326
e52318f11812
Change code to make eric compatible to PyQt 5.5 (QByteArry issue).
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
4021
diff
changeset

427  match = req.rawHeader(b"XRequestWith") == "XMLHttpRequest" 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

428  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

429  if self.__xmlhttprequestException: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

430  return not match 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

431  else: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

432  return match 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

433  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

434  def isException(self): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

435  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

436  Public method to check, if the rule defines an exception. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

437  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

438  @return flag indicating an exception (boolean) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

439  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

440  return self.__exception 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

441  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

442  def setException(self, exception): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

443  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

444  Public method to set the rule's exception flag. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

445  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

446  @param exception flag indicating an exception rule (boolean) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

447  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

448  self.__exception = exception 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

449  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

450  def isEnabled(self): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

451  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

452  Public method to check, if the rule is enabled. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

453  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

454  @return flag indicating enabled state (boolean) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

455  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

456  return self.__enabled 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

457  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

458  def setEnabled(self, enabled): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

459  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

460  Public method to set the rule's enabled state. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

461  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

462  @param enabled flag indicating the new enabled state (boolean) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

463  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

464  self.__enabled = enabled 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

465  if not enabled: 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

466  self.__filter = "!" + self.__filter 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

467  else: 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

468  self.__filter = self.__filter[1:] 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

469  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

470  def isCSSRule(self): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

471  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

472  Public method to check, if the rule is a CSS rule. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

473  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

474  @return flag indicating a CSS rule (boolean) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

475  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

476  return self.__cssRule 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

477  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

478  def cssSelector(self): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

479  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

480  Public method to get the CSS selector of the rule. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

481  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

482  @return CSS selector (string) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

483  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

484  return self.__cssSelector 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

485  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

486  def isDocument(self): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

487  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

488  Public method to check, if this is a document rule. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

489  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

490  @return flag indicating a document rule (boolean) 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

491  """ 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

492  return self.__document 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

493  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

494  def isElementHiding(self): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

495  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

496  Public method to check, if this is an element hiding rule. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

497  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

498  @return flag indicating an element hiding rule (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

499  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

500  return self.__elemhide 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

501  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

502  def isDomainRestricted(self): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

503  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

504  Public method to check, if this rule is restricted by domain. 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

505  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

506  @return flag indicating a domain restriction (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

507  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

508  return self.__domainRestricted 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

509  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

510  def isComment(self): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

511  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

512  Public method to check, if this is a comment. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

513  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

514  @return flag indicating a comment (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

515  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

516  return self.__filter.startswith("!") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

517  
1994
84d55b34ba5a
Added support for AdBlock checksum checking when downloading a subscription to the web browser.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1963
diff
changeset

518  def isHeader(self): 
84d55b34ba5a
Added support for AdBlock checksum checking when downloading a subscription to the web browser.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1963
diff
changeset

519  """ 
84d55b34ba5a
Added support for AdBlock checksum checking when downloading a subscription to the web browser.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1963
diff
changeset

520  Public method to check, if this is a header. 
84d55b34ba5a
Added support for AdBlock checksum checking when downloading a subscription to the web browser.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1963
diff
changeset

521  
84d55b34ba5a
Added support for AdBlock checksum checking when downloading a subscription to the web browser.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1963
diff
changeset

522  @return flag indicating a header (boolean) 
84d55b34ba5a
Added support for AdBlock checksum checking when downloading a subscription to the web browser.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1963
diff
changeset

523  """ 
84d55b34ba5a
Added support for AdBlock checksum checking when downloading a subscription to the web browser.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1963
diff
changeset

524  return self.__filter.startswith("[Adblock") 
84d55b34ba5a
Added support for AdBlock checksum checking when downloading a subscription to the web browser.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1963
diff
changeset

525  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

526  def isSlow(self): 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

527  """ 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

528  Public method to check, if this is a slow rule. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

529  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

530  @return flag indicating a slow rule (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

531  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

532  return self.__useRegExp 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

533  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

534  def isInternalDisabled(self): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

535  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

536  Public method to check, if this rule was disabled internally. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

537  
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

538  @return flag indicating an internally disabled rule (boolean) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

539  """ 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1950
diff
changeset

540  return self.__internalDisabled 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

541  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

542  def __convertPatternToRegExp(self, wildcardPattern): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

543  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

544  Private method to convert a wildcard pattern to a regular expression. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

545  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

546  @param wildcardPattern string containing the wildcard pattern (string) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

547  @return string containing a regular expression (string) 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

548  """ 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

549  pattern = wildcardPattern 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

550  
3000
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

551  # remove multiple wildcards 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

552  pattern = re.sub(r"\*+", "*", pattern) 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

553  # remove anchors following separator placeholder 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

554  pattern = re.sub(r"\^\$", "^", pattern) 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

555  # remove leading wildcards 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

556  pattern = re.sub(r"^(\*)", "", pattern) 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

557  # remove trailing wildcards 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

558  pattern = re.sub(r"(\*)$", "", pattern) 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

559  # escape special symbols 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

560  pattern = re.sub(r"(\W)", r"\\\1", pattern) 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

561  # process extended anchor at expression start 
3022
57179e4cdadd
Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
3000
diff
changeset

562  pattern = re.sub( 
57179e4cdadd
Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
3000
diff
changeset

563  r"^\\\\\\", 
3000
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

564  r"^[\w\]+:\/+(?!\/)(?:[^\/]+\.)?", pattern) 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

565  # process separator placeholders 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

566  pattern = re.sub(r"\\\^", r"(?:[^\w\d\.%]$)", pattern) 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

567  # process anchor at expression start 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

568  pattern = re.sub(r"^\\\", "^", pattern) 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

569  # process anchor at expression end 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

570  pattern = re.sub(r"\\\$", "$", pattern) 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

571  # replace wildcards by .* 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2953
diff
changeset

572  pattern = re.sub(r"\\\*", ".*", pattern) 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

573  
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

574  return pattern 