diff options
author | Urbain Vaes <urbain@vaes.uk> | 2015-08-11 15:58:37 +0200 |
---|---|---|
committer | Urbain Vaes <urbain@vaes.uk> | 2015-08-11 15:58:37 +0200 |
commit | 5e17418f1eef06e97bb621edf53530437df8b14f (patch) | |
tree | f87466982929a0d0fead9ec97a5e9b017c76bf9e /.uzbl/scripts/adblock.py | |
parent | 8df9472e0cdbab0d12211c2bc77918e8f515c409 (diff) |
Back up several files
Diffstat (limited to '.uzbl/scripts/adblock.py')
-rwxr-xr-x | .uzbl/scripts/adblock.py | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/.uzbl/scripts/adblock.py b/.uzbl/scripts/adblock.py new file mode 100755 index 0000000..aae39f4 --- /dev/null +++ b/.uzbl/scripts/adblock.py @@ -0,0 +1,86 @@ +#!/usr/bin/python +import os +from sys import argv +from urlparse import urlparse + +# This is the original adblock.py script from http://www.uzbl.org/wiki/adblock +# Nothing was modified. This script is here just to have everything at one place. + +def xdghome(key, default): + '''Attempts to use the environ XDG_*_HOME paths if they exist otherwise + use $HOME and the default path.''' + + xdgkey = "XDG_%s_HOME" % key + if xdgkey in os.environ.keys() and os.environ[xdgkey]: + return os.environ[xdgkey] + + return os.path.join(os.environ['HOME'], default) + +# Setup xdg paths. +DATA_DIR = os.path.join(xdghome('DATA', '.local/share/'), 'uzbl/') + +# Blockfile location. +BLOCKFILE = os.path.join(DATA_DIR, 'adblock') + +JAVASCRIPT = ' '.join(filter(None, map(str.strip, ''' +var uzblAdBlock = function() { + var toblock = %s; + for(var n = 0; n < toblock.length; n++) { + var items; + while (1) { + try { + items = document.evaluate(toblock[n], document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); + if (items == null) { break; } + var i = items.iterateNext(); + if (i == null) { break; } + i.parentNode.removeChild(i); + } catch (e) { + break; + } + } + } +}; +'''.split('\n')))) + + +def get_domain(url): + '''Return domain segment of url.''' + + if not url.startswith('http'): + url = "http://%s" % url + + loc = urlparse(url).netloc + if loc.startswith('www.'): + loc = loc[4:] + + return loc + + +def adblock(url, fifo): + fh = open(BLOCKFILE, 'r') + lines = [line.strip() for line in fh.readlines()] + fh.close() + + rules, capture = [], False + for l in lines: + if not l: # newline splits section + capture = False + + elif l[0] == '#': + continue + + elif capture: + rules.append(l) + + elif l[-1] == ':': + if get_domain(l[:-1]) == url or l[:-1] == "global": + capture = True + + rulestr = repr(rules).replace("@", "\@") + js = "js %s\n" % (JAVASCRIPT % rulestr) + fh = open(fifo, "w") + fh.write(js) + fh.close() + +if __name__ == '__main__': + adblock(get_domain(argv[6]), argv[4]) |