forked from izogain/chrome-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-sites.js
More file actions
44 lines (40 loc) · 1.36 KB
/
Copy pathbuild-sites.js
File metadata and controls
44 lines (40 loc) · 1.36 KB
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
var fs = require('fs');
function escapeRegExp(s) {
return s.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}
function makeMatchRe(pattern) {
var matchRe = /^(\*|https?):\/\/((?:\*\.[^\/*]+)|[^\/*]+|\*)(\/.*)$/;
var match = pattern.match(matchRe);
if (!match) {
return;
}
var parts = [];
parts.push(match[1] === '*' ? 'https?' : match[1]);
if (match[2] === '*') {
parts.push('[^/]+');
} else if (match[2].substr(0, 2) == '*.') {
parts.push('(?:.+\\.)?' + escapeRegExp(match[2].substr(2)));
} else {
parts.push(escapeRegExp(match[2]));
}
parts.push(escapeRegExp(match[3]).replace(/\\\*/g, '.+?'));
return '(?:^' + parts[0] + '://' + parts[1] + parts[2] + '$)';
}
var sites = {videoSites: [], streamingSites: []};
fs.readFile(process.argv[2], function(err, data) {
if (err) throw err;
var patterns = JSON.parse(data);
var categories = ['videoSites', 'streamingSites'];
for (var category, c = 0; (category = categories[c++]); category !== undefined) {
for (var pattern, p = 0; (pattern = patterns[category][p++]); pattern !== undefined) {
var matchRe = makeMatchRe(pattern);
if (!matchRe) {
process.stderr.write("bad " + category + " pattern: " + pattern);
}
sites[category].push(matchRe);
}
}
fs.writeFile(process.argv[3], JSON.stringify(sites), function(err) {
if (err) throw err;
});
});