-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
155 lines (136 loc) · 4.15 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
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
154
155
// Log errors from proxy script
browser.proxy.onError.addListener(error => {
console.error(`Proxy error: ${error.message}`);
});
var proxy = {
enabled: false,
type: '',
host: '',
port: '',
filters: {urls: ["<all_urls>"]}
}
function proxyListener(requestDetails) {
proxyInfo = proxy
console.debug('proxying request: ', requestDetails);
return proxyInfo
}
const proxyOnIcon = {
"16": "data/on_16.png",
"32": "data/on_32.png",
"64": "data/on_64.png"
};
const proxyOffIcon = {
"16": "data/off_16.png",
"32": "data/off_32.png",
"64": "data/off_64.png"
};
const proxyPermission = {
origins: ["<all_urls>"]
};
const proxyOnLabel = "Proxy is On - press Alt+Shift+X to turn off";
const proxyOffLabel = "Proxy is Off - press Alt+Shift+X to turn on";
var oneClickToggle = false
function init() {
browser.storage.local.get()
.then((storedSettings) => {
requiredKeys = ["enabled", "port", "host", "type"]
hasRequiredKeys = requiredKeys.every(key => Object.keys(storedSettings).includes(key))
hasNoRequiredKeys = requiredKeys.every(key => !(Object.keys(storedSettings).includes(key)))
if(hasRequiredKeys) {
proxy.enabled = storedSettings.enabled;
proxy.host = storedSettings.host;
proxy.port = storedSettings.port;
proxy.type = storedSettings.type;
if(proxy.enabled) {
browser.action.setIcon({path: proxyOnIcon});
browser.action.setTitle({title: proxyOnLabel});
browser.proxy.onRequest.addListener(proxyListener, proxy.filters);
}
} else if(hasNoRequiredKeys) {
saveProxyConfig();
}
if("oneClickToggle" in storedSettings) {
oneClickToggle = storedSettings.oneClickToggle
toggleOneClickToggle(oneClickToggle)
}
});
}
function getProxyConfig() {
browser.storage.local.get()
.then((storedSettings) => {
proxy.enabled = storedSettings.enabled;
proxy.host = storedSettings.host;
proxy.port = storedSettings.port;
proxy.type = storedSettings.type;
});
}
function saveProxyConfig() {
browser.storage.local.set(proxy);
}
async function requestPermission() {
const response = await browser.permissions.request(proxyPermission);
if (response) {
console.log("Permission was granted");
browser.action.setPopup({popup: "menu/menu.html"})
} else {
console.log("Permission was denied");
}
}
function toggleProxyConfig() {
proxy.enabled = !proxy.enabled;
if(proxy.enabled) {
console.debug("Proxy has been turned ON")
browser.action.setIcon({path: proxyOnIcon});
browser.action.setTitle({title: proxyOnLabel});
browser.proxy.onRequest.addListener(proxyListener, proxy.filters);
}
else {
console.debug("Proxy has been turned OFF")
browser.action.setIcon({path: proxyOffIcon});
browser.action.setTitle({title: proxyOffLabel});
browser.proxy.onRequest.removeListener(proxyListener);
}
saveProxyConfig();
}
function toggleOneClickToggle(checked) {
if(checked){
browser.action.setPopup({popup: ""})
browser.action.onClicked.addListener(toggleProxyConfig)
} else {
browser.action.setPopup({popup: "menu/menu.html"})
}
oneClickToggle = checked
browser.storage.local.set({oneClickToggle: checked});
}
init();
async function checkPermission() {
const has_permission = await browser.permissions.contains(proxyPermission);
if (!has_permission) {
browser.action.setPopup({popup: ""})
browser.action.onClicked.addListener(requestPermission)
}
}
checkPermission();
browser.commands.onCommand.addListener(function(command) {
if (command == "toggle-status") {
// This message makes the popup toggle the switch when open
browser.runtime.sendMessage('toggle-switch');
toggleProxyConfig();
}
});
function createMenu(details) {
console.log(details.reason);
browser.menus.create({
id: "oneClickToggle",
type: "checkbox",
title: "One click toggle",
contexts: ["browser_action"],
checked: oneClickToggle
});
}
browser.runtime.onInstalled.addListener(createMenu);
browser.menus.onClicked.addListener(function(info, tab) {
if (info.menuItemId == "oneClickToggle") {
toggleOneClickToggle(info.checked)
}
});