8 """ |
8 """ |
9 |
9 |
10 |
10 |
11 import os |
11 import os |
12 |
12 |
13 from PyQt5.QtCore import pyqtSignal, QObject, QUrl, QUrlQuery, QFile, \ |
13 from PyQt5.QtCore import ( |
14 QByteArray, QMutex, QMutexLocker |
14 pyqtSignal, QObject, QUrl, QUrlQuery, QFile, QByteArray, QMutex, |
|
15 QMutexLocker |
|
16 ) |
15 from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInfo |
17 from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInfo |
16 |
18 |
17 from E5Gui import E5MessageBox |
19 from E5Gui import E5MessageBox |
18 |
20 |
19 from .AdBlockSubscription import AdBlockSubscription |
21 from .AdBlockSubscription import AdBlockSubscription |
59 self.__exceptedHosts = Preferences.getWebBrowser("AdBlockExceptions") |
61 self.__exceptedHosts = Preferences.getWebBrowser("AdBlockExceptions") |
60 self.__saveTimer = AutoSaver(self, self.save) |
62 self.__saveTimer = AutoSaver(self, self.save) |
61 self.__limitedEasyList = Preferences.getWebBrowser( |
63 self.__limitedEasyList = Preferences.getWebBrowser( |
62 "AdBlockUseLimitedEasyList") |
64 "AdBlockUseLimitedEasyList") |
63 |
65 |
64 self.__defaultSubscriptionUrlString = \ |
66 self.__defaultSubscriptionUrlString = ( |
65 "abp:subscribe?location=" \ |
67 "abp:subscribe?location=" |
66 "https://easylist-downloads.adblockplus.org/easylist.txt&"\ |
68 "https://easylist-downloads.adblockplus.org/easylist.txt&" |
67 "title=EasyList" |
69 "title=EasyList" |
|
70 ) |
68 self.__additionalDefaultSubscriptionUrlStrings = ( |
71 self.__additionalDefaultSubscriptionUrlStrings = ( |
69 "abp:subscribe?location=https://raw.githubusercontent.com/" |
72 "abp:subscribe?location=https://raw.githubusercontent.com/" |
70 "hoshsadiq/adblock-nocoin-list/master/nocoin.txt&" |
73 "hoshsadiq/adblock-nocoin-list/master/nocoin.txt&" |
71 "title=NoCoin", |
74 "title=NoCoin", |
72 ) |
75 ) |
73 self.__customSubscriptionUrlString = \ |
76 self.__customSubscriptionUrlString = ( |
74 bytes(self.__customSubscriptionUrl().toEncoded()).decode() |
77 bytes(self.__customSubscriptionUrl().toEncoded()).decode() |
|
78 ) |
75 |
79 |
76 self.__mutex = QMutex() |
80 self.__mutex = QMutex() |
77 self.__matcher = AdBlockMatcher(self) |
81 self.__matcher = AdBlockMatcher(self) |
78 |
82 |
79 self.rulesChanged.connect(self.__saveTimer.changeOccurred) |
83 self.rulesChanged.connect(self.__saveTimer.changeOccurred) |
96 def close(self): |
100 def close(self): |
97 """ |
101 """ |
98 Public method to close the open search engines manager. |
102 Public method to close the open search engines manager. |
99 """ |
103 """ |
100 self.__adBlockDialog and self.__adBlockDialog.close() |
104 self.__adBlockDialog and self.__adBlockDialog.close() |
101 self.__adBlockExceptionsDialog and \ |
105 (self.__adBlockExceptionsDialog and |
102 self.__adBlockExceptionsDialog.close() |
106 self.__adBlockExceptionsDialog.close()) |
103 |
107 |
104 self.__saveTimer.saveIfNeccessary() |
108 self.__saveTimer.saveIfNeccessary() |
105 |
109 |
106 def isEnabled(self): |
110 def isEnabled(self): |
107 """ |
111 """ |
151 |
155 |
152 urlString = bytes(info.requestUrl().toEncoded()).decode().lower() |
156 urlString = bytes(info.requestUrl().toEncoded()).decode().lower() |
153 urlDomain = info.requestUrl().host().lower() |
157 urlDomain = info.requestUrl().host().lower() |
154 urlScheme = info.requestUrl().scheme().lower() |
158 urlScheme = info.requestUrl().scheme().lower() |
155 |
159 |
156 if not self.canRunOnScheme(urlScheme) or \ |
160 if ( |
157 not self.__canBeBlocked(info.firstPartyUrl()): |
161 not self.canRunOnScheme(urlScheme) or |
|
162 not self.__canBeBlocked(info.firstPartyUrl()) |
|
163 ): |
158 return False |
164 return False |
159 |
165 |
160 res = False |
166 res = False |
161 blockedRule = self.__matcher.match(info, urlDomain, urlString) |
167 blockedRule = self.__matcher.match(info, urlDomain, urlString) |
162 |
168 |
163 if blockedRule: |
169 if blockedRule: |
164 res = True |
170 res = True |
165 if info.resourceType() == \ |
171 if ( |
166 QWebEngineUrlRequestInfo.ResourceTypeMainFrame: |
172 info.resourceType() == |
|
173 QWebEngineUrlRequestInfo.ResourceTypeMainFrame |
|
174 ): |
167 url = QUrl("eric:adblock") |
175 url = QUrl("eric:adblock") |
168 query = QUrlQuery() |
176 query = QUrlQuery() |
169 query.addQueryItem("rule", blockedRule.filter()) |
177 query.addQueryItem("rule", blockedRule.filter()) |
170 query.addQueryItem( |
178 query.addQueryItem( |
171 "subscription", blockedRule.subscription().title()) |
179 "subscription", blockedRule.subscription().title()) |
414 if subscription.startswith(self.__customSubscriptionUrlString): |
422 if subscription.startswith(self.__customSubscriptionUrlString): |
415 break |
423 break |
416 else: |
424 else: |
417 subscriptions.append(self.__customSubscriptionUrlString) |
425 subscriptions.append(self.__customSubscriptionUrlString) |
418 else: |
426 else: |
419 subscriptions = [self.__defaultSubscriptionUrlString] + \ |
427 subscriptions = ( |
420 self.__additionalDefaultSubscriptionUrlStrings + \ |
428 [self.__defaultSubscriptionUrlString] + |
|
429 self.__additionalDefaultSubscriptionUrlStrings + |
421 [self.__customSubscriptionUrlString] |
430 [self.__customSubscriptionUrlString] |
|
431 ) |
422 for subscription in subscriptions: |
432 for subscription in subscriptions: |
423 url = QUrl.fromEncoded(subscription.encode("utf-8")) |
433 url = QUrl.fromEncoded(subscription.encode("utf-8")) |
424 adBlockSubscription = AdBlockSubscription( |
434 adBlockSubscription = AdBlockSubscription( |
425 url, |
435 url, |
426 subscription.startswith(self.__customSubscriptionUrlString), |
436 subscription.startswith(self.__customSubscriptionUrlString), |
502 @param url URL to get hiding rules for |
512 @param url URL to get hiding rules for |
503 @type QUrl |
513 @type QUrl |
504 @return element hiding rules |
514 @return element hiding rules |
505 @rtype str |
515 @rtype str |
506 """ |
516 """ |
507 if not self.isEnabled() or \ |
517 if ( |
508 not self.canRunOnScheme(url.scheme()) or \ |
518 not self.isEnabled() or |
509 not self.__canBeBlocked(url): |
519 not self.canRunOnScheme(url.scheme()) or |
|
520 not self.__canBeBlocked(url) |
|
521 ): |
510 return "" |
522 return "" |
511 |
523 |
512 return self.__matcher.elementHidingRules() |
524 return self.__matcher.elementHidingRules() |
513 |
525 |
514 def elementHidingRulesForDomain(self, url): |
526 def elementHidingRulesForDomain(self, url): |
518 @param url URL to get hiding rules for |
530 @param url URL to get hiding rules for |
519 @type QUrl |
531 @type QUrl |
520 @return element hiding rules |
532 @return element hiding rules |
521 @rtype str |
533 @rtype str |
522 """ |
534 """ |
523 if not self.isEnabled() or \ |
535 if ( |
524 not self.canRunOnScheme(url.scheme()) or \ |
536 not self.isEnabled() or |
525 not self.__canBeBlocked(url): |
537 not self.canRunOnScheme(url.scheme()) or |
|
538 not self.__canBeBlocked(url) |
|
539 ): |
526 return "" |
540 return "" |
527 |
541 |
528 return self.__matcher.elementHidingRulesForDomain(url.host()) |
542 return self.__matcher.elementHidingRulesForDomain(url.host()) |
529 |
543 |
530 def exceptions(self): |
544 def exceptions(self): |