LeechBlock in qutebrowser

7 July 2022

I use qutebrowser as my daily driver. With the exception of the errant ad that Firefox or Chrome would catch,An active area of research. there is nothing to complain about.

Unfortunately I’ve developed a nasty habit of unthinkingly typing in anxiogenic websites. The usual solution on Chrome/Firefox is LeechBlock, but qutebrowser naturally doesn’t support extensions.I’m honestly fine with this design choice; most of the extensions I would need are implementable as user stylesheets/scripts/plugins, this one included. Keep it simple, S.

Fortunately, it does have an interface to intercept outgoing requests. Even more fortunately, Jay Kamat wrote most of the plumbing for what I need.

The following is graciously adapted from its code:

from typing import Literal

from qutebrowser.api import interceptor
from PyQt5.QtCore import QUrl

BREATHER = [
    # Sites to block here...
]


def redirect_to(qurl: QUrl) -> QUrl | Literal[False]:
    """Return the URL to which `qurl` should be redirected,
    or False if no redirect is to be performed.
    """
    url = qurl.url()

    if any(domain in url for domain in BREATHER):
        return QUrl("https://sverona.dev/breather/")

    return False


def intercept(req: interceptor.Request):
    if (req.resource_type != interceptor.ResourceType.main_frame or
            req.request_url.scheme() in {"data", "blob"}):
        return

    url = req.request_url

    redir = redirect_to(url)
    if redir:
        req.redirect(redir)


interceptor.register(intercept)

I have this in ~/.config/qutebrowser/pyconfig/block.py.

May your mental health never falter.