Skip to content

Commit 0bf979b

Browse files
committed
add commands for set proxy in FF
Commands: 'sn' -> none proxy 'ss' -> system proxy 'sa' -> autodetect proxy 'sm' -> manual proxy
1 parent 654edf2 commit 0bf979b

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

background_scripts/commands.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,8 @@ const Commands = {
356356
"zoomOut",
357357
"zoomReset",
358358
],
359-
misc: ["showHelp", "toggleViewSource"],
359+
misc: ["showHelp", "toggleViewSource"] +
360+
["setProxyNone", "setProxyAutodetect", "setProxySystem", "setProxyManual"],
360361
},
361362

362363
// Rarely used commands are not shown by default in the help dialog or in the README. The goal is
@@ -391,6 +392,7 @@ const Commands = {
391392
"zoomIn",
392393
"zoomOut",
393394
"zoomReset",
395+
"setProxyNone", "setProxyAutodetect", "setProxySystem", "setProxyManual",
394396
],
395397
};
396398

@@ -476,6 +478,11 @@ const defaultKeyMappings = {
476478
// Misc
477479
"?": "showHelp",
478480
"gs": "toggleViewSource",
481+
482+
"sn": "setProxyNone",
483+
"sa": "setProxyAutodetect",
484+
"ss": "setProxySystem",
485+
"sm": "setProxyManual",
479486
};
480487

481488
// This is a mapping of: commandIdentifier => [description, options].
@@ -584,6 +591,11 @@ const commandDescriptions = {
584591

585592
"Marks.activateCreateMode": ["Create a new mark", { noRepeat: true }],
586593
"Marks.activateGotoMode": ["Go to a mark", { noRepeat: true }],
594+
595+
setProxyNone: ["No proxy", { background: true }],
596+
setProxyAutodetect: ["Auto-detect proxy settings", { background: true }],
597+
setProxySystem: ["Use system proxy settings", { background: true }],
598+
setProxyManual: ["Use manual proxy configuration", { background: true }],
587599
};
588600

589601
globalThis.Commands = Commands;

background_scripts/main.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,48 @@ function nextZoomLevel(currentZoom, steps) {
210210
}
211211
}
212212

213+
function setProxy(request, sender, proxy_type) {
214+
const currentTab = request.tab;
215+
const tabId = request.tabId;
216+
const registryEntry = request.registryEntry;
217+
218+
var getting = browser.proxy.settings.get({});
219+
220+
getting.then((got) => {
221+
got.value.proxyType = proxy_type;
222+
223+
var msg;
224+
if (proxy_type == "none")
225+
msg = "None proxy";
226+
else if (proxy_type == "autoDetect")
227+
msg = "Auto detect proxy";
228+
else if (proxy_type == "system")
229+
msg = "System proxy";
230+
else if (proxy_type == "manual")
231+
msg = "Manual proxy:" + got.value.http;
232+
233+
browser.proxy.settings.set({value: got.value})
234+
.then((res) => {
235+
if (res) {
236+
chrome.tabs.sendMessage(tabId, {
237+
frameId: sender.frameId,
238+
handler: "showMessage",
239+
message: msg
240+
});
241+
} else
242+
throw new Error();
243+
})
244+
.catch((err) => {
245+
chrome.tabs.sendMessage(tabId, {
246+
frameId: sender.frameId,
247+
handler: "showMessage",
248+
message: "Required private browsing permission"
249+
});
250+
}
251+
);
252+
});
253+
}
254+
213255
// These are commands which are bound to keystrokes which must be handled by the background page.
214256
// They are mapped in commands.js.
215257
const BackgroundCommands = {
@@ -411,6 +453,23 @@ const BackgroundCommands = {
411453
chrome.tabs.reload(tab.id, { bypassCache: true });
412454
});
413455
},
456+
457+
setProxyNone(request, sender) {
458+
setProxy(request, sender, "none");
459+
},
460+
461+
setProxyAutodetect(request, sender) {
462+
setProxy(request, sender, "autoDetect");
463+
},
464+
465+
setProxySystem(request, sender) {
466+
setProxy(request, sender, "system");
467+
},
468+
469+
setProxyManual(request, sender) {
470+
setProxy(request, sender, "manual");
471+
},
472+
414473
};
415474

416475
async function forCountTabs(count, currentTab, callback) {

make.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ function createFirefoxManifest(manifest) {
3636
manifest.permissions = manifest.permissions
3737
// The favicon permission is not yet supported by Firefox.
3838
.filter((p) => p != "favicon")
39+
// added for switch proxy configuration in Firefox
40+
.concat(["browserSettings", "proxy"])
3941
// Firefox needs clipboardRead and clipboardWrite for commands like "copyCurrentUrl", but Chrome
4042
// does not. See #4186.
4143
.concat(["clipboardRead", "clipboardWrite"]);

0 commit comments

Comments
 (0)