-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
79 lines (64 loc) · 2.12 KB
/
Copy pathbackground.js
File metadata and controls
79 lines (64 loc) · 2.12 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function normalizeProxyInput(message) {
const host = (message.host || '127.0.0.1').toString().trim() || '127.0.0.1';
const parsedPort = Number.parseInt(message.port, 10);
const port = Number.isInteger(parsedPort) && parsedPort > 0 && parsedPort <= 65535 ? parsedPort : 8080;
return { host, port };
}
function clearProxy() {
chrome.proxy.settings.clear({ scope: 'regular' }, () => {
if (chrome.runtime.lastError) {
console.error('Proxy clearing failed:', chrome.runtime.lastError.message);
return;
}
console.log('Proxy disabled. Control yielded.');
});
}
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (message.action === 'enableProxy') {
const { host, port } = normalizeProxyInput(message);
const proxyServer = {
scheme: 'http',
host,
port
};
const config = {
mode: 'fixed_servers',
rules: {
singleProxy: proxyServer,
bypassList: ['<-loopback>']
}
};
chrome.proxy.settings.set({ value: config, scope: 'regular' }, () => {
if (chrome.runtime.lastError) {
console.error('Proxy setting failed:', chrome.runtime.lastError.message);
sendResponse({ ok: false, error: chrome.runtime.lastError.message });
return;
}
console.log(`Proxy enabled at ${host}:${port}`);
sendResponse({ ok: true });
});
return true;
}
if (message.action === 'disableProxy') {
chrome.proxy.settings.clear({ scope: 'regular' }, () => {
if (chrome.runtime.lastError) {
console.error('Proxy clearing failed:', chrome.runtime.lastError.message);
sendResponse({ ok: false, error: chrome.runtime.lastError.message });
return;
}
console.log('Proxy disabled. Control yielded.');
sendResponse({ ok: true });
});
return true;
}
return false;
});
function clearProxyIfDisabled() {
chrome.storage.local.get(['proxyEnabled'], (result) => {
if (!result.proxyEnabled) {
clearProxy();
}
});
}
chrome.runtime.onStartup.addListener(clearProxyIfDisabled);
chrome.runtime.onInstalled.addListener(clearProxyIfDisabled);