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-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-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-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'' .Quick reference for section ``hints''
@ -1433,6 +1434,16 @@ Valid values:
Default: +pass:[true]+ 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 == hints
Hinting settings. Hinting settings.

View File

@ -24,6 +24,8 @@ import os.path
import functools import functools
import posixpath import posixpath
import zipfile import zipfile
import fnmatch
import re
from qutebrowser.config import config from qutebrowser.config import config
from qutebrowser.utils import objreg, standarddir, log, message 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') 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: class FakeDownload:
"""A download stub to use on_download_finished with local files.""" """A download stub to use on_download_finished with local files."""
@ -188,7 +208,8 @@ class HostBlocker:
else: else:
error_count += 1 error_count += 1
continue continue
if host not in self.WHITELISTED: if (host not in self.WHITELISTED
and not is_whitelisted_domain(host)):
self.blocked_hosts.add(host) self.blocked_hosts.add(host)
log.misc.debug("{}: read {} lines".format(byte_io.name, line_count)) log.misc.debug("{}: read {} lines".format(byte_io.name, line_count))
if error_count > 0: if error_count > 0:

View File

@ -735,6 +735,14 @@ def data(readonly=False):
SettingValue(typ.Bool(), 'true'), SettingValue(typ.Bool(), 'true'),
"Whether host blocking is enabled."), "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 readonly=readonly
)), )),