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 __future__ import unicode_literals |
10 from __future__ import unicode_literals |
11 |
11 |
12 from PyQt5.QtCore import QObject, QUrl |
12 from PyQt5.QtCore import QObject |
13 |
13 |
14 from ..Tools import Scripts |
14 from ..Tools import Scripts |
15 |
15 |
16 |
16 |
17 class AdBlockPage(QObject): |
17 class AdBlockPage(QObject): |
35 # apply domain specific element hiding rules |
35 # apply domain specific element hiding rules |
36 elementHiding = manager.elementHidingRulesForDomain(page.url()) |
36 elementHiding = manager.elementHidingRulesForDomain(page.url()) |
37 if elementHiding: |
37 if elementHiding: |
38 script = Scripts.setCss(elementHiding) |
38 script = Scripts.setCss(elementHiding) |
39 page.runJavaScript(script) |
39 page.runJavaScript(script) |
40 ## docElement = page.mainFrame().documentElement() |
|
41 ## |
|
42 ## for entry in page.getAdBlockedPageEntries(): |
|
43 ## urlString = entry.urlString() |
|
44 ## if urlString.endswith((".js", ".css")): |
|
45 ## continue |
|
46 ## |
|
47 ## urlEnd = "" |
|
48 ## pos = urlString.rfind("/") |
|
49 ## if pos >= 0: |
|
50 ## urlEnd = urlString[pos + 1:] |
|
51 ## if urlString.endswith("/"): |
|
52 ## urlEnd = urlString[:-1] |
|
53 ## |
|
54 ## selector = \ |
|
55 ## 'img[src$="{0}"], iframe[src$="{0}"], embed[src$="{0}"]'\ |
|
56 ## .format(urlEnd) |
|
57 ## elements = docElement.findAll(selector) |
|
58 ## |
|
59 ## for element in elements: |
|
60 ## src = element.attribute("src") |
|
61 ## src = src.replace("../", "") |
|
62 ## if src in urlString: |
|
63 ## element.setStyleProperty("display", "none") |
|
64 ## |
|
65 ## |
|
66 ## elementHiding += "{display: none !important;}\n</style>" |
|
67 ## |
|
68 ## bodyElement = docElement.findFirst("body") |
|
69 ## bodyElement.appendInside( |
|
70 ## '<style type="text/css">\n/* AdBlock for eric */\n' + |
|
71 ## elementHiding) |
|
72 ## |
|
73 ## |
|
74 ##class AdBlockedPageEntry(object): |
|
75 ## """ |
|
76 ## Class implementing a data structure for web page rules. |
|
77 ## """ |
|
78 ## def __init__(self, rule, url): |
|
79 ## """ |
|
80 ## Constructor |
|
81 ## |
|
82 ## @param rule AdBlock rule to add (AdBlockRule) |
|
83 ## @param url URL that matched the rule (QUrl) |
|
84 ## """ |
|
85 ## self.rule = rule |
|
86 ## self.url = QUrl(url) |
|
87 ## |
|
88 ## def __eq__(self, other): |
|
89 ## """ |
|
90 ## Special method to test equality. |
|
91 ## |
|
92 ## @param other reference to the other entry (AdBlockedPageEntry) |
|
93 ## @return flag indicating equality (boolean) |
|
94 ## """ |
|
95 ## return self.rule == other.rule and self.url == other.url |
|
96 ## |
|
97 ## def urlString(self): |
|
98 ## """ |
|
99 ## Public method to get the URL as a string. |
|
100 ## |
|
101 ## @return URL as a string (string) |
|
102 ## """ |
|
103 ## return self.url.toString() |
|