-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcert.js
150 lines (134 loc) · 3.64 KB
/
cert.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
/**
* @fileoverview Certificate and Host Verification Module
* Handles I2P certificate verification and host information display
*/
// Constants
const I2P_SUFFIX = ".i2p";
const B32_SUFFIX = "b32.i2p";
const HTTPS_PREFIX = "https";
const HOST_LENGTH_THRESHOLD = 51;
/**
* Message keys for i18n
* @enum {string}
*/
const MessageKeys = {
SITE_LABEL: "siteLabel",
CERT_LABEL: "CertLabel",
IS_HOSTNAME: "isHostName",
IS_BASE32: "isBase32",
CERT_PRESENT: "certPresent",
CERT_ABSENT: "certAbsent",
};
/**
* DOM element IDs
* @enum {string}
*/
const ElementIds = {
TYPE_LABEL: "TypeLabel",
CERT_LABEL: "CertLabel",
ADDRESS_INFO: "AddressInfo",
ADDRESS_CERT_INFO: "AddressCertInfo",
SIGNED_LABEL: "SignedLabel",
};
/**
* Updates element content with i18n message
* @param {string} elementId - Target DOM element ID
* @param {string} messageKey - i18n message key
* @return {void}
*/
function updateContent(elementId, messageKey) {
const element = document.getElementById(elementId);
if (!element) {
console.warn(`Element not found : ${elementId}`);
return;
}
const message = chrome.i18n.getMessage(messageKey);
if (!message) {
console.warn(`Translation missing for: ${messageKey}`);
return;
}
element.textContent = message;
}
/**
* Clears content of specified element
* @param {string} elementId - Target DOM element ID
* @return {void}
*/
function clearContent(elementId) {
const element = document.getElementById(elementId);
if (!element) {
console.warn(`Element not found : ${elementId}`);
return;
}
element.textContent = "";
}
/**
* Extracts I2P host from URL
* @param {string} url - Full URL
* @return {string} I2P host
*/
function extractI2PHost(url) {
const baseHost = url.split(I2P_SUFFIX)[0] + I2P_SUFFIX;
return baseHost;
}
/**
* Determines host type and updates UI
* @param {string} host - I2P host
* @return {void}
*/
function updateHostTypeInfo(host) {
if (host.length < HOST_LENGTH_THRESHOLD) {
updateContent(ElementIds.ADDRESS_INFO, MessageKeys.IS_HOSTNAME);
} else if (host.endsWith(B32_SUFFIX)) {
updateContent(ElementIds.ADDRESS_INFO, MessageKeys.IS_BASE32);
}
}
/**
* Handles certificate verification and UI updates
* @param {string} url - Page URL
* @param {string} host - I2P host
* @return {Promise<void>}
*/
async function handleCertificateVerification(url, host) {
if (url.startsWith(HTTPS_PREFIX)) {
updateContent(ElementIds.ADDRESS_CERT_INFO, MessageKeys.CERT_PRESENT);
try {
const response = await fetch(host);
console.info("Certificate verification completed:", response);
} catch (error) {
console.error("Certificate verification failed:", error);
}
} else {
updateContent(ElementIds.ADDRESS_CERT_INFO, MessageKeys.CERT_ABSENT);
clearContent(ElementIds.SIGNED_LABEL);
}
}
/**
* Processes active tab information
* @param {browser.tabs.Tab[]} tabs - Active tab information
* @return {Promise<void>}
*/
async function processActiveTab(tabs) {
if (!tabs || !tabs[0] || !tabs[0].url) {
console.error("Invalid tab information");
return;
}
const url = tabs[0].url;
const host = extractI2PHost(url);
updateHostTypeInfo(host);
await handleCertificateVerification(url, host);
}
/**
* Initializes the certificate verification UI
* @return {void}
*/
function initializeCertUI() {
updateContent(ElementIds.TYPE_LABEL, MessageKeys.SITE_LABEL);
updateContent(ElementIds.CERT_LABEL, MessageKeys.CERT_LABEL);
browser.tabs
.query({ active: true })
.then(processActiveTab)
.catch((error) => console.error("Tab processing failed:", error));
}
// Initialize certificate verification
initializeCertUI();