-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
93 lines (83 loc) · 2.38 KB
/
background.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
// Generated by CoffeeScript 1.6.3
(function() {
var display_status, is_config_valid, proxy, switch_proxy, test_proxy;
test_proxy = function(cb) {
return cb(true);
};
is_config_valid = function(conf) {
return typeof conf === "object" && conf.host.trim() && /\d+/.exec(conf.port.trim());
};
proxy = function(enable, conf, cb) {
var config;
if (enable && is_config_valid(conf)) {
config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "socks5",
host: conf.host,
port: Number(conf.port)
},
bypassList: conf.bypassList
}
};
return chrome.proxy.settings.set({
value: config,
scope: 'regular'
}, test_proxy(cb));
} else {
if (!is_config_valid(conf)) {
chrome.tabs.create({
url: "options.html"
});
alert("[WARN]: SOCKS5 proxy is not configured yet. \n\nPlease configure and save it.");
}
config = {
mode: "direct"
};
return chrome.proxy.settings.set({
value: config,
scope: 'regular'
}, function() {
return cb(null);
});
}
};
switch_proxy = function(action) {
return chrome.storage.local.get(["enabled", "fixed_servers"], function(data) {
if (!("enabled" in data)) {
data.enabled = false;
}
if (action) {
data.enabled = !data.enabled;
}
return proxy(data.enabled, data.fixed_servers, function(enabled) {
data.enabled = enabled;
return chrome.storage.local.set(data, function() {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
return display_status();
});
});
});
};
display_status = function() {
return chrome.storage.local.get("enabled", function(data) {
return chrome.browserAction.setBadgeText({
text: data.enabled ? "on" : "off"
});
});
};
chrome.runtime.onStartup.addListener(display_status);
chrome.runtime.onInstalled.addListener(display_status);
chrome.browserAction.onClicked.addListener(switch_proxy);
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request && (request.id === "refresh")) {
switch_proxy();
}
if (sendResponse) {
return sendResponse(null);
}
});
}).call(this);