-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathproxyinfo.js
215 lines (197 loc) · 5.57 KB
/
proxyinfo.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
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
/**
* @fileoverview I2P Proxy Status Manager
* Handles proxy connectivity checking and UI updates for I2P extension
*/
// Constants
const PROXY_CONFIG = {
PROXY_URL: "http://proxy.i2p",
CONSOLE_URL: "http://127.0.0.1:7657",
LOGO_PATH: "/themes/console/light/images/i2plogo.png",
FETCH_OPTIONS: { cache: "no-store" },
};
const UI_ELEMENTS = {
PROXY_STATUS: "proxy-check",
READINESS_CLASS: ".readyness",
CONSOLE_LINKS: ".application-info",
HIDDEN_CLASS: "hidden",
};
const MESSAGE_KEYS = {
SUCCESS: "proxySuccessStatus",
FAILURE: "proxyFailedStatus",
};
/**
* UI Manager for handling element visibility
*/
class ProxyUIManager {
/**
* Toggle element visibility
* @param {Element|NodeList} elements - Elements to modify
* @param {boolean} show - Whether to show or hide
*/
/**
* Toggle element visibility with strict null checking
* @param {Element|NodeList} elements - Elements to modify
* @param {boolean} show - Whether to show or hide
*/
static toggleVisibility(elements, show) {
try {
// Validate input
if (!elements) {
throw new Error("Elements parameter is null or undefined");
}
// Convert to array if NodeList
const elementArray =
elements instanceof NodeList ? Array.from(elements) : [elements];
elementArray.forEach((element) => {
// Explicit null check for element and style property
if (element && element.style !== undefined && element.style !== null) {
const action = show ? "remove" : "add";
element.classList[action](UI_ELEMENTS.HIDDEN_CLASS);
console.debug(`(proxyinfo) ${show ? "showing" : "hiding"} element`);
} else {
console.warn(
"(proxyinfo) Invalid element encountered during visibility toggle"
);
}
});
} catch (error) {
console.error("Visibility toggle failed:", error);
throw error; // Re-throw for error boundary handling
}
}
/**
* Update element content by ID
* @param {string} elementId - Target element ID
* @param {string} messageKey - i18n message key
*/
static updateContent(elementId, messageKey) {
try {
const element = document.getElementById(elementId);
if (!element) {
throw new Error(`Element not found : ${elementId}`);
}
element.textContent = chrome.i18n.getMessage(messageKey);
} catch (error) {
console.error("Content update failed:", error);
}
}
/**
* Get elements by selector
* @param {string} selector - CSS selector
* @return {?NodeList}
*/
static getElements(selector) {
try {
return document.querySelectorAll(selector);
} catch (error) {
console.error("Element selection failed:", error);
return null;
}
}
}
/**
* Proxy Status Manager
*/
class ProxyStatusManager {
/**
* Check proxy connectivity
* @return {Promise<void>}
*/
static async checkProxyStatus() {
console.info("(proxyinfo) Checking proxy status");
try {
const response = await fetch(
PROXY_CONFIG.PROXY_URL,
PROXY_CONFIG.FETCH_OPTIONS
);
await this.handleProxySuccess(response);
} catch (error) {
await this.handleProxyError(error);
}
}
/**
* Handle successful proxy connection
* @param {Response} response - Fetch response
*/
static async handleProxySuccess(response) {
console.info("(proxyinfo) Proxy check successful");
ProxyUIManager.updateContent(
UI_ELEMENTS.PROXY_STATUS,
MESSAGE_KEYS.SUCCESS
);
const readinessElements = ProxyUIManager.getElements(
UI_ELEMENTS.READINESS_CLASS
);
if (readinessElements) {
ProxyUIManager.toggleVisibility(readinessElements, true);
}
}
/**
* Handle proxy connection failure
* @param {Error} error - Connection error
*/
static async handleProxyError(error) {
console.error("(proxyinfo) Proxy check failed:", error);
ProxyUIManager.updateContent(
UI_ELEMENTS.PROXY_STATUS,
MESSAGE_KEYS.FAILURE
);
const readinessElements = ProxyUIManager.getElements(
UI_ELEMENTS.READINESS_CLASS
);
if (readinessElements) {
ProxyUIManager.toggleVisibility(readinessElements, false);
}
}
/**
* Check console connectivity
* @return {Promise<void>}
*/
static async checkConsoleStatus() {
const logoUrl = `${PROXY_CONFIG.CONSOLE_URL}${PROXY_CONFIG.LOGO_PATH}`;
console.info("(proxyinfo) Checking console status");
try {
await fetch(logoUrl);
const consoleLinks = ProxyUIManager.getElements(
UI_ELEMENTS.CONSOLE_LINKS
);
if (consoleLinks) {
ProxyUIManager.toggleVisibility(consoleLinks, true);
}
console.info("(proxyinfo) Console check successful");
} catch (error) {
const consoleLinks = ProxyUIManager.getElements(
UI_ELEMENTS.CONSOLE_LINKS
);
if (consoleLinks) {
ProxyUIManager.toggleVisibility(consoleLinks, false);
}
console.error("(proxyinfo) Console check failed:", error);
}
}
}
/**
* Initialize proxy status checking
*/
function initializeProxyChecks() {
try {
ProxyStatusManager.checkProxyStatus();
ProxyStatusManager.checkConsoleStatus();
} catch (error) {
console.error("Proxy initialization failed:", error);
}
}
// Event Listeners
document.addEventListener("DOMContentLoaded", initializeProxyChecks, {
passive: true,
capture: false,
});
// Export for testing
if (typeof module !== "undefined" && module.exports) {
module.exports = {
ProxyStatusManager,
UIManager: ProxyUIManager,
PROXY_CONFIG,
UI_ELEMENTS,
};
}