-
Notifications
You must be signed in to change notification settings - Fork 8
/
popup.js
115 lines (109 loc) · 2.87 KB
/
popup.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
document.addEventListener("DOMContentLoaded", init, false);
async function init() {
let settings = [
{mode: "direct"},
{mode: "system"},
{mode: "auto_detect"}
].concat(await loadSettings());
let selected = await loadSelected();
for (let i = 0; i < settings.length; i++) {
tableAdd(i, settings[i], selected);
}
}
function tableAdd(i, proxy, selected) {
let tbody = document.getElementById("list");
let tr = document.createElement("tr");
let td = document.createElement("td");
let input = document.createElement("input");
input.type = "radio";
input.name = "choice"; // same name so only one selected at a time
input.id = "proxy-" + i;
if (selected
&& proxy.mode == selected.mode
&& proxy.value == selected.value) {
input.checked = true;
}
input.onclick = function() {
switchProxy(proxy);
chrome.action.setTitle({title: nameProxy(proxy)});
saveSelected(proxy);
};
td.appendChild(input);
let label = document.createElement("label");
label.htmlFor = input.id;
label.innerText = nameProxy(proxy);
td.appendChild(label);
tr.appendChild(td);
tbody.appendChild(tr);
}
function nameProxy(proxy) {
switch (proxy.mode) {
case "direct":
return "Direct";
case "system":
return "System";
case "auto_detect":
return "Auto detect";
case "fixed_servers":
return "Fixed: " + proxy.value;
case "pac_script":
return "PAC: " + proxy.value;
case "pac_script_data":
return "PAC: " + proxy.value;
}
return "n/a";
}
function switchProxy(proxy) {
let config = {mode: proxy.mode};
switch (proxy.mode) {
case "direct":
break;
case "system":
break;
case "auto_detect":
// downloads a PAC script at http://wpad/wpad.dat
break;
case "fixed_servers":
config.rules = {
singleProxy: parseURL(proxy.value)
};
break;
case "pac_script":
config.pacScript = {
url: proxy.value,
mandatory: true // do not fallback to direct if invalid PAC script
};
break;
case "pac_script_data":
config.mode = "pac_script";
config.pacScript = {
data: proxy.data,
mandatory: true // do not fallback to direct if invalid PAC script
};
break;
default:
throw "unexpected mode: " + proxy.mode;
}
switch (proxy.mode) {
case "direct":
case "system":
case "auto_detect":
chrome.action.setIcon({path: "icon24-grey.png"});
break;
default:
chrome.action.setIcon({path: "icon24.png"});
}
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
}
const urlRE = new RegExp("^(?<scheme>[^:]+?)://(?<host>.*?):(?<port>[0-9]+)$");
function parseURL(url) {
let m = url.match(urlRE);
if (!m) {
return {}
}
return {
scheme: m.groups.scheme,
host: m.groups.host,
port: parseInt(m.groups.port)
}
}