forked from liamstewart23/ChromeExt-WebsiteIP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
148 lines (128 loc) · 4.81 KB
/
options.js
File metadata and controls
148 lines (128 loc) · 4.81 KB
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
// Function to load saved IP-name associations
function loadSavedIpNames() {
chrome.storage.sync.get('ipNames', function(data) {
if (chrome.runtime.lastError) {
return;
}
const ipNames = data.ipNames || [];
const container = document.getElementById('ip-entries');
container.innerHTML = '';
if (ipNames.length === 0) {
addNewEntry();
} else {
ipNames.forEach((entry, index) => {
addEntryToUI(entry.ip, entry.name, index);
});
}
});
}
// Function to add a new entry to the interface
function addEntryToUI(ip = '', name = '', index) {
const container = document.getElementById('ip-entries');
const entryDiv = document.createElement('div');
entryDiv.className = 'ip-entry';
entryDiv.dataset.index = index;
// Create elements programmatically to prevent XSS
const ipInput = document.createElement('input');
ipInput.type = 'text';
ipInput.className = 'ip-input';
ipInput.placeholder = 'Indirizzo IP';
ipInput.value = ip;
ipInput.maxLength = 45;
const nameInput = document.createElement('input');
nameInput.type = 'text';
nameInput.className = 'name-input';
nameInput.placeholder = 'Nome';
nameInput.value = name;
nameInput.maxLength = 10;
const deleteBtn = document.createElement('button');
deleteBtn.className = 'delete-btn';
deleteBtn.textContent = 'Elimina';
const errorDiv = document.createElement('div');
errorDiv.className = 'error';
entryDiv.appendChild(ipInput);
entryDiv.appendChild(nameInput);
entryDiv.appendChild(deleteBtn);
entryDiv.appendChild(errorDiv);
container.appendChild(entryDiv);
// Add event listener to the delete button
deleteBtn.addEventListener('click', function() {
entryDiv.remove();
saveEntries();
});
// Add event listener to the inputs to save automatically
ipInput.addEventListener('change', saveEntries);
nameInput.addEventListener('change', saveEntries);
}
// Function to add a new empty entry
function addNewEntry() {
const container = document.getElementById('ip-entries');
const currentEntries = container.querySelectorAll('.ip-entry');
addEntryToUI('', '', currentEntries.length);
}
// Validate IPv4 address with proper octet range check
function isValidIPv4(ip) {
const parts = ip.split('.');
if (parts.length !== 4) return false;
return parts.every(part => {
const num = parseInt(part, 10);
return !isNaN(num) && num >= 0 && num <= 255 && part === num.toString();
});
}
// Validate IPv6 address (supports full and compressed formats)
function isValidIPv6(ip) {
// Full IPv6 or compressed format
const ipv6Pattern = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9]))$/;
return ipv6Pattern.test(ip);
}
// Function to save all entries
function saveEntries() {
const entries = document.querySelectorAll('.ip-entry');
const ipNames = [];
let hasError = false;
entries.forEach(entry => {
const ipInput = entry.querySelector('.ip-input');
const nameInput = entry.querySelector('.name-input');
const errorDiv = entry.querySelector('.error');
const ip = ipInput.value.trim();
const name = nameInput.value.trim();
// Validation
errorDiv.textContent = '';
if (ip && name) {
// Proper IP validation for IPv4 and IPv6
if (!isValidIPv4(ip) && !isValidIPv6(ip)) {
errorDiv.textContent = 'Invalid IP format';
hasError = true;
} else if (name.length > 10) {
errorDiv.textContent = 'The name must be max 10 characters';
hasError = true;
} else {
ipNames.push({ ip, name });
}
}
});
if (!hasError) {
chrome.storage.sync.set({ ipNames }, function() {
const status = document.getElementById('status');
if (chrome.runtime.lastError) {
status.textContent = 'Error saving settings';
status.style.color = '#f44336';
setTimeout(() => {
status.textContent = '';
status.style.color = '';
}, 1500);
return;
}
status.textContent = 'Settings saved!';
status.style.color = '';
setTimeout(() => { status.textContent = ''; }, 1500);
});
}
}
// Initialization
document.addEventListener('DOMContentLoaded', function() {
loadSavedIpNames();
document.getElementById('add-new').addEventListener('click', function() {
addNewEntry();
});
});