WebBrowser/AdBlock/AdBlockPage.py

branch
QtWebEngine
changeset 4847
a1a8eac81b54
parent 4631
5c1a96925da4
child 4860
0a44aff88bfa
equal deleted inserted replaced
4846:960e5e18894b 4847:a1a8eac81b54
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 - 2016 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a class to apply AdBlock rules to a web page.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import QObject, QUrl
13
14 from ..Tools import Scripts
15
16
17 class AdBlockPage(QObject):
18 """
19 Class to apply AdBlock rules to a web page.
20 """
21 def hideBlockedPageEntries(self, page):
22 """
23 Public method to apply AdBlock rules to a web page.
24
25 @param page reference to the web page (HelpWebPage)
26 """
27 if page is None:
28 return
29
30 from WebBrowser.WebBrowserWindow import WebBrowserWindow
31 manager = WebBrowserWindow.adBlockManager()
32 if not manager.isEnabled():
33 return
34
35 # apply domain specific element hiding rules
36 elementHiding = manager.elementHidingRulesForDomain(page.url())
37 if elementHiding:
38 script = Scripts.setCss(elementHiding)
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()

eric ide

mercurial