summaryrefslogtreecommitdiff
path: root/tools/grab_adblocker
blob: 4f750e0a7f484820259bec7d2e4027a7e75de2d8 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash 

# Filterlist, uncomment to download lists, mutliple lists are supported, but
# don't choose too many lists since it will slow down the adblocker
URLS=(

# Easylist
# Easylist English
#https://easylist-downloads.adblockplus.org/easylist.txt

# Easylist Privacy, blocks tracking
#https://easylist-downloads.adblockplus.org/easyprivacy.txt

# Easylist Without element hiding
#https://easylist-downloads.adblockplus.org/easylist_noelemhide.txt

# Easylist additional subscriptions
# Easylist Germany
#https://easylist-downloads.adblockplus.org/easylistgermany.txt

# Easylist Italy
#https://easylist-downloads.adblockplus.org/easylistitaly.txt

# Easylist Dutch
#http://dutchadblockfilters.googlecode.com/svn/trunk/AdBlock_Dutch_hide.txt

# Easylist French
#http://lian.info.tm/liste_fr.txt

# Easylist China
#http://adblock-chinalist.googlecode.com/svn/trunk/adblock.txt

# Easylist Bulgaria
#http://stanev.org/abp/adblock_bg.txt

# Easylist Indonesia
#http://indonesianadblockrules.googlecode.com/hg/subscriptions/abpindo.txt

# Easylist Finland
#http://www.wiltteri.net/wiltteri.txt

# Easylist Greece 
#http://www.void.gr/kargig/void-gr-filters.txt

# Adversity
# Adversity English list
#https://adversity.googlecode.com/hg/Adversity.txt  

# Adversity Privacy
#https://adversity.googlecode.com/hg/Adversity-Tracking.txt

# Fanboy
# Fanboy English list
#http://www.fanboy.co.nz/adblock/fanboy-adblock.txt

# Fanboy Tracking list
#http://www.fanboy.co.nz/adblock/fanboy-tracking.txt

# Antisocial
#https://adversity.googlecode.com/hg/Antisocial.txt
)


DESCRIPTION_INTERACTIVE=(
"Easylist English"
"Easylist privacy, blocks tracking"
"Easylist without element hiding"
"Easylist Germany"
"Easylist Italy"
"Easylist Dutch"
"Easylist French"
"Easylist China"
"Easylist Bulgaria"
"Easylist Indonesia"
"Easylist Finland"
"Easylist Greece"
"Adversity English list"
"Adversity Privacy"
"Fanboy English list"
"Fanboy Tracking list"
)
URLS_INTERACTIVE=(
https://easylist-downloads.adblockplus.org/easylist.txt
https://easylist-downloads.adblockplus.org/easyprivacy.txt
https://easylist-downloads.adblockplus.org/easylist_noelemhide.txt
https://easylist-downloads.adblockplus.org/easylistgermany.txt
https://easylist-downloads.adblockplus.org/easylistitaly.txt
http://dutchadblockfilters.googlecode.com/svn/trunk/AdBlock_Dutch_hide.txt
http://lian.info.tm/liste_fr.txt
http://adblock-chinalist.googlecode.com/svn/trunk/adblock.txt
http://stanev.org/abp/adblock_bg.txt
http://indonesianadblockrules.googlecode.com/hg/subscriptions/abpindo.txt
http://www.wiltteri.net/wiltteri.txt
http://www.void.gr/kargig/void-gr-filters.txt
https://adversity.googlecode.com/hg/Adversity.txt  
https://adversity.googlecode.com/hg/Adversity-Tracking.txt
http://www.fanboy.co.nz/adblock/fanboy-adblock.txt
http://www.fanboy.co.nz/adblock/fanboy-tracking.txt
)

# General not supported filterlists
_USP="object-subrequest|ping|xbl|xmlhttprequest|dtd|elemhide|other|collapse|donottrack|popup"
# Only remove rules, not exceptions
UNSUPPORTED="(^@@.*[\$,]~(${_USP})($|,))"
UNSUPPORTED+="|(^[^@].*[\$,](${_USP})($|,))"


declare PROFILE
# Parse settings
CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}"
CONFIG="${CONFIG_DIR}/dwb/settings"

while read; do 
  if [[ ${REPLY} =~ ^\[ ]]; then 
    PROFILE=${REPLY:1:$((${#REPLY}-2))}
  fi
  if [ "${PROFILE}" = "default" ]  && [[ ${REPLY} =~ ^adblocker-filterlist ]]; then 
    DEST=${REPLY//*=/}
    break
  fi
done < ${CONFIG}

if [ ! ${DEST} ]; then 
  if ! test -z $(pgrep -U $(id -u) dwb); then 
    cat << ! 
You haven't set 'adblocker-filterlist' in dwb yet. You can either set the path
in dwb or close all instances of dwb and run this script again.
!
    exit 1
  fi
  DEST=${CONFIG_DIR}/dwb/adblock_default
  echo "No setting 'adblocker-filterlist' found for profile default, using '${DEST}'"
  sed -i "0,/adblocker-filterlist/s#^adblocker-filterlist=.*#adblocker-filterlist=${DEST}#" ${CONFIG}
fi

echo -e "The filterlist will be saved as ${DEST}\n"

if [ -e  ${DEST} ]; then 
  rm ${DEST}
fi

AFTER=0
BEFORE=0
REMOVED=0
TOTAL=0

get_list() {
  local TMP=$(mktemp)
  echo "Grabbing ${1}"
  wget -O ${TMP} ${1} &>/dev/null
  BEFORE=( $(wc -l "${TMP}") )

  ((BEFORE+=TOTAL))
  sed -r "/${UNSUPPORTED}/d" "${TMP}" >> ${DEST}
  rm ${TMP}
  AFTER=( $(wc -l "${DEST}") )
  ((REMOVED=BEFORE-AFTER))
  TOTAL=${BEFORE}
}

# Download the filterlists
if [ $# -gt 0 ]; then 
  URLS=$@
fi
if [ ${#URLS[@]} -gt 0 ]; then 
  for URL in ${URLS[@]}; do 
    get_list ${URL}
  done
else 
  OLDPS3=$PS3
  echo -e "Choose filterlists:\n"
  PS3="Comma-separated list: "
  select blub in "${DESCRIPTION_INTERACTIVE[@]}"; do 
    echo
    REPURL=( ${REPLY//,/ } )
    for n in ${REPURL[@]}; do 
      if [ ! ${URLS_INTERACTIVE[$((n-1))]} ]; then
        echo "Choose appropriate numbers next time."
        exit 1
      fi
    done
    for n in ${REPURL[@]}; do
      get_list ${URLS_INTERACTIVE[$((n-1))]}
    done
    break
  done
  PS3=${OLDPS3}
fi

echo "Removed ${REMOVED} unsupported of ${BEFORE} filters."
echo "Removing comments."
sed -i "/^[!\[]/d" "${DEST}"
echo "Done."