I'm making a web browser using Python and PySide6. I wanted to set up adblocking on this browser. I know it has to be done with a request interceptor, but I cannot figure out how to do it.
I have some code that doesn't work like it should:
from adblockparser import AdblockRules
with open("easylist.txt") as f:
raw = f.readlines()
rules = AdblockRules(raw)
class WebEngineUrlRequestInterceptor():
def intercept(self, info):
url = info.requestUrl().toString()
if rules.should_block(url):
print("block::::::::::::::::::::::", url)
info.block(True)
interceptor = WebEngineUrlRequestInterceptor()
QtWebEngineWidgets.QWebEngineProfile.defaultProfile().setRequestInterceptor(interceptor)
Could anyone link me up to some resources or help me out by editing my code?
I tried reading the docs and edited the code multiple times but it didn't work.
I expect the code to intercept requests to any of the 'ad sites' mentioned in easylist.txt