-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
proxy_error_handler.js
137 lines (117 loc) · 4.05 KB
/
proxy_error_handler.js
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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview This file implements the ProxyErrorHandler class, which will
* flag proxy errors in a visual way for the extension's user.
*
* @author Mike West <[email protected]>
*/
/**
* The proxy error handling object. Binds to the 'onProxyError' event, and
* changes the extensions badge to reflect the error state (yellow for
* non-fatal errors, red for fatal).
*
* @constructor
*/
function ProxyErrorHandler() {
// Handle message events from popup.
chrome.extension.onRequest.addListener(this.handleOnRequest_.bind(this));
// Handle proxy error events.
chrome.proxy.onProxyError.addListener(this.handleError_.bind(this));
};
///////////////////////////////////////////////////////////////////////////////
/**
* @typedef {{fatal: boolean, error: string, details: string}}
*/
ProxyErrorHandler.ErrorDetails;
///////////////////////////////////////////////////////////////////////////////
ProxyErrorHandler.prototype = {
/**
* Details of the most recent error.
* @type {?ProxyErrorHandler.ErrorDetails}
* @private
*/
lastError_: null,
/**
* Handle request messages from the popup.
*
* @param {!{type:string}} request The external request to answer.
* @param {!MessageSender} sender Info about the script context that sent
* the request.
* @param {!function} sendResponse Function to call to send a response.
* @private
*/
handleOnRequest_: function(request, sender, sendResponse) {
if (request.type === 'getError') {
sendResponse({result: this.getErrorDetails()});
} else if (request.type === 'clearError') {
this.clearErrorDetails();
sendResponse({result: true});
}
},
/**
* Handles the error event, storing the error details for later use, and
* badges the browser action icon.
*
* @param {!ProxyErrorHandler.ErrorDetails} details The error details.
* @private
*/
handleError_: function(details) {
var xhttp = new XMLHttpRequest();
xhttp.addEventListener("error", transferFailed );
xhttp.addEventListener("load", transferComplete);
xhttp.open("GET", "https://geoip.nekudo.com/api/", true);
xhttp.send();
function transferFailed(){
var RED = [255, 0, 0, 255];
var YELLOW = [255, 205, 0, 255];
// Badge the popup icon.
var color = details.fatal ? RED : YELLOW;
chrome.browserAction.setBadgeBackgroundColor({color: color});
chrome.browserAction.setBadgeText({text: 'X'});
chrome.browserAction.setTitle({
title: chrome.i18n.getMessage('errorPopupTitle', details.error)
});
}
// Store the error for display in the popup.
this.lastError_ = JSON.stringify(details);
function transferComplete(){
if(document.getElementById('proxyFail') !== null){
document.getElementById('proxyFail').setAttribute('hidden', 'hidden');
var GREEN = [124, 252, 0, 255];
chrome.browserAction.setBadgeText({text: 'o'});
chrome.browserAction.setBadgeBackgroundColor({color: GREEN});
chrome.browserAction.setTitle({
title: chrome.i18n.getMessage('connectedPopupTitle')
});
}
}
},
/**
* Returns details of the last error handled.
*
* @return {?ProxyErrorHandler.ErrorDetails}
*/
getErrorDetails: function() {
return this.lastError_;
},
/**
* Clears last handled error.
*/
clearErrorDetails: function() {
chrome.browserAction.setBadgeText({text: ''});
chrome.browserAction.setTitle({
title: chrome.i18n.getMessage('extDescription')
});
if(window.localStorage['proxyConfig'][20] == "f"){
var GREEN = [124, 252, 0, 255];
chrome.browserAction.setBadgeText({text: 'o'});
chrome.browserAction.setBadgeBackgroundColor({color: GREEN});
chrome.browserAction.setTitle({
title: chrome.i18n.getMessage('connectedPopupTitle')
});
}
this.lastError_ = null;
}
}