summaryrefslogtreecommitdiffstats
path: root/src/scripts/patterns.js
blob: a276a20c8f73c0f6e1e4ed0711e303da6019a41b (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
'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 -----------------

document.addEventListener('keyup', evt => {
  if (evt.keyCode === 27) {
    history.back(); // We either came from /proxy.html or /options.html
  }
});

// ----------------- Spinner -------------------------------
const spinner = document.querySelector('.spinner');
function hideSpinner() {

  spinner.classList.remove('on');
  setTimeout(() => { spinner.style.display = 'none'; }, 600);
}

function showSpinner() {

  spinner.style.display = 'flex';
  spinner.classList.add('on');
}
// ----------------- /spinner ------------------------------

// ----- global
let proxy = {};
const header = document.querySelector('.header');
const tbody = document.querySelectorAll('tbody');         // there are 2
const template = document.querySelector('tr.template');
const docfrag = document.createDocumentFragment();

const defaultPattern = {
  title: '',
  active: true,
  pattern: '',
  type: 1,                  // PATTERN_TYPE_WILDCARD,
  protocols: 1              // PROTOCOL_ALL
};


const protocolSet = {                                     // converting to meaningful terms
  1: 'All',
  2: 'HTTP',
  4: 'HTTPS'
};

const patternTypeSet = {
  1: 'wildcard',
  2: 'Reg Exp'
}

// ----- check for Edit
const id = localStorage.getItem('id');
const sync = localStorage.getItem('sync') === 'true';
const storageArea = !sync ? chrome.storage.local : chrome.storage.sync;
if (id) {                                                   // This is an edit operation

  storageArea.get(id, result => {

    if (!Object.keys(result).length) {
/*
      if (id === LASTRESORT) {                              // error prevention
        proxy = DEFAULT_PROXY_SETTING;
        processOptions();
        return;
      }*/
      console.error('Unable to edit saved proxy (could not get existing settings)')
      return;
    }

    proxy = result[id];
    if (proxy.title) { header.textContent = chrome.i18n.getMessage('editPatternsFor', proxy.title); }
    processOptions();
    hideSpinner();
  })
}
/*
else {
  // Error, shouldn't ever get here
  hideSpinner();
  document.querySelector('#error').classList.remove('hide');
  document.querySelector('.main').classList.add('hide');
  console.error("2: Unable to read saved proxy proxy (could not get existing settings)");
}*/

// --- processing all buttons
document.querySelectorAll('button').forEach(item => item.addEventListener('click', process));

function process() {

  switch (this.dataset.i18n) {

    case 'back':                                            // error
    case 'cancel':
      location.href = '/options.html';
      break;

    case 'exportPatterns': exportPatterns(); break;

    case 'newWhite':
       addNew(tbody[0], 'whitePatterns');
      break;

    case 'newBlack':
      addNew(tbody[1], 'blackPatterns');
      break;

    case 'save':
      checkOptions();
      break;

    case 'add':
      if (typeof(this.dataset.black) !== 'undefined') {
        proxy.blackPatterns.push(...blacklistSet);
        processOptions();
      }
      else {
        proxy.whitePatterns.push(PATTERN_ALL_WHITE);
        processOptions();
      }
      break;
  }
}

function processOptions() {

  // clearing the content
  tbody[0].textContent = '';
  tbody[1].textContent = '';

  proxy.whitePatterns.forEach((item, index) => docfrag.appendChild(makeRow(item, index, 'whitePatterns')));
  docfrag.hasChildNodes() && tbody[0].appendChild(docfrag);

  proxy.blackPatterns.forEach((item, index) => docfrag.appendChild(makeRow(item, index, 'blackPatterns')));
  docfrag.hasChildNodes() && tbody[1].appendChild(docfrag);

}

function makeRow(pat, index, bw) {

  const tr = template.cloneNode(true);
  tr.classList.remove('template');
  tr.classList.add(pat.active ? 'success' : 'secondary');
  tr.dataset.idx = index;
  tr.dataset.bw = bw;                                       // black/white
  const td = tr.children;


  td[0].children[0].value = pat.title;
  td[1].children[0].value = pat.pattern;
  td[2].children[0].value = pat.type;
  td[3].children[0].value = pat.protocols;
  td[4].children[0].checked = pat.active;
  td[4].children[0].id = bw + index;
  td[4].children[1].setAttribute('for', td[4].children[0].id);

  pat.importedPattern && td[5].children[0].classList.remove('hide');

  // add Listeners();
  [...td[5].children].forEach(item => item.addEventListener('click', processEdit));

  return tr;
}

function addNew(parent, bw) {

  const tr = makeRow(defaultPattern, parent.children.length, bw);
  parent.appendChild(tr);
  tr.children[1].children[0].focus();
}

function processEdit() {

  const parent = this.parentNode.parentNode;
  const idx = parent.dataset.idx *1;
  const patternsArray = proxy[parent.dataset.bw];           // whitePatterns | blackPatterns

  switch (this.dataset.i18n) {

    case 'imported|title':
      alert(chrome.i18n.getMessage('importedPattern') + ' \n\n' + patternsArray[idx].importedPattern);
      break;

    case 'patternTester|title':
      const pat = patternsArray[idx];
      if (pat) {
        localStorage.setItem('pattern', pat.pattern);
        localStorage.setItem('type', pat.type);
        localStorage.setItem('protocols', pat.protocols);
      }
      chrome.tabs.create({url: '/pattern-tester.html'});
      break;

    case 'delete|title':
      parent.style.opacity = 0;
      setTimeout(() => { parent.remove(); }, 300);          // remove row
      break;
  }
}


function checkOptions() {

  const pxy = {
    whitePatterns: [],
    blackPatterns: []
  };
  
  // use for loop to be able to return early on error
  for (const item of document.querySelectorAll('tr[data-idx]')) {

    const td = item.children;

    // --- trim text values
    [td[0].children[0], td[1].children[0]].forEach(item => item.value = item.value.trim());

    // test pattern
    const regex = testPattern(td[1].children[0], td[2].children[0]);
    if (!regex) { return; }

    const bw = item.dataset.bw;
    pxy[bw].push({
      title: td[0].children[0].value,
      pattern: td[1].children[0].value,
      type: td[2].children[0].value *1,
      protocols: td[3].children[0].value *1,
      active: td[4].children[0].checked
    });
  }

  // all patterns passed
  proxy.whitePatterns = pxy.whitePatterns;
  proxy.blackPatterns = pxy.blackPatterns;
  storageArea.set({[id]: proxy}, () => location.href = '/options.html');
}


function testPattern(pattern, type) {

  // --- reset
  pattern.classList.remove('invalid');
  result.classList.add('hide');
  result.classList.remove('alert');

  // --- pattern check
  return checkPattern(pattern, type);
}



function exportPatterns() {

  const tmpObject = {whitePatterns: proxy.whitePatterns, blackPatterns: proxy.blackPatterns};
  const blob = new Blob([JSON.stringify(tmpObject, null, 2)], {type : 'text/plain'});
  const filename = 'foxyproxy' + (proxy.title ? '-' + proxy.title : '')  + '-patterns' + '_' + new Date().toISOString().substring(0, 10) + '.json';
  chrome.downloads.download({
    url: URL.createObjectURL(blob),
    filename,
    saveAs: true,
    conflictAction: 'uniquify'
  }, () => console.log('Export/download finished'));        // wait for it to complete before returning
}


document.getElementById('file').addEventListener('change', processFileSelect);
function processFileSelect(e) {

  const file = e.target.files[0];

  Utils.importFile(file, ['application/json'], 1024*1024*5, 'json', imported => {
    proxy.whitePatterns = imported.whitePatterns;
    proxy.blackPatterns = imported.blackPatterns;
    processOptions();
    Utils.notify(chrome.i18n.getMessage('importBW', [proxy.whitePatterns.length, proxy.blackPatterns.length]));
  });
}