forked from AdAway/adaway.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.html
103 lines (95 loc) · 2.78 KB
/
export.html
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
---
layout: inner
title: AdAway - Import and Export
---
<h1 class="center pt-2 pb-2">Import and Export hosts files easily</h1>
<textarea placeholder="Input your legacy host list" id="legacyExportTextArea"></textarea>
<button class="button-submit mb-3" onclick="convertToModern()">Convert to modern</button>
<textarea placeholder="Input your modern host list" id="modernExportTextArea"></textarea>
<button class="button-submit" onclick="convertToLegacy()">Convert to legacy</button>
<script>
let legacyExport = document.getElementById("legacyExportTextArea");
let modernExport = document.getElementById("modernExportTextArea");
let redirectedPattern = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\s+([^\s]+)$/;
function convertToLegacy() {
// Parse export
var text = modernExport.value;
var modern = JSON.parse(text);
// Convert blocked hosts
var legacy = "";
if (modern.blocked) {
for (var blocked of modern.blocked) {
if (blocked && blocked.host && blocked.enabled) {
legacy += "127.0.0.1 " + blocked.host + "\n";
}
}
}
// Convert allowed hosts
if (modern.allowed) {
for (var allowed of modern.allowed) {
if (allowed.host && allowed.enabled) {
legacy += "white " + allowed.host + "\n";
}
}
}
// Convert redicted hosts
if (modern.redirected) {
for (var redirected of modern.redirected) {
if (redirected.host && redirected.redirect && redirected.enabled) {
legacy += redirected.redirect + " " + redirected.host + "\n";
}
}
}
// Display legacy export
legacyExport.value = legacy;
}
function convertToModern() {
var modern = {
'sources': [],
'blocked': [],
'allowed': [],
'redirected': []
};
var text = legacyExport.value;
var lines = text.split('\n');
for (var line of lines) {
// Remove comments
var commentStart = line.indexOf('#');
if (commentStart !== -1) {
line = line.substr(0, commentStart);
}
// Clean up line
line = line.trim();
if (line.length == 0) {
continue;
}
// Test blocked hosts
if (line.startsWith("127.0.0.1 ")) {
modern.blocked.push({
"host": line.substr(10),
"enabled": true
});
}
// Test allowed hosts
else if (line.startsWith("white ")) {
modern.allowed.push({
"host": line.substr(6),
"enabled": true
});
}
// Test redirected domains
else {
var match = line.match(redirectedPattern);
if (match.length > 5) {
modern.redirected.push({
"host": match[5],
"redirect": match[1] + '.' + match[2] + '.' + match[3] + '.' + match[4],
"enabled": true
});
}
}
}
// Display modern export
modernExport.value = JSON.stringify(modern, null, ' ');
}
</script>