1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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])
|