Skip to content

Commit f803613

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 f803613

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-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: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,57 @@ 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+
if (typeof browser === "undefined" || typeof browser.proxy === "undefined") {
219+
chrome.tabs.sendMessage(tabId, {
220+
frameId: sender.frameId,
221+
handler: "showMessage",
222+
message: "Not supported proxy change"
223+
});
224+
return;
225+
}
226+
227+
var getting = browser.proxy.settings.get({});
228+
229+
getting.then((got) => {
230+
got.value.proxyType = proxy_type;
231+
232+
var msg;
233+
if (proxy_type == "none")
234+
msg = "None proxy";
235+
else if (proxy_type == "autoDetect")
236+
msg = "Auto detect proxy";
237+
else if (proxy_type == "system")
238+
msg = "System proxy";
239+
else if (proxy_type == "manual")
240+
msg = "Manual proxy: " + got.value.http;
241+
242+
browser.proxy.settings.set({value: got.value})
243+
.then((res) => {
244+
if (res) {
245+
chrome.tabs.sendMessage(tabId, {
246+
frameId: sender.frameId,
247+
handler: "showMessage",
248+
message: msg
249+
});
250+
} else
251+
throw new Error();
252+
})
253+
.catch((err) => {
254+
chrome.tabs.sendMessage(tabId, {
255+
frameId: sender.frameId,
256+
handler: "showMessage",
257+
message: "Required private browsing permission"
258+
});
259+
}
260+
);
261+
});
262+
}
263+
213264
// These are commands which are bound to keystrokes which must be handled by the background page.
214265
// They are mapped in commands.js.
215266
const BackgroundCommands = {
@@ -411,6 +462,23 @@ const BackgroundCommands = {
411462
chrome.tabs.reload(tab.id, { bypassCache: true });
412463
});
413464
},
465+
466+
setProxyNone(request, sender) {
467+
setProxy(request, sender, "none");
468+
},
469+
470+
setProxyAutodetect(request, sender) {
471+
setProxy(request, sender, "autoDetect");
472+
},
473+
474+
setProxySystem(request, sender) {
475+
setProxy(request, sender, "system");
476+
},
477+
478+
setProxyManual(request, sender) {
479+
setProxy(request, sender, "manual");
480+
},
481+
414482
};
415483

416484
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)