forked from skroll/privoxy-adblock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
privoxy-adblock.js
executable file
·295 lines (272 loc) · 11 KB
/
privoxy-adblock.js
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* @fileOverview converting Adblock filter lists to Privoxy format.
* @author <a href="[email protected]">James Edward Lewis II</a>
* @version 0.0.3
*/
var v = new ActiveXObject('Shell.Application'),
http = new ActiveXObject('Microsoft.XMLHTTP'),
fso = new ActiveXObject('Scripting.FileSystemObject'),
env = new ActiveXObject('WScript.Shell'), testing,
tmp = env.expandEnvironmentStrings('%TEMP%'),
prg32 = env.ExpandEnvironmentStrings('%PROGRAMFILES(X86)%'),
defaultprivoxydir = (fso.FolderExists(prg32) ? prg32 :
env.ExpandEnvironmentStrings('%PROGRAMFILES%')) + '\\Privoxy',
defaulturls = ['http://jansal.net/m.txt',
'http://pgl.yoyo.org/as/serverlist.php?hostformat=adblockplus&mimetype=plaintext',
'https://easylist-downloads.adblockplus.org/easylist.txt',
'https://easylist-downloads.adblockplus.org/easylist_noelemhide.txt',
'https://easylist-downloads.adblockplus.org/easylistgermany+easylist.txt',
'https://easylist-downloads.adblockplus.org/malwaredomains_full.txt',
'https://easylist-downloads.adblockplus.org/easyprivacy.txt',
'https://easylist-downloads.adblockplus.org/easyprivacy+easylist.txt',
'https://easylist-downloads.adblockplus.org/antiadblockfilters.txt',
'https://easylist-downloads.adblockplus.org/fb_annoyances_full.txt',
'https://easylist-downloads.adblockplus.org/message_seen_remover_for_facebook.txt',
'https://easylist-downloads.adblockplus.org/fanboy-annoyance.txt',
'https://easylist-downloads.adblockplus.org/fanboy-social.txt',
'https://secure.fanboy.co.nz/r/fanboy-complete.txt',
'https://secure.fanboy.co.nz/r/fanboy-ultimate.txt',
'https://secure.fanboy.co.nz/enhancedstats.txt',
'https://raw.githubusercontent.com/r4vi/block-the-eu-cookie-shit-list/master/filterlist.txt',
'https://raw.githubusercontent.com/liamja/Prebake/master/obtrusive.txt',
'https://raw.githubusercontent.com/reek/anti-adblock-killer/master/anti-adblock-killer-filters.txt',
'https://lists.malwarepatrol.net/cgi/getfile?receipt=f1379438547&product=8&list=mozilla_adblock',
'https://spam404bl.com/spam404scamlist.txt',
'http://rickrolldb.com/ricklist.txt'];
/**
* Cleans up after script termination. (not currently implemented)
* @this {undefined}
* @return {undefined}
*/
function cleanup() {
'use strict';
/*
trap - INT TERM EXIT
[[ -f "${pidfile}" ]] && rm "$pidfile"
exit
*/
}
/**
* Is any previous instance of this script already running?
* (not currently implemented)
* @this {undefined}
* @param {string} pidfile
* @return {boolean}
*/
function isrunning(pidfile) {
'use strict';
if (!fso.FileExists(pidfile)) return false;
/*
procpid=$(<"${pidfile}")
[[ -z "${procpid}" ]] && return 1 #pid file contains no pid
# check process list for pid existence and is an instance of this script
[[ ! $(ps -p ${procpid} | grep $(basename ${0})) == "" ]] && value=0 || value=1
return ${value}
*/
}
/**
* Atomic creation of pid file with no race condition. (not currently implemented)
* @this {undefined}
* @param {string} mypid
* @param {string} pidfile
* @return {undefined}
*/
function createpidfile(mypid, pidfile) {
'use strict';
/*
#Close stderr, don't overwrite existing file, shove my pid in the lock file.
$(exec 2>&-; set -o noclobber; echo "$mypid" > "$pidfile")
[[ ! -f "${pidfile}" ]] && exit #Lock file creation failed
procpid=$(<"${pidfile}")
[[ ${mypid} -ne ${procpid} ]] && {
#I'm not the pid in the lock file
# Is the process pid in the lockfile still running?
isrunning "${pidfile}" || {
# No. Kill the pidfile and relaunch
rm "${pidfile}"
$0 $@
}
exit
}
*/
}
/**
* Create a predictable pid file name, put it in the right inode.
* (not currently implemented)
* @this {undefined}
* @return {string} mypidfile
*/
function pidfilename() {
'use strict';
/*
myfile=$(basename "$0" .sh)
whoiam=$(whoami)
mypidfile="/tmp/${myfile}.pid"
[[ "$whoiam" == 'root' ]] && mypidfile="/var/run/$myfile.pid"
echo $mypidfile
*/
}
/**
* Returns the filename corresponding to a filesystem path or URI.
* @this {undefined}
* @param {string} path
* @return {string}
*/
function basename(path) {
'use strict';
return path.replace(/\\/g, '/').replace(/.*\//, '').replace(/[#?].*/, '');
}
/**
* Download requested scripts and perform the conversion.
* @this {undefined}
* @param {string} privoxydir
* @param {Array} urls
* @return {undefined}
*/
function doconvert(privoxydir, urls) {
'use strict';
var url, file, actionfile, actionfiledest, filterfile, filterfiledest, i, s,
tfil, acnm, ftnm, list, basenm, hrt, l = urls.length;
for (i = l; i > 0; i--) {
url = urls[l - i];
basenm = basename(url);
list = basenm.replace(/\.[^.]*$/, '') || basenm;
file = tmp + '\\' + list + '.txt';
tfil = file + '.tmp';
acnm = list + '.script.action';
ftnm = list + '.script.filter';
actionfile = tmp + '\\' + acnm;
filterfile = tmp + '\\' + ftnm;
testing && WScript.Echo('downloading ' + url + ' ...\n');
http.open('GET', url, false);
http.setRequestHeader('Accept','text/html');
try {
http.send();
if (fso.FileExists(tfil)) fso.GetFile(tfil).Delete();
if (hrt = http.responseText) s = fso.CreateTextFile(tfil, true);
} catch (e) { }
if (('' + s) && typeof hrt === 'string') {
s.Write(hrt);
s.Close();
} else {
if (typeof s === 'object') s.Close();
if (fso.FileExists(tfil)) fso.GetFile(tfil).Delete();
}
/* wget removed in favor of native solution, next up, factor out sed
v.ShellExecute('wget', '-t 3 --no-check-certificate -0 "' + file + '" "' + url +
'" > "' + tmp + '\\wget-' + url.replace(/\//g,'-') + '.log"', '', '', 0);*/
// clean up files
if (fso.FileExists(tfil) && fso.FileExists(file)) {
fso.GetFile(file).Delete();
fso.MoveFile(tfil, file);
}
fso.FileExists(actionfile) && fso.GetFile(actionfile).Delete();
fso.FileExists(filterfile) && fso.GetFile(filterfile).Delete();
if (!fso.FileExists(file)) continue;
s = fso.OpenTextFile(file, 1);
if (!s.ReadLine().match(/^.*\[Adblock.*\].*$/)) {
testing && WScript.Echo('The list received from ' + url + ' isn\'t an Adblock list. (skipped)\n');
s.Close();
continue;
}
s.Close();
testing && WScript.Echo('Creating actionfile for ' + list + ' ...\n');
s = fso.CreateTextFile(actionfile, true);
s.WriteLine('{ +block{' + list + '} }');
s.Close();
env.run('cmd /c sed "/^!.*/d;1,1 d;/^@@.*/d;/\\$.*/d;/#/d;s/\\./\\\\./g;s/\\?/\\\\?/g;s/\\*/.*/g;s/(/\\\\(/g;s/)/\\\\)/g;s/\\[/\\\\[/g;s/\\]/\\\\]/g;s/\\^/[\\/\\&:\\?=_]/g;s/^||/\\./g;s/^|/^/g;s/|$/\\$/g;/|/d" "' +
file + '" >> "' + actionfile + '"', 0, true);
testing && WScript.Echo('... creating filterfile for ' + list + ' ...\n');
s = fso.CreateTextFile(filterfile, true);
s.WriteLine('FILTER: ' + list + ' Tag filter of ' + list);
s.Close();
env.run('cmd /c sed "/^#/!d;s/^##//g;s/^#\\(.*\\)\\[.*\\]\\[.*\\]*/s@<([a-zA-Z0-9]+)\\\\s+.*id=.?\\1.*>.*<\\/\\\\1>@@g/g;s/^#\\(.*\\)/s@<([a-zA-Z0-9]+)\\\\s+.*id=.?\\1.*>.*<\\/\\\\1>@@g/g;s/^\\.\\(.*\\)/s@<([a-zA-Z0-9]+)\\\\s+.*class=.?\\1.*>.*<\\/\\\\1>@@g/g;s/^a\\[\\(.*\\)\\]/s@<a.*\\1.*>.*<\\/a>@@g/g;s/^\\([a-zA-Z0-9]*\\)\\.\\(.*\\)\\[.*\\]\\[.*\\]*/s@<\\1.*class=.?\\2.*>.*<\\/\\1>@@g/g;s/^\\([a-zA-Z0-9]*\\)#\\(.*\\):.*[:[^:]]*[^:]*/s@<\\1.*id=.?\\2.*>.*<\\/\\1>@@g/g;s/^\\([a-zA-Z0-9]*\\)#\\(.*\\)/s@<\\1.*id=.?\\2.*>.*<\\/\\1>@@g/g;s/^\\[\\([a-zA-Z]*\\).=\\(.*\\)\\]/s@\\1^=\\2>@@g/g;s/\\^/[\\/\\&:\\?=_]/g;s/\\.\\([a-zA-Z0-9]\\)/\\\\.\\1/g" "' +
file + '" >> "' + filterfile + '"', 0, true);
testing && WScript.Echo('... filterfile created - adding filterfile to actionfile ...\n');
s = fso.OpenTextFile(actionfile, 8);
s.WriteLine('{ +filter{' + list + '} }');
s.WriteLine('*');
testing && WScript.Echo('... filterfile added ...\n... creating and adding whitelist for urls ...\n');
s.WriteLine('{ -block }');
s.Close();
env.run('cmd /c sed "/^@@.*/!d;s/^@@//g;/\\$.*/d;/#/d;s/\\./\\\\./g;s/\\?/\\\\?/g;s/\\*/.*/g;s/(/\\\\(/g;s/)/\\\\)/g;s/\\[/\\\\[/g;s/\\]/\\\\]/g;s/\\^/[\\/\\&:\\?=_]/g;s/^||/\\./g;s/^|/^/g;s/|$/\\$/g;/|/d" "' +
file + '" >> "' + actionfile + '"', 0, true);
testing && WScript.Echo('... created and added whitelist - creating and adding image handler ...\n');
s = fso.OpenTextFile(actionfile, 8);
s.WriteLine('{ -block +handle-as-image }');
s.Close();
env.run('cmd /c sed "/^@@.*/!d;s/^@@//g;/\\$.*image.*/!d;s/\\$.*image.*//g;/#/d;s/\\./\\\\./g;s/\\?/\\\\?/g;s/\\*/.*/g;s/(/\\\\(/g;s/)/\\\\)/g;s/\\[/\\\\[/g;s/\\]/\\\\]/g;s/\\^/[\\/\\&:\\?=_]/g;s/^||/\\./g;s/^|/^/g;s/|$/\\$/g;/|/d" "' +
file + '" >> "' + actionfile + '"', 0, true);
testing && WScript.Echo('... created and added image handler ...\n... created actionfile for ' + list + '\n');
actionfiledest = privoxydir + '\\' + acnm;
testing && WScript.Echo('... copying ' + actionfile + ' to ' + actionfiledest + '\n');
fso.CopyFile(actionfile, actionfiledest);
filterfiledest = privoxydir + '\\' + ftnm;
testing && WScript.Echo('... copying ' + filterfile + ' to ' + filterfiledest + '\n');
fso.CopyFile(filterfile, filterfiledest);
}
}
/**
* Prints command usage.
* @this {undefined}
* @return {undefined}
*/
function usage() {
'use strict';
testing && WScript.Echo('Usage: ' + WScript.ScriptName + ' [-d] [-p <privoxy config dir>] [-u <url1>] [-u <url2>]...\n');
}
/**
* Main script entry point.
* @this {undefined}
* @return {undefined}
*/
function main() {
'use strict';
var pidfile = pidfilename(), tempdir = tmp, objArgs = WScript.Arguments,
privoxydir = defaultprivoxydir, urls = defaulturls, debug, i, arg, next,
l = objArgs.length;
if (isrunning(pidfile)) {
testing && WScript.Echo(WScript.ScriptName + ' is already running\n');
return;
}
/* (not currently implemented)
createpidfile $$ "${pidfile}"
trap 'cleanup' INT TERM EXIT
debug="false"
trap 'logger -t $0 -i -- $USER : $BASH_COMMAND' ERR #log errors regardless
*/
for (i = l; i > 0; i--) {
arg = objArgs[l - i];
if (arg.charAt(0) !== '-') continue;
next = objArgs[l - i + 1] || '';
switch (arg) {
case '-p':
if (next.charAt(0) === '-') {
testing && WScript.Echo('Option -p must be followed by a directory path.\n');
usage();
break;
}
privoxydir = next || defaultprivoxydir;
break;
case '-u':
if (next.charAt(0) === '-') {
testing && WScript.Echo('Option -u must be followed by a URL.\n');
usage();
break;
}
urls.push(next);
break;
case '-d':
debug = true;
break;
default:
usage();
break;
}
}
/* [[ "$debug" == "true" ]] && trap 'logger -t $0 -i -- $USER : $BASH_COMMAND' DEBUG #syslog everything if we're debugging */
if (!urls.length || !fso.FolderExists(privoxydir)) usage();
// perform the operation
doconvert(privoxydir, urls);
}
main();