From f803613d7042c203c512759a804d9bb119d79c87 Mon Sep 17 00:00:00 2001 From: Igor Date: Tue, 1 Oct 2024 18:47:14 +0300 Subject: [PATCH] add commands for set proxy in FF Commands: 'sn' -> none proxy 'ss' -> system proxy 'sa' -> autodetect proxy 'sm' -> manual proxy --- background_scripts/commands.js | 14 ++++++- background_scripts/main.js | 68 ++++++++++++++++++++++++++++++++++ make.js | 2 + 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/background_scripts/commands.js b/background_scripts/commands.js index 8fa8c29c4..87b8b7a77 100644 --- a/background_scripts/commands.js +++ b/background_scripts/commands.js @@ -356,7 +356,8 @@ const Commands = { "zoomOut", "zoomReset", ], - misc: ["showHelp", "toggleViewSource"], + misc: ["showHelp", "toggleViewSource"] + + ["setProxyNone", "setProxyAutodetect", "setProxySystem", "setProxyManual"], }, // 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 = { "zoomIn", "zoomOut", "zoomReset", + "setProxyNone", "setProxyAutodetect", "setProxySystem", "setProxyManual", ], }; @@ -476,6 +478,11 @@ const defaultKeyMappings = { // Misc "?": "showHelp", "gs": "toggleViewSource", + + "sn": "setProxyNone", + "sa": "setProxyAutodetect", + "ss": "setProxySystem", + "sm": "setProxyManual", }; // This is a mapping of: commandIdentifier => [description, options]. @@ -584,6 +591,11 @@ const commandDescriptions = { "Marks.activateCreateMode": ["Create a new mark", { noRepeat: true }], "Marks.activateGotoMode": ["Go to a mark", { noRepeat: true }], + + setProxyNone: ["No proxy", { background: true }], + setProxyAutodetect: ["Auto-detect proxy settings", { background: true }], + setProxySystem: ["Use system proxy settings", { background: true }], + setProxyManual: ["Use manual proxy configuration", { background: true }], }; globalThis.Commands = Commands; diff --git a/background_scripts/main.js b/background_scripts/main.js index 823f50187..4cf5927a4 100644 --- a/background_scripts/main.js +++ b/background_scripts/main.js @@ -210,6 +210,57 @@ function nextZoomLevel(currentZoom, steps) { } } +function setProxy(request, sender, proxy_type) { + const currentTab = request.tab; + const tabId = request.tabId; + const registryEntry = request.registryEntry; + + if (typeof browser === "undefined" || typeof browser.proxy === "undefined") { + chrome.tabs.sendMessage(tabId, { + frameId: sender.frameId, + handler: "showMessage", + message: "Not supported proxy change" + }); + return; + } + + var getting = browser.proxy.settings.get({}); + + getting.then((got) => { + got.value.proxyType = proxy_type; + + var msg; + if (proxy_type == "none") + msg = "None proxy"; + else if (proxy_type == "autoDetect") + msg = "Auto detect proxy"; + else if (proxy_type == "system") + msg = "System proxy"; + else if (proxy_type == "manual") + msg = "Manual proxy: " + got.value.http; + + browser.proxy.settings.set({value: got.value}) + .then((res) => { + if (res) { + chrome.tabs.sendMessage(tabId, { + frameId: sender.frameId, + handler: "showMessage", + message: msg + }); + } else + throw new Error(); + }) + .catch((err) => { + chrome.tabs.sendMessage(tabId, { + frameId: sender.frameId, + handler: "showMessage", + message: "Required private browsing permission" + }); + } + ); + }); +} + // These are commands which are bound to keystrokes which must be handled by the background page. // They are mapped in commands.js. const BackgroundCommands = { @@ -411,6 +462,23 @@ const BackgroundCommands = { chrome.tabs.reload(tab.id, { bypassCache: true }); }); }, + + setProxyNone(request, sender) { + setProxy(request, sender, "none"); + }, + + setProxyAutodetect(request, sender) { + setProxy(request, sender, "autoDetect"); + }, + + setProxySystem(request, sender) { + setProxy(request, sender, "system"); + }, + + setProxyManual(request, sender) { + setProxy(request, sender, "manual"); + }, + }; async function forCountTabs(count, currentTab, callback) { diff --git a/make.js b/make.js index cb5e314d5..d49e7a5ff 100755 --- a/make.js +++ b/make.js @@ -36,6 +36,8 @@ function createFirefoxManifest(manifest) { manifest.permissions = manifest.permissions // The favicon permission is not yet supported by Firefox. .filter((p) => p != "favicon") + // added for switch proxy configuration in Firefox + .concat(["browserSettings", "proxy"]) // Firefox needs clipboardRead and clipboardWrite for commands like "copyCurrentUrl", but Chrome // does not. See #4186. .concat(["clipboardRead", "clipboardWrite"]);