WebBrowser/AdBlock/AdBlockManager.py

branch
QtWebEngine
changeset 4858
19dff9c9cf26
parent 4847
a1a8eac81b54
child 4859
36c4b21c9f7b
equal deleted inserted replaced
4857:8dba5fb92f12 4858:19dff9c9cf26
9 9
10 from __future__ import unicode_literals 10 from __future__ import unicode_literals
11 11
12 import os 12 import os
13 13
14 from PyQt5.QtCore import pyqtSignal, QObject, QUrl, QFile 14 from PyQt5.QtCore import pyqtSignal, QObject, QUrl, QUrlQuery, QFile, \
15 QByteArray
16 from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInfo
17
18 from E5Gui import E5MessageBox
15 19
16 from .AdBlockSubscription import AdBlockSubscription 20 from .AdBlockSubscription import AdBlockSubscription
21 from .AdBlockUrlInterceptor import AdBlockUrlInterceptor
17 22
18 from Utilities.AutoSaver import AutoSaver 23 from Utilities.AutoSaver import AutoSaver
19 import Utilities 24 import Utilities
20 import Preferences 25 import Preferences
21 26
54 "title=EasyList" 59 "title=EasyList"
55 self.__customSubscriptionUrlString = \ 60 self.__customSubscriptionUrlString = \
56 bytes(self.__customSubscriptionUrl().toEncoded()).decode() 61 bytes(self.__customSubscriptionUrl().toEncoded()).decode()
57 62
58 self.rulesChanged.connect(self.__saveTimer.changeOccurred) 63 self.rulesChanged.connect(self.__saveTimer.changeOccurred)
64 self.rulesChanged.connect(self.__rulesChanged)
65
66 self.__interceptor = AdBlockUrlInterceptor(self)
67
68 from WebBrowser.WebBrowserWindow import WebBrowserWindow
69 WebBrowserWindow.networkManager().installUrlInterceptor(
70 self.__interceptor)
71
72 def __rulesChanged(self):
73 """
74 Private slot handling a change of the AdBlock rules.
75 """
76 from WebBrowser.WebBrowserWindow import WebBrowserWindow
77 WebBrowserWindow.mainWindow().reloadUserStyleSheet()
59 78
60 def close(self): 79 def close(self):
61 """ 80 """
62 Public method to close the open search engines manager. 81 Public method to close the open search engines manager.
63 """ 82 """
92 for mainWindow in WebBrowserWindow.mainWindows(): 111 for mainWindow in WebBrowserWindow.mainWindows():
93 mainWindow.adBlockIcon().setEnabled(enabled) 112 mainWindow.adBlockIcon().setEnabled(enabled)
94 if enabled: 113 if enabled:
95 self.__loadSubscriptions() 114 self.__loadSubscriptions()
96 self.rulesChanged.emit() 115 self.rulesChanged.emit()
97 ## 116
98 ## def network(self): 117 def block(self, info):
99 ## """ 118 """
100 ## Public method to get a reference to the network block object. 119 Public method to check, if a request should be blocked.
101 ## 120
102 ## @return reference to the network block object (AdBlockNetwork) 121 @param info request info aobject
103 ## """ 122 @type QWebEngineUrlRequestInfo
104 ## if self.__adBlockNetwork is None: 123 @return flag indicating to block the request
105 ## from .AdBlockNetwork import AdBlockNetwork 124 @rtype bool
106 ## self.__adBlockNetwork = AdBlockNetwork(self) 125 """
107 ## return self.__adBlockNetwork 126 urlString = bytes(info.requestUrl().toEncoded()).decode().lower()
127 urlDomain = info.requestUrl().host().lower()
128 urlScheme = info.requestUrl().scheme().lower()
129 refererHost = info.firstPartyUrl().host().lower()
130
131 if not self.isEnabled() or not self.__canRunOnScheme(urlScheme):
132 return False
133
134 if self.isHostExcepted(urlDomain) or self.isHostExcepted(refererHost):
135 return False
136
137 res = False
138
139 for subscription in self.subscriptions():
140 if subscription.isEnabled():
141 if subscription.adBlockDisabledForUrl(info.requestUrl()):
142 continue
143
144 blockedRule = subscription.match(info, urlDomain, urlString)
145 if blockedRule:
146 res = True
147 if info.resourceType() == \
148 QWebEngineUrlRequestInfo.ResourceTypeMainFrame:
149 url = QUrl("eric:adblock")
150 query = QUrlQuery()
151 query.addQueryItem("rule", blockedRule.filter())
152 query.addQueryItem(
153 "subscription", blockedRule.subscription().title())
154 url.setQuery(query)
155 info.redirect(url)
156 res = False
157 else:
158 info.block(True)
159 break
160
161 return res
162
163 def __canRunOnScheme(self, scheme):
164 """
165 Private method to check, if AdBlock can be performed on the scheme.
166
167 @param scheme scheme to check (string)
168 @return flag indicating, that AdBlock can be performed (boolean)
169 """
170 return scheme not in ["data", "eric", "qthelp", "qrc", "file", "abp"]
108 171
109 def page(self): 172 def page(self):
110 """ 173 """
111 Public method to get a reference to the page block object. 174 Public method to get a reference to the page block object.
112 175
215 self.removeSubscription(requiresSubscription, False) 278 self.removeSubscription(requiresSubscription, False)
216 if emitSignal: 279 if emitSignal:
217 self.rulesChanged.emit() 280 self.rulesChanged.emit()
218 except ValueError: 281 except ValueError:
219 pass 282 pass
283
284 def addSubscriptionFromUrl(self, url):
285 """
286 Public method to ad an AdBlock subscription given the abp URL:
287
288 @param url URL to subscribe an AdBlock subscription
289 @type QUrl
290 @return flag indicating success
291 @rtype bool
292 """
293 if url.path() != "subscribe":
294 return False
295
296 title = QUrl.fromPercentEncoding(
297 QByteArray(QUrlQuery(url).queryItemValue("title").encode()))
298 if not title:
299 return False
300
301 res = E5MessageBox.yesNo(
302 None,
303 self.tr("Subscribe?"),
304 self.tr(
305 """<p>Subscribe to this AdBlock subscription?</p>"""
306 """<p>{0}</p>""").format(title))
307 if res:
308 from .AdBlockSubscription import AdBlockSubscription
309 from WebBrowser.WebBrowserWindow import WebBrowserWindow
310
311 dlg = WebBrowserWindow.adBlockManager().showDialog()
312 subscription = AdBlockSubscription(
313 url, False,
314 WebBrowserWindow.adBlockManager())
315 WebBrowserWindow.adBlockManager().addSubscription(subscription)
316 dlg.addSubscription(subscription, False)
317 dlg.setFocus()
318 dlg.raise_()
220 319
221 def addSubscription(self, subscription): 320 def addSubscription(self, subscription):
222 """ 321 """
223 Public method to add an AdBlock subscription. 322 Public method to add an AdBlock subscription.
224 323
269 return 368 return
270 369
271 self.__loaded = True 370 self.__loaded = True
272 371
273 self.__enabled = Preferences.getWebBrowser("AdBlockEnabled") 372 self.__enabled = Preferences.getWebBrowser("AdBlockEnabled")
373 ## if (!m_enabled) {
374 ## mApp->networkManager()->removeUrlInterceptor(m_interceptor);
375 ## return;
376 ## }
274 if self.__enabled: 377 if self.__enabled:
275 self.__loadSubscriptions() 378 self.__loadSubscriptions()
379 ## mApp->networkManager()->installUrlInterceptor(m_interceptor);
276 380
277 def __loadSubscriptions(self): 381 def __loadSubscriptions(self):
278 """ 382 """
279 Private method to load the set of subscriptions. 383 Private method to load the set of subscriptions.
280 """ 384 """
360 self.__adBlockDialog = AdBlockDialog() 464 self.__adBlockDialog = AdBlockDialog()
361 465
362 self.__adBlockDialog.show() 466 self.__adBlockDialog.show()
363 return self.__adBlockDialog 467 return self.__adBlockDialog
364 468
365 def showRule(self):
366 """
367 Public slot to show an AdBlock rule.
368 """
369 act = self.sender()
370 if act is not None:
371 rule = act.data()
372 if rule:
373 self.showDialog().showRule(rule)
374
375 def elementHidingRules(self): 469 def elementHidingRules(self):
376 """ 470 """
377 Public method to get the element hiding rules. 471 Public method to get the element hiding rules.
378 472
379 @return element hiding rules (string) 473 @return element hiding rules (string)
380 """ 474 """
381 if not self.__enabled: 475 if not self.isEnabled():
382 return "" 476 return ""
383 477
384 rules = "" 478 rules = ""
385 479
386 for subscription in self.__subscriptions: 480 for subscription in self.__subscriptions:
397 Public method to get the element hiding rules for a domain. 491 Public method to get the element hiding rules for a domain.
398 492
399 @param url URL to get hiding rules for (QUrl) 493 @param url URL to get hiding rules for (QUrl)
400 @return element hiding rules (string) 494 @return element hiding rules (string)
401 """ 495 """
402 if not self.__enabled: 496 if not self.isEnabled():
403 return "" 497 return ""
404 498
405 rules = "" 499 rules = ""
406 500
407 for subscription in self.__subscriptions: 501 for subscription in self.__subscriptions:
430 """ 524 """
431 Public method to set the list of excepted hosts. 525 Public method to set the list of excepted hosts.
432 526
433 @param hosts list of excepted hosts (list of string) 527 @param hosts list of excepted hosts (list of string)
434 """ 528 """
435 self.__exceptedHosts = hosts[:] 529 self.__exceptedHosts = [host.lower() for host in hosts]
436 Preferences.setWebBrowser("AdBlockExceptions", self.__exceptedHosts) 530 Preferences.setWebBrowser("AdBlockExceptions", self.__exceptedHosts)
437 531
438 def addException(self, host): 532 def addException(self, host):
439 """ 533 """
440 Public method to add an exception. 534 Public method to add an exception.
441 535
442 @param host to be excepted (string) 536 @param host to be excepted (string)
443 """ 537 """
538 host = host.lower()
444 if host and host not in self.__exceptedHosts: 539 if host and host not in self.__exceptedHosts:
445 self.__exceptedHosts.append(host) 540 self.__exceptedHosts.append(host)
446 Preferences.setWebBrowser( 541 Preferences.setWebBrowser(
447 "AdBlockExceptions", self.__exceptedHosts) 542 "AdBlockExceptions", self.__exceptedHosts)
448 543
450 """ 545 """
451 Public method to remove an exception. 546 Public method to remove an exception.
452 547
453 @param host to be removed from the list of exceptions (string) 548 @param host to be removed from the list of exceptions (string)
454 """ 549 """
550 host = host.lower()
455 if host in self.__exceptedHosts: 551 if host in self.__exceptedHosts:
456 self.__exceptedHosts.remove(host) 552 self.__exceptedHosts.remove(host)
457 Preferences.setWebBrowser( 553 Preferences.setWebBrowser(
458 "AdBlockExceptions", self.__exceptedHosts) 554 "AdBlockExceptions", self.__exceptedHosts)
459 555
462 Public slot to check, if a host is excepted. 558 Public slot to check, if a host is excepted.
463 559
464 @param host host to check (string) 560 @param host host to check (string)
465 @return flag indicating an exception (boolean) 561 @return flag indicating an exception (boolean)
466 """ 562 """
563 host = host.lower()
467 return host in self.__exceptedHosts 564 return host in self.__exceptedHosts
468 565
469 def showExceptionsDialog(self): 566 def showExceptionsDialog(self):
470 """ 567 """
471 Public method to show the AdBlock Exceptions dialog. 568 Public method to show the AdBlock Exceptions dialog.

eric ide

mercurial