10 from __future__ import unicode_literals |
10 from __future__ import unicode_literals |
11 |
11 |
12 import re |
12 import re |
13 |
13 |
14 from PyQt5.QtCore import Qt, QRegExp |
14 from PyQt5.QtCore import Qt, QRegExp |
15 from PyQt5.QtNetwork import QNetworkRequest |
|
16 from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInfo |
15 from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInfo |
17 |
16 |
18 |
17 |
19 def toSecondLevelDomain(url): |
18 def toSecondLevelDomain(url): |
20 """ |
19 """ |
83 self.__scriptException = False |
82 self.__scriptException = False |
84 self.__stylesheet = False |
83 self.__stylesheet = False |
85 self.__stylesheetException = False |
84 self.__stylesheetException = False |
86 self.__objectSubrequest = False |
85 self.__objectSubrequest = False |
87 self.__objectSubrequestException = False |
86 self.__objectSubrequestException = False |
|
87 self.__stringMatchRule = False |
88 |
88 |
89 self.setFilter(filter) |
89 self.setFilter(filter) |
90 |
90 |
91 def subscription(self): |
91 def subscription(self): |
92 """ |
92 """ |
249 return |
249 return |
250 |
250 |
251 # no regexp required |
251 # no regexp required |
252 self.__useRegExp = False |
252 self.__useRegExp = False |
253 self.__matchString = parsedLine |
253 self.__matchString = parsedLine |
|
254 self.__stringMatchRule = True |
254 |
255 |
255 def __parseDomains(self, domains, separator): |
256 def __parseDomains(self, domains, separator): |
256 """ |
257 """ |
257 Private method to parse a string with a domain list. |
258 Private method to parse a string with a domain list. |
258 |
259 |
279 @param request reference to the network request |
280 @param request reference to the network request |
280 @type QWebEngineUrlRequestInfo |
281 @type QWebEngineUrlRequestInfo |
281 @param domain domain name |
282 @param domain domain name |
282 @type str |
283 @type str |
283 @param encodedUrl string encoded URL to be checked |
284 @param encodedUrl string encoded URL to be checked |
|
285 @type str |
|
286 @return flag indicating a match |
|
287 @rtype bool |
|
288 """ |
|
289 if self.__cssRule or not self.__enabled or self.__internalDisabled: |
|
290 return False |
|
291 |
|
292 matched = self.__stringMatch(domain, encodedUrl) |
|
293 |
|
294 if matched: |
|
295 # check domain restrictions |
|
296 if self.__domainRestricted and \ |
|
297 not self.matchDomain(request.firstPartyUrl().host()): |
|
298 return False |
|
299 |
|
300 # check third-party restrictions |
|
301 if self.__thirdParty and not self.matchThirdParty(request): |
|
302 return False |
|
303 |
|
304 # check object restrictions |
|
305 if self.__object and not self.matchObject(request): |
|
306 return False |
|
307 |
|
308 # check subdocument restrictions |
|
309 if self.__subdocument and not self.matchSubdocument(request): |
|
310 return False |
|
311 |
|
312 # check xmlhttprequest restriction |
|
313 if self.__xmlhttprequest and not self.matchXmlHttpRequest(request): |
|
314 return False |
|
315 |
|
316 # check image restriction |
|
317 if self.__image and not self.matchImage(request): |
|
318 return False |
|
319 |
|
320 # check script restriction |
|
321 if self.__script and not self.matchScript(request): |
|
322 return False |
|
323 |
|
324 # check stylesheet restriction |
|
325 if self.__stylesheet and not self.matchStyleSheet(request): |
|
326 return False |
|
327 |
|
328 # check object-subrequest restriction |
|
329 if self.__objectSubrequest and \ |
|
330 not self.matchObjectSubrequest(request): |
|
331 return False |
|
332 |
|
333 return matched |
|
334 |
|
335 def urlMatch(self, url): |
|
336 """ |
|
337 Public method to check an URL against the rule. |
|
338 |
|
339 @param url URL to check (QUrl) |
|
340 @return flag indicating a match (boolean) |
|
341 """ |
|
342 if not self.__document and not self.__elemhide: |
|
343 return False |
|
344 |
|
345 encodedUrl = bytes(url.toEncoded()).decode() |
|
346 domain = url.host() |
|
347 return self.__stringMatch(domain, encodedUrl) |
|
348 |
|
349 def __stringMatch(self, domain, encodedUrl): |
|
350 """ |
|
351 Private method to match a domain string. |
|
352 |
|
353 @param domain domain to match |
|
354 @type str |
|
355 @param encodedUrl URL in encoded form |
284 @type str |
356 @type str |
285 @return flag indicating a match |
357 @return flag indicating a match |
286 @rtype bool |
358 @rtype bool |
287 """ |
359 """ |
288 if self.__cssRule or not self.__enabled or self.__internalDisabled: |
360 if self.__cssRule or not self.__enabled or self.__internalDisabled: |
304 if self.__caseSensitivity == Qt.CaseInsensitive: |
376 if self.__caseSensitivity == Qt.CaseInsensitive: |
305 matched = self.__matchString.lower() in encodedUrl.lower() |
377 matched = self.__matchString.lower() in encodedUrl.lower() |
306 else: |
378 else: |
307 matched = self.__matchString in encodedUrl |
379 matched = self.__matchString in encodedUrl |
308 |
380 |
309 if matched: |
|
310 # check domain restrictions |
|
311 if self.__domainRestricted and \ |
|
312 not self.matchDomain(request.firstPartyUrl().host()): |
|
313 return False |
|
314 |
|
315 # check third-party restrictions |
|
316 if self.__thirdParty and not self.matchThirdParty(request): |
|
317 return False |
|
318 |
|
319 # check object restrictions |
|
320 if self.__object and not self.matchObject(request): |
|
321 return False |
|
322 |
|
323 # check subdocument restrictions |
|
324 if self.__subdocument and not self.matchSubdocument(request): |
|
325 return False |
|
326 |
|
327 # check xmlhttprequest restriction |
|
328 if self.__xmlhttprequest and not self.matchXmlHttpRequest(request): |
|
329 return False |
|
330 |
|
331 # check image restriction |
|
332 if self.__image and not self.matchImage(request): |
|
333 return False |
|
334 |
|
335 # check script restriction |
|
336 if self.__script and not self.matchScript(request): |
|
337 return False |
|
338 |
|
339 # check stylesheet restriction |
|
340 if self.__stylesheet and not self.matchStyleSheet(request): |
|
341 return False |
|
342 |
|
343 # check object-subrequest restriction |
|
344 if self.__objectSubrequest and \ |
|
345 not self.matchObjectSubrequest(request): |
|
346 return False |
|
347 |
|
348 return matched |
381 return matched |
349 |
|
350 def urlMatch(self, url): |
|
351 """ |
|
352 Public method to check an URL against the rule. |
|
353 |
|
354 @param url URL to check (QUrl) |
|
355 @return flag indicating a match (boolean) |
|
356 """ |
|
357 if not self.__document and not self.__elemhide: |
|
358 return False |
|
359 |
|
360 encodedUrl = bytes(url.toEncoded()).decode() |
|
361 domain = url.host() |
|
362 return self.networkMatch(QNetworkRequest(url), domain, encodedUrl) |
|
363 |
382 |
364 def matchDomain(self, domain): |
383 def matchDomain(self, domain): |
365 """ |
384 """ |
366 Public method to match a domain. |
385 Public method to match a domain. |
367 |
386 |