summaryrefslogtreecommitdiffstats
path: root/src/scripts/popup.js
blob: 120e5edbd0650d6ff1d352b3f542518e9e06d322 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';

// ----------------- Internationalization ------------------
document.querySelectorAll('[data-i18n]').forEach(node => {
  let [text, attr] = node.dataset.i18n.split('|');
  text = chrome.i18n.getMessage(text);
  attr ? node[attr] = text : node.appendChild(document.createTextNode(text));
});
// ----------------- /Internationalization -----------------

// ----------------- User Preference -----------------------
let storageArea;
chrome.storage.local.get(null, result => {
  storageArea = result.sync ? chrome.storage.sync : chrome.storage.local;
  result.sync ? chrome.storage.sync.get(null, processOptions) : processOptions(result);
});
// ----------------- /User Preference ----------------------

function processOptions(pref) {

  // ----- templates & containers
  const docfrag = document.createDocumentFragment();
  const temp = document.querySelector('li.template');

  // add default lastresort if not there
  //pref[LASTRESORT] || (pref[LASTRESORT] = DEFAULT_PROXY_SETTING);

  const prefKeys = Object.keys(pref).filter(item => !NON_PROXY_KEYS.includes(item)); // not for these

  prefKeys.sort((a, b) => pref[a].index - pref[b].index);   // sort by index

  pref.mode = pref.mode || 'disabled';                      // defaults to disabled
  let hasProxySettings = false;
  prefKeys.forEach(id => {

    const item = pref[id];

    if (!Utils.isUnsupportedType(item.type)) {              // if supported

      const li = temp.cloneNode(true);
      li.classList.remove('template');
      li.id = id;
      li.style.color = item.color;
      li.children[0].textContent = Utils.getProxyTitle(item);
      li.children[1].textContent = '(' + chrome.i18n.getMessage('forAll') + ')';

      docfrag.appendChild(li);
      hasProxySettings = true;
    }
  });

  docfrag.hasChildNodes() && temp.parentNode.appendChild(docfrag, temp.nextElementSibling);

  if (FOXYPROXY_BASIC) {
    temp.parentNode.children[0].classList.add('hide');      // hide by pattern option
    pref.mode === 'patterns' && (pref.mode = 'disabled');
  }

  // hide the selections if there are no proxy settings defined
  document.getElementById('scroll').style.display = hasProxySettings ? 'block' : 'none';

  const node = document.getElementById(pref.mode);          // querySelector error with selectors starting with number
  node.classList.add('on');

  // add Listeners
  document.querySelectorAll('li, button').forEach(item => item.addEventListener('click', process));
}

function process() {

  let tabs;
  switch (this.dataset.i18n) {

    case 'myIP':
      chrome.tabs.create({url: 'https://getfoxyproxy.org/geoip/'}); // no need to wait for it
      window.close();
      break;

    case 'log':
      const url = chrome.runtime.getURL('log.html');
      chrome.tabs.query({url}, tabs => { // find a log tab
        tabs[0] ? chrome.tabs.update(tabs[0].id, {active: true}) : chrome.tabs.create({url}); // active existing tab OR open new tab
        window.close();
      });
      break;

    case 'options':
      chrome.tabs.query({url: chrome.runtime.getURL('') + '*'}, tabs => {
        if (!tabs[0]) {
          chrome.runtime.openOptionsPage();
          window.close();
          return;
        }
        const tab = tabs.find(item => /(proxy|options|patterns)\.html/.test(item.url));  // find a option tab
        tab ? chrome.tabs.update(tab.id, {active: true}) : chrome.tabs.update(tabs[0].id, {active: true, url: '/options.html'});
        window.close();
      });
      break;

    default:
      // reset the old one
      const old = document.querySelector('.on');
      old &&  old.classList.remove('on');
      this.classList.add('on');


      storageArea.set({mode: this.id});                     // keep it open for more action
      // popup & options are the only place that can set mode
      // sending message to option && bg, if it is open
      chrome.runtime.sendMessage({mode: this.id});
  }
}