-
Notifications
You must be signed in to change notification settings - Fork 8
/
options.js
153 lines (143 loc) · 4.3 KB
/
options.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
document.addEventListener("DOMContentLoaded", init, false);
async function init() {
let settings = await loadSettings();
for (let i = 0; i < settings.length; i++) {
tableAdd(settings[i]);
}
document.getElementById("mode").onchange = changeMode;
changeMode();
document.getElementById("add").onclick = addProxy;
}
function changeMode() {
let select = document.getElementById("mode");
let mode = select[select.selectedIndex].value;
let container = document.getElementById("container");
while (container.firstChild) container.removeChild(container.firstChild);
switch (mode) {
case "fixed_servers":
case "pac_script":
container.appendChild(document.createTextNode("URL: "));
let input = document.createElement("input");
input.id = "value";
input.type = "text";
input.size = 40;
container.appendChild(input);
break;
case "pac_script_data":
container.appendChild(document.createTextNode("Name: "));
let name = document.createElement("input");
name.id = "value";
name.type = "text";
name.size = 32;
container.appendChild(name);
container.appendChild(document.createElement("br"));
let textarea = document.createElement("textarea");
textarea.id = "data";
textarea.rows = 2;
textarea.cols = 40;
container.appendChild(textarea);
break;
default:
throw "unexpected mode: " + proxy.mode;
}
}
function addProxy(settings) {
let mode = document.getElementById("mode");
let value = document.getElementById("value");
let data = document.getElementById("data");
let proxy = {
mode: mode.value,
value: value.value,
};
value.value = "";
if (proxy.mode === "pac_script_data") {
proxy.data = data.value;
data.value = "";
}
tableAdd(proxy);
save();
}
function tableAdd(proxy) {
let tbody = document.getElementById("list");
let tr = document.createElement("tr");
let td = document.createElement("td");
td.innerText = friendlyMode(proxy.mode);
td.setAttribute("data-mode", proxy.mode);
tr.appendChild(td);
td = document.createElement("td");
td.innerText = proxy.value;
td.setAttribute("data-value", proxy.value);
if (proxy.mode === "pac_script_data") {
td.setAttribute("data-pac", proxy.data);
}
tr.appendChild(td);
td = document.createElement("td");
let up = document.createElement("input");
up.type = "button";
up.value = "^";
up.onclick = function() {
let list = document.getElementById("list");
let before = Array.prototype.slice.call(list.children).indexOf(tr) - 1;
// Keep Direct/System/Auto Detect at the top.
if (before < 3) before = 3;
list.insertBefore(tr, list.children[before]);
save();
};
td.appendChild(up);
let del = document.createElement("input");
del.type = "button";
del.value = "delete";
del.onclick = function() {
tbody.removeChild(tr);
save();
};
td.appendChild(del);
let down = document.createElement("input");
down.type = "button";
down.value = "v";
down.onclick = function() {
let list = document.getElementById("list");
let after = Array.prototype.slice.call(list.children).indexOf(tr) + 1;
// Keep the form to add at the bottom.
if (after >= list.children.length - 1) after = list.children.length - 2;
list.insertBefore(tr, list.children[after].nextSibling);
save();
};
td.appendChild(down);
tr.appendChild(td);
tbody.insertBefore(tr, document.getElementById("last"));
}
function friendlyMode(mode) {
switch (mode) {
case "direct":
return "Direct";
case "system":
return "System";
case "auto_detect":
return "Auto detect";
case "fixed_servers":
return "Fixed server";
case "pac_script":
return "PAC URL";
case "pac_script_data":
return "PAC script";
}
return "n/a";
}
function save() {
let settings = [];
let trs = document.getElementById("list").children;
for (let i = 0; i < trs.length; i++) {
let tds = trs[i].children;
if (tds.length < 2) continue;
let mode = tds[0].getAttribute("data-mode");
if (!mode) continue;
let value = tds[1].getAttribute("data-value");
let proxy = {mode: mode, value: value};
if (mode === "pac_script_data") {
proxy.data = tds[1].getAttribute("data-pac");
}
settings.push(proxy);
}
saveSettings(settings);
}