Helpviewer/AdBlock/AdBlockPage.py

changeset 1947
84626f18f2c3
parent 1509
c0b5e693b0eb
child 1960
d8c45fe8a1b9
equal deleted inserted replaced
1945:47016f5af3b8 1947:84626f18f2c3
6 """ 6 """
7 Module implementing a class to apply AdBlock rules to a web page. 7 Module implementing a class to apply AdBlock rules to a web page.
8 """ 8 """
9 9
10 from PyQt4.QtCore import QObject 10 from PyQt4.QtCore import QObject
11 from PyQt4 import QtWebKit
12 11
13 import Helpviewer.HelpWindow 12 import Helpviewer.HelpWindow
14 13
15 14
16 class AdBlockPage(QObject): 15 class AdBlockPage(QObject):
23 22
24 @param rule reference to the rule to check (AdBlockRule) 23 @param rule reference to the rule to check (AdBlockRule)
25 @param page reference to the web page (QWebPage) 24 @param page reference to the web page (QWebPage)
26 @param host host name (string) 25 @param host host name (string)
27 """ 26 """
28 if hasattr(QtWebKit, 'QWebElement'): 27 if not rule.isEnabled():
29 if not rule.isEnabled(): 28 return
29
30 filter = rule.filter()
31 offset = filter.find("##")
32 if offset == -1:
33 return
34
35 selectorQuery = ""
36 if offset > 0:
37 domainRules = filter[:offset]
38 selectorQuery = filter[offset + 2:]
39 domains = domainRules.split(",")
40
41 match = False
42 for domain in domains:
43 reverse = domain[0] == '~'
44 if reverse:
45 xdomain = domain[1:]
46 if host.endswith(xdomain):
47 return
48 match = True
49 if host.endswith(domain):
50 match = True
51 if not match:
30 return 52 return
31 53
32 filter = rule.filter() 54 if offset == 0:
33 offset = filter.find("##") 55 selectorQuery = filter[2:]
34 if offset == -1: 56
35 return 57 document = page.mainFrame().documentElement()
36 58 elements = document.findAll(selectorQuery)
37 selectorQuery = "" 59 for element in elements.toList():
38 if offset > 0: 60 element.setStyleProperty("visibility", "hidden")
39 domainRules = filter[:offset] 61 element.removeFromDocument()
40 selectorQuery = filter[offset + 2:]
41 domains = domainRules.split(",")
42
43 match = False
44 for domain in domains:
45 reverse = domain[0] == '~'
46 if reverse:
47 xdomain = domain[1:]
48 if host.endswith(xdomain):
49 return
50 match = True
51 if host.endswith(domain):
52 match = True
53 if not match:
54 return
55
56 if offset == 0:
57 selectorQuery = filter[2:]
58
59 document = page.mainFrame().documentElement()
60 elements = document.findAll(selectorQuery)
61 for element in elements.toList():
62 element.setStyleProperty("visibility", "hidden")
63 element.removeFromDocument()
64 62
65 def applyRulesToPage(self, page): 63 def applyRulesToPage(self, page):
66 """ 64 """
67 Public method to applay AdBlock rules to a web page. 65 Public method to applay AdBlock rules to a web page.
68 66
69 @param page reference to the web page (QWebPage) 67 @param page reference to the web page (QWebPage)
70 """ 68 """
71 if hasattr(QtWebKit, 'QWebElement'): 69 if page is None or page.mainFrame() is None:
72 if page is None or page.mainFrame() is None: 70 return
73 return 71
74 72 manager = Helpviewer.HelpWindow.HelpWindow.adblockManager()
75 manager = Helpviewer.HelpWindow.HelpWindow.adblockManager() 73 if not manager.isEnabled():
76 if not manager.isEnabled(): 74 return
77 return 75
78 76 host = page.mainFrame().url().host()
79 host = page.mainFrame().url().host() 77 subscriptions = manager.subscriptions()
80 subscriptions = manager.subscriptions() 78 for subscription in subscriptions:
81 for subscription in subscriptions: 79 rules = subscription.pageRules()
82 rules = subscription.pageRules() 80 for rule in rules:
83 for rule in rules: 81 self.__checkRule(rule, page, host)
84 self.__checkRule(rule, page, host)

eric ide

mercurial