Wed, 09 Oct 2013 19:47:41 +0200
Continued to shorten the code lines to max. 79 characters.
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  
2302
f29e9405c851
Updated copyright for 2013.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1970
diff
changeset

3  # Copyright (c) 2009  2013 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 a class to apply AdBlock rules to a web page. 
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  
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

10  from PyQt4.QtCore import QObject, QUrl 
381
64ae9e09d8bc
Extended the AdBlocker of the web browser and added capability to configure the text encoding of the web browser (needs Qt >= 4.6).
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
96
diff
changeset

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

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

13  class AdBlockPage(QObject): 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

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

15  Class to apply AdBlock rules to a web page. 
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

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

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

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

19  Public method to apply AdBlock rules to a web page. 
1947
84626f18f2c3
Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1509
diff
changeset

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

21  @param page reference to the web page (HelpWebPage) 
0
de9c2efb9d02
Started porting eric4 to Python3
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
diff
changeset

22  """ 
1947
84626f18f2c3
Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1509
diff
changeset

23  if page is None or page.mainFrame() is None: 
84626f18f2c3
Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1509
diff
changeset

24  return 
84626f18f2c3
Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1509
diff
changeset

25  
2403
e3d7a861547c
Continued implementing the delayed import.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2302
diff
changeset

26  import Helpviewer.HelpWindow 
1970
02cf3bac079b
Rewrote huge part of AdBlock to speed up and added support for dependent AdBlock subscriptions.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1965
diff
changeset

27  manager = Helpviewer.HelpWindow.HelpWindow.adBlockManager() 
1947
84626f18f2c3
Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1509
diff
changeset

28  if not manager.isEnabled(): 
84626f18f2c3
Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1509
diff
changeset

29  return 
84626f18f2c3
Simplified the code a little bit by deleting the checks for the existance of QWebElement because Qt 4.7 is the minimum requirement.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1509
diff
changeset

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

31  docElement = page.mainFrame().documentElement() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

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

33  for entry in page.getAdBlockedPageEntries(): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

34  urlString = entry.urlString() 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

35  if urlString.endswith((".js", ".css")): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

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

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

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

39  pos = urlString.rfind("/") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

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

41  urlEnd = urlString[pos + 1:] 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

42  if urlString.endswith("/"): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

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

44  
3001
3674ff5fa8f8
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
3000
diff
changeset

45  selector = \ 
3674ff5fa8f8
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
3000
diff
changeset

46  'img[src$="{0}"], iframe[src$="{0}"], embed[src$="{0}"]'\ 
3674ff5fa8f8
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
3000
diff
changeset

47  .format(urlEnd) 
1960
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

48  elements = docElement.findAll(selector) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

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

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

51  src = element.attribute("src") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

52  src = src.replace("../", "") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

53  if src in urlString: 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

54  element.setStyleProperty("display", "none") 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

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

56  # apply domain specific element hiding rules 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

57  elementHiding = manager.elementHidingRulesForDomain(page.url()) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

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

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

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

61  elementHiding += "{display: none !important;}\n</style>" 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

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

63  bodyElement = docElement.findFirst("body") 
3000
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2403
diff
changeset

64  bodyElement.appendInside( 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2403
diff
changeset

65  '<style type="text/css">\n/* AdBlock for eric */\n' + 
971d84f7a6d6
Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
2403
diff
changeset

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

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

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

69  class AdBlockedPageEntry(object): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

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

71  Class implementing a data structure for web page rules. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

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

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

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

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

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

77  @param rule AdBlock rule to add (AdBlockRule) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

78  @param url URL that matched the rule (QUrl) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

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

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

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

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

83  def __eq__(self, other): 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

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

85  Special method to test equality. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

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

87  @param other reference to the other entry (AdBlockedPageEntry) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

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

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

90  return self.rule == other.rule and self.url == other.url 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

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

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

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

94  Public method to get the URL as a string. 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

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

96  @return URL as a string (string) 
d8c45fe8a1b9
Reworked some of the AdBlock code to achieve some speed improvements.
Detlev Offenbach <detlev@dieoffenbachs.de>
parents:
1947
diff
changeset

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

98  return self.url.toString() 