from JAK.Utils import check_url_rules, bindings
if bindings() == "PyQt5":
from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInterceptor, QWebEngineUrlRequestInfo
else:
from PySide2.QtWebEngineCore import QWebEngineUrlRequestInterceptor, QWebEngineUrlRequestInfo
class Interceptor(QWebEngineUrlRequestInterceptor):
def __init__(self, config):
self.config = config
super(Interceptor, self).__init__()
def interceptRequest(self, info) -> None:
if self.config['webview']["urlRules"] is not None:
If we have any URL’s in the block dictionary
url = info.requestUrl().toString()
try:
if check_url_rules("Block", url, self.config['webview']["urlRules"]["block"]):
block url’s
info.block(True)
print(f"Blocked:{url}")
except KeyError:
pass
if self.config["debug"]:
url = info.requestUrl().toString()
resource = info.resourceType()
if resource == QWebEngineUrlRequestInfo.ResourceType.ResourceTypeMainFrame:
print(f"Intercepted link:{url}")
elif resource != QWebEngineUrlRequestInfo.ResourceType.ResourceTypeMainFrame:
print(f"Intercepted resource:{url}")