Add adblock host whitelisting

The config option "content host-blocking-whitelist" may contain comma
separated domains that are exempt from host blocking.

The listed domains may contain the wildcards "*" and "?" to match many
and one character, respectively.

You need to run :adblock-update after modifying the list.
This commit is contained in:
Daniel 2015-09-16 16:31:17 +02:00
parent 0fa9da56bf
commit ccdb59cce1
3 changed files with 41 additions and 1 deletions

View File

@ -159,6 +159,7 @@
|<<content-cookies-store,cookies-store>>|Whether to store cookies.
|<<content-host-block-lists,host-block-lists>>|List of URLs of lists which contain hosts to block.
|<<content-host-blocking-enabled,host-blocking-enabled>>|Whether host blocking is enabled.
|<<content-host-blocking-whitelist,host-blocking-whitelist>>|List of domains that should always be loaded, despite being ad-blocked.
|==============
.Quick reference for section ``hints''
@ -1433,6 +1434,16 @@ Valid values:
Default: +pass:[true]+
[[content-host-blocking-whitelist]]
=== host-blocking-whitelist
List of domains that should always be loaded, despite being ad-blocked.
Domains may contain * and ? wildcards and are otherwise required to exactly match the requested domain.
Local domains are always exempt from hostblocking.
Default: empty
== hints
Hinting settings.

View File

@ -24,6 +24,8 @@ import os.path
import functools
import posixpath
import zipfile
import fnmatch
import re
from qutebrowser.config import config
from qutebrowser.utils import objreg, standarddir, log, message
@ -59,6 +61,24 @@ def get_fileobj(byte_io):
return io.TextIOWrapper(byte_io, encoding='utf-8')
def is_whitelisted_domain(host):
"""Check if the given host is on the adblock whitelist.
Args:
host: The host as given by the adblocker as string.
"""
whitelist = objreg.get('config').get('content', 'host-blocking-whitelist')
if whitelist is None:
return False
for domain in whitelist:
fnmatch_translated = fnmatch.translate(domain)
domain_regex = re.compile(fnmatch_translated, re.IGNORECASE)
if domain_regex.match(host):
return True
return False
class FakeDownload:
"""A download stub to use on_download_finished with local files."""
@ -188,7 +208,8 @@ class HostBlocker:
else:
error_count += 1
continue
if host not in self.WHITELISTED:
if (host not in self.WHITELISTED
and not is_whitelisted_domain(host)):
self.blocked_hosts.add(host)
log.misc.debug("{}: read {} lines".format(byte_io.name, line_count))
if error_count > 0:

View File

@ -735,6 +735,14 @@ def data(readonly=False):
SettingValue(typ.Bool(), 'true'),
"Whether host blocking is enabled."),
('host-blocking-whitelist',
SettingValue(typ.List(none_ok=True), ''),
"List of domains that should always be loaded, despite being "
"ad-blocked.\n\n"
"Domains may contain * and ? wildcards and are otherwise "
"required to exactly match the requested domain.\n\n"
"Local domains are always exempt from hostblocking."),
readonly=readonly
)),