emacs-eaf / eaf-browser

A modern, customizable and extensible browser in Emacs
GNU General Public License v3.0
129 stars 26 forks source link

adblock #51

Closed manateelazycat closed 1 year ago

manateelazycat commented 1 year ago

Below is adblock code, but it will block main thread after add, don't know why.

self.interceptor = AdBlockInterceptor(self.profile, self)

with open(os.path.join(os.path.dirname(__file__), "easylistchina+easylist.txt")) as f:
    raw_rules = f.readlines()
    ad_rules = AdblockRules(raw_rules, use_re2=True)

class AdBlockInterceptor(QWebEngineUrlRequestInterceptor):
    def __init__(self, profile, buffer):
        QWebEngineUrlRequestInterceptor.__init__(self)
        profile.setUrlRequestInterceptor(self)
        self.buffer = buffer

    def interceptRequest(self, info):
        # Ad Test site:
        # https://d3ward.github.io/toolz/adblock.html

        if self.buffer.enable_adblocker:
            url = info.requestUrl().toString()
            if ad_rules.should_block(url):
                print("Block Ad: ", url)
                info.block(True)