aboutsummaryrefslogtreecommitdiff
path: root/store/works/ProxyChanger/popup/popup.js
blob: 06150352f2e37430e2d9cba573cd778e94d743c4 (plain)
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
const buttons = {
  direct: document.getElementById("direct_button"),
  system: document.getElementById("system_button"),
  proxy: document.getElementById("proxy_button"),
};

function setSelectedButton(button) {
  for (const key in buttons) {
    buttons[key].classList.remove("selected");
  }
  buttons[button].classList.add("selected");
}

browser.proxy.settings.get({}).then(({ value }) => {
  console.log("current proxy settings: ", value);
  if (value.proxyType === "none") {
    setSelectedButton("direct");
  } else if (value.proxyType === "system") {
    setSelectedButton("system");
  } else if (value.proxyType === "manual") {
    setSelectedButton("proxy");
  }
});

browser.storage.local.get("proxyUrl").then(({ proxyUrl }) => {
  console.log("Saved proxy url is ", proxyUrl);

  const proxyConfigs = {
    direct: {
      proxyType: "none",
    },
    system: {
      proxyType: "system",
    },
    proxy: {
      proxyType: "manual",
      http: proxyUrl,
      httpProxyAll: true,
    },
  };

  for (const key in buttons) {
    buttons[key].addEventListener("click", () => {
      console.log("Try to set proxy to ", proxyConfigs[key]);
      browser.proxy.settings
        .set({ value: proxyConfigs[key] })
        .then((success) => {
          if (success) {
            setSelectedButton(key);
          }
        });
    });
  }
});

document.getElementById("settings_button").addEventListener("click", () => {
  browser.runtime.openOptionsPage();
});