/
popup.ts
463 lines (367 loc) · 16.5 KB
/
popup.ts
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* This file is part of SmartProxy <https://github.com/salarcode/SmartProxy>,
* Copyright (C) 2019 Salar Khalilzadeh <salar2k@gmail.com>
*
* SmartProxy is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* SmartProxy is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SmartProxy. If not, see <http://www.gnu.org/licenses/>.
*/
import { browser, environment } from "../../lib/environment";
import { jQuery, messageBox } from "../../lib/External";
import { Messages, PopupInternalDataType, ProxyModeType, ProxyableDomainType, FailedRequestType, ProxyServer } from "../../core/definitions";
import { PolyFill } from "../../lib/PolyFill";
import { CommonUi } from "./CommonUi";
import { Utils } from "../../lib/Utils";
export class popup {
private static popupData: PopupInternalDataType = null;
public static initialize() {
popup.onDocumentReady(popup.bindEvents);
PolyFill.runtimeSendMessage(Messages.PopupGetInitialData,
(dataForPopup: PopupInternalDataType) => {
if (dataForPopup != null) {
popup.popupData = dataForPopup;
popup.populateDataForPopup(dataForPopup);
}
},
(error: Error) => {
PolyFill.runtimeSendMessage("PopupGetInitialData failed! > " + error);
});
// start handling messages
browser.runtime.onMessage.addListener(popup.handleMessages);
popup.onDocumentReady(CommonUi.localizeHtmlPage);
}
private static handleMessages(message: string, sender: any, sendResponse: Function) {
if (typeof (message) == "object") {
if (message["command"] === Messages.WebFailedRequestNotification) {
if (message["tabId"] == null)
return;
let sourceTabId = popup.popupData.currentTabId;
let tabId = message["tabId"];
if (tabId != sourceTabId) {
return;
}
let failedRequests: FailedRequestType[] = message["failedRequests"];
// display the failed requests
popup.populateFailedRequests(failedRequests);
// Chrome requires a response
if (sendResponse)
sendResponse(null);
return;
}
}
// Chrome requires a response
if (sendResponse)
sendResponse(null);
}
private static onDocumentReady(callback: Function) {
jQuery(document).ready(callback);
}
private static bindEvents() {
jQuery("#openSettings").click(() => {
PolyFill.runtimeOpenOptionsPage();
popup.closeSelf();
});
jQuery("#openProxyable").click(() => {
if (!popup.popupData)
return;
var sourceTabId = popup.popupData.currentTabId;
browser.tabs.create(
{
active: true,
//openerTabId: null,
url: browser.extension.getURL(`ui/proxyable.html?id=${sourceTabId}`)
}
);
popup.closeSelf();
});
jQuery("#divFailedRequests a").click(() => {
jQuery(".popup-menu-failed").toggle();
});
jQuery("#btnAddFailedRequests").click(popup.onAddFailedRequestsClick);
}
private static populateDataForPopup(dataForPopup: PopupInternalDataType) {
popup.populateUpdateAvailable(dataForPopup);
popup.populateUnsupportedFeatures(dataForPopup);
popup.populateProxyMode(dataForPopup.proxyMode);
popup.populateActiveProxy(dataForPopup);
popup.populateProxyableDomainList(dataForPopup.proxyableDomains);
popup.populateFailedRequests(dataForPopup.failedRequests);
}
private static populateUpdateAvailable(dataForPopup: PopupInternalDataType) {
if (dataForPopup.updateAvailableText && dataForPopup.updateInfo) {
jQuery("#divUpdateIsAvailable").show()
.find("a")
.text(dataForPopup.updateAvailableText)
.attr("href", dataForPopup.updateInfo.downloadPage);
}
}
private static populateUnsupportedFeatures(dataForPopup: PopupInternalDataType) {
if (dataForPopup.notSupportedSetProxySettings) {
jQuery("#linkSystemProxy").hide();
}
}
private static populateProxyMode(proxyMode: ProxyModeType) {
let divProxyMode = jQuery("#divProxyMode");
divProxyMode.find("li.disabled a").css("cursor", "default");
divProxyMode.find(".nav-link").removeClass("active");
divProxyMode.find(`.nav-link[data-proxyMode=${proxyMode}]`)
.addClass("active")
.parent("li");
divProxyMode.find(".nav-link:not(.disabled)")
.on("click", popup.onProxyModeClick);
}
private static populateActiveProxy(dataForPopup: PopupInternalDataType) {
let divActiveProxy = jQuery("#divActiveProxy");
let cmbActiveProxy = divActiveProxy.find("#cmbActiveProxy");
if (!dataForPopup.proxyServers)
dataForPopup.proxyServers = [];
if (!dataForPopup.proxyServersSubscribed)
dataForPopup.proxyServersSubscribed = [];
// remove previous items
cmbActiveProxy.find("option").remove();
if (dataForPopup.proxyServers.length > 1 ||
dataForPopup.proxyServersSubscribed.length) {
// display select combo
divActiveProxy.show();
let activeProxyName = "";
if (dataForPopup.activeProxyServer != null) {
activeProxyName = dataForPopup.activeProxyServer.name;
}
// display select options
jQuery.each(dataForPopup.proxyServers, (index: number, proxyServer: ProxyServer) => {
// proxyServer
let $option = jQuery("<option>")
.attr("value", proxyServer.name)
.text(proxyServer.name)
.appendTo(cmbActiveProxy);
$option.prop("selected", (proxyServer.name === activeProxyName));
});
if (dataForPopup.proxyServersSubscribed.length > 0) {
let subscriptionGroup = jQuery("<optgroup>")
// -Subscriptions-
.attr("label", browser.i18n.getMessage("popupSubscriptions"))
.appendTo(cmbActiveProxy);
dataForPopup.proxyServersSubscribed.forEach(proxyServer => {
// proxyServer
let $option = jQuery("<option>")
.attr("value", proxyServer.name)
.text(proxyServer.name)
.appendTo(subscriptionGroup);
$option.prop("selected", (proxyServer.name === activeProxyName));
});
}
cmbActiveProxy.on("change", popup.onActiveProxyChange);
} else {
// for one or less we dont show the select proxy
divActiveProxy.hide();
}
}
private static populateProxyableDomainList(proxyableDomainList: ProxyableDomainType[]) {
if (!proxyableDomainList || !proxyableDomainList.length) return;
var divProxyableContainer = jQuery("#divProxyableContainer");
var divProxyableDomain = divProxyableContainer.find("#divProxyableDomains");
var divProxyableDomainItem = divProxyableDomain.find("#divProxyableDomainItem");
// display the list container
divProxyableContainer.show();
// this is proxyable
jQuery("#openProxyable").show();
for (let i = 0; i < proxyableDomainList.length; i++) {
let domainResult = proxyableDomainList[i];
let domain = domainResult.domain;
let item = divProxyableDomainItem.clone();
item.show()
.find("span.proxyable-host-name")
.text(domain);
item.appendTo(divProxyableDomain);
item.data("domainResult", domainResult);
//item.data("host-name", domain);
//item.data("ruleIsForThisHost", ruleIsForThisHost);
//item.data("hasMatchingRule", domainResult.hasMatchingRule);
var itemIcon = item.find(".proxyable-status-icon");
if (domainResult.hasMatchingRule) {
itemIcon.removeClass("fa-square")
.addClass("fa-check-square");
// if the matching rule is not for this host
if (!domainResult.ruleIsForThisHost) {
item.find(".nav-link").addClass("disabled")
.attr("title", `Enabled by other domains`);
}
} else {
itemIcon.removeClass("fa-check-square")
.addClass("fa-square");
}
item.on("click", popup.onProxyableDomainClick);
divProxyableDomainItem.hide();
}
}
private static populateFailedRequests(failedRequests: FailedRequestType[]) {
var divFailedRequests = jQuery("#divFailedRequests");
if (failedRequests && failedRequests.length) {
let failedRequestCount = 0;
let failedRequestsItemsContainer = jQuery(".popup-menu-failed .failed-request-container");
// the item template
let failedRequestsItemTemplate = failedRequestsItemsContainer.find(".failed-request-template");
failedRequestsItemTemplate.hide();
// save checked domains, preventing check change on refresh
let domainsStatus: { [index: string]: any } = {};
failedRequestsItemsContainer.find(".request-box input:checkbox").each((index: number, e: any) => {
var element = jQuery(e);
domainsStatus[element.attr("data-domain")] = element.prop("checked");
});
// remove previous items
failedRequestsItemsContainer.find(".request-box:not(.failed-request-template)").remove();
// Order by parent domain
failedRequests = failedRequests.sort((a, b) => {
if (a._domainSortable === null)
a._domainSortable = Utils.reverseString(a.domain);
if (b._domainSortable === null)
b._domainSortable = Utils.reverseString(b.domain);
if (a._domainSortable > b._domainSortable)
return 1;
if (a._domainSortable < b._domainSortable)
return -1;
return 0;
});
for (let i = 0; i < failedRequests.length; i++) {
let request = failedRequests[i];
if (request.hasRule || request.ignored)
// don't add if the request has rule
continue;
let newItem = failedRequestsItemTemplate.clone();
newItem.find(".request-name a").attr("href", request.url);
newItem.find(".request-name label>span").text(request.domain);
let newItemCheckbox = newItem.find("input");
newItemCheckbox.attr("data-domain", request.domain);
if (request.isRootHost) {
failedRequestCount += request.hitCount;
newItemCheckbox.prop("checked", false);
} else {
newItem.find(".failed-request-root").show();
newItemCheckbox.prop("checked", true);
newItem.addClass("request-box-dependant");
}
newItem.find(".failed-request-count").text(request.hitCount).show();
// set previous status, preventing check change on refresh
let previousStatus = domainsStatus[request.domain];
if (previousStatus != null) {
newItemCheckbox.prop("checked", previousStatus);
}
newItem.removeClass("failed-request-template");
newItem.show();
failedRequestsItemsContainer.append(newItem);
}
// updating the failed request count
divFailedRequests.find("#lblFailedRequestCount").text(failedRequestCount);
if (failedRequestCount) {
// display the failed requests block
divFailedRequests.show();
} else {
divFailedRequests.hide();
}
} else {
divFailedRequests.hide();
}
}
//#region Events
private static onProxyModeClick() {
let element = jQuery(this);
let selectedProxyMode = element.attr("data-proxyMode");
if (popup.popupData.notAllowedSetProxySettings &&
selectedProxyMode == ProxyModeType.SystemProxy) {
let message: string;
if (environment.chrome)
message = browser.i18n.getMessage("popupNotAllowedSetProxySettingsChrome");
else
message = browser.i18n.getMessage("popupNotAllowedSetProxySettingsFirefox");
messageBox.error(message, 5000);
return;
}
// change proxy mode
PolyFill.runtimeSendMessage({
command: Messages.PopupChangeProxyMode,
proxyMode: selectedProxyMode
});
if (selectedProxyMode != ProxyModeType.Direct &&
selectedProxyMode != ProxyModeType.SystemProxy &&
!popup.popupData.hasProxyServers) {
// open the settings page
PolyFill.runtimeOpenOptionsPage();
}
popup.closeSelf();
}
private static onActiveProxyChange() {
let cmbActiveProxy = jQuery("#divActiveProxy #cmbActiveProxy");
let value = cmbActiveProxy.val();
if (!value) return;
PolyFill.runtimeSendMessage(
{
command: Messages.PopupChangeActiveProxyServer,
name: value
});
}
private static onProxyableDomainClick() {
let domainResult: ProxyableDomainType = jQuery(this).data("domainResult");
let domain = domainResult.domain;
let hasMatchingRule = domainResult.hasMatchingRule;
let ruleIsForThisHost = domainResult.ruleIsForThisHost;
if (!hasMatchingRule || (hasMatchingRule && ruleIsForThisHost == true)) {
PolyFill.runtimeSendMessage(`proxyable-host-name: ${domain}`);
PolyFill.runtimeSendMessage({
command: Messages.PopupToggleProxyForDomain,
domain: domain
});
popup.closeSelf();
} else {
PolyFill.runtimeSendMessage(`rule is not for this domain: ${domain}`);
}
}
private static onAddFailedRequestsClick() {
let domainList: string[] = [];
jQuery(".failed-request-container .request-box input:checked").each((index: number, e: any) => {
let element = jQuery(e);
let domain = element.attr("data-domain");
if (domain)
domainList.push(domain);
});
if (domainList.length)
// Add the selected domains to rule list?
if ((!environment.chrome && environment.version < environment.bugFreeVersions.firefoxConfirmInPopupWorks) ||
confirm(browser.i18n.getMessage("popupAddFailedRequestsConfirm"))) {
// send message to the core
PolyFill.runtimeSendMessage(
{
command: Messages.PopupAddDomainListToProxyRule,
domainList: domainList,
tabId: popup.popupData.currentTabId
},
(response: any) => {
if (!response) return;
if (response.failedRequests) {
// display the failed requests
popup.populateFailedRequests(response.failedRequests);
}
});
popup.closeSelf();
}
}
private static closeSelf() {
if (!environment.mobile) {
window.close();
}
else {
PolyFill.tabsGetCurrent(details => {
return PolyFill.tabsRemove(details.id);
});
}
}
//#endregion
}
popup.initialize();