-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
59 lines (53 loc) · 1.35 KB
/
proxy.js
File metadata and controls
59 lines (53 loc) · 1.35 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Proxy {
constructor() {
this.proxyEnabled = false;
this.proxyServer = "";
this.init();
}
async init() {
const data = await this.getStorageData(["proxyEnabled", "proxyServer"]);
this.proxyEnabled = data.proxyEnabled || false;
this.proxyServer = data.proxyServer || "";
this.setProxy();
}
async getStorageData(keys) {
return new Promise((resolve) => {
chrome.storage.local.get(keys, (data) => {
resolve(data);
});
});
}
async setStorageData(data) {
return new Promise((resolve) => {
chrome.storage.local.set(data, () => {
resolve();
});
});
}
async toggleProxy() {
this.proxyEnabled = !this.proxyEnabled;
await this.setStorageData({ proxyEnabled: this.proxyEnabled });
this.setProxy();
}
setProxy() {
if (!this.proxyEnabled || !this.proxyServer) {
chrome.proxy.settings.set({ value: { mode: "direct" }, scope: "regular" });
} else {
chrome.proxy.settings.set({
value: {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: this.proxyServer.split(":")[0],
port: parseInt(this.proxyServer.split(":")[1]),
},
bypassList: [],
},
},
scope: "regular",
});
}
}
}
export default Proxy;