-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
158 lines (123 loc) · 5.58 KB
/
index.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
// Validate newMac format (6 octets separated by colons)
const macRegex = /^([0-9A-F]{2}:){5}[0-9A-F]{2}$/i;
// Function to validate MAC address.
const isValidMac = (mac) => /^([0-9A-F]{2}:){5}[0-9A-F]{2}$/i.test(mac);
function autoUpdateDeviceMac(url, param) {
// Create a URL object
const parsedUrl = new URL(url);
let macAddress = parsedUrl.searchParams.get(param);
// Split the mac address into octets
const macOctets = macAddress.split(':');
const validOctets = macOctets.length > 6 ? macOctets.slice(0, 3) : macOctets;
// Randomize the last three octets (ensure they stay valid hex values)
for (let i = 3; i < 6; i++) {
validOctets[i] = Math.floor(Math.random() * 16).toString(16).padStart(2, '0');
}
// Rebuild the new mac address
const newMac = validOctets.join(':');
// Update the chosen parameter (if any)
if (param) {
parsedUrl.searchParams.set(param, newMac);
} else {
macError.textContent = `MAC address not found. Please check the input URL.`
}
// Return the updated URL
return parsedUrl.toString();
}
const list = document.getElementById('mac-param-list');
function populateMacParamRadioButtons(potentialMacParams) {
list.innerHTML = ''; // Clear existing list items
potentialMacParams.forEach((param, index) => {
const listItem = document.createElement('li');
listItem.classList.add('w-full', 'border-b', 'border-gray-200', 'dark:border-gray-600');
const div = document.createElement('div');
div.classList.add('flex', 'items-center', 'ps-3');
const input = document.createElement('input');
input.type = 'radio';
input.id = `list-radio-${index}`;
input.name = 'list-radio';
input.value = param;
input.classList.add('w-4', 'h-4', 'text-blue-600', 'bg-gray-100', 'border-gray-300', 'focus:ring-blue-500', 'dark:focus:ring-blue-600', 'dark:ring-offset-gray-700', 'dark:focus:ring-offset-gray-700', 'focus:ring-2', 'dark:bg-gray-600', 'dark:border-gray-500');
const label = document.createElement('label');
label.setAttribute('for', input.id);
label.classList.add('w-full', 'py-3', 'ms-2', 'text-sm', 'font-medium', 'text-gray-900', 'dark:text-gray-300');
label.textContent = param; // Use the MAC parameter as the label text
// Select first
if (index === 0) {
input.checked = true;
}
// Append elements
div.appendChild(input);
div.appendChild(label);
listItem.appendChild(div);
list.appendChild(listItem);
});
}
const refreshButton = document.getElementById('refresh-button');
const regenButton = document.getElementById('regen-button');
const updatedUrl = document.getElementById('updated-url');
const macError = document.getElementById('mac-error');
const copyButton = document.getElementById('copy-button');
const $defaultMessage = document.getElementById('default-message');
const $successMessage = document.getElementById('success-message');
const dynamicLink = document.getElementById('dynamic-link');
const originalUrl = document.getElementById('original-url');
const macParamListHeader = document.getElementById('mac-param-list-header');
regenButton.addEventListener('click', () => {
macError.textContent = "";
try {
const selectedRadio = document.querySelector('input[name="list-radio"]:checked');
const selectedParam = selectedRadio.value;
const updatedURL = autoUpdateDeviceMac(originalUrl.value, selectedParam);
updatedUrl.textContent = `${updatedURL}`;
dynamicLink.href = `${updatedURL}`;
regenButton.classList.add('focus:ring-4')
setTimeout(() => {
regenButton.classList.remove('focus:ring-4')
})
} catch (error) {
console.error(error);
//updatedUrl.textContent = `Error: ${error.message}`;
}
});
copyButton.addEventListener('click', () => {
// Use Clipboard API if available
try {
if (updatedUrl.textContent.length < 5) {
macError.textContent = `Enter a valid input URL.`
throw new Error(`The input URL is empty: ${updatedUrl.textContent}`)
}
navigator.clipboard.writeText(updatedUrl.textContent);
copyButton.classList.add('focus:ring-4', 'bg-blue-500', 'text-white')
copyButton.classList.remove('outline-1', 'outline-blue-500', 'text-blue-500')
$defaultMessage.classList.add('hidden');
$successMessage.classList.remove('hidden');
} catch {
macError.textContent = `Error copying to clipboard. Please try manually.`
}
// reset to default state
setTimeout(() => {
$defaultMessage.classList.remove('hidden');
$successMessage.classList.add('hidden');
copyButton.classList.remove('focus:ring', 'bg-blue-500', 'text-white')
copyButton.classList.add('outline-1', 'outline-blue-500', 'text-blue-500')
}, 2000);
});
originalUrl.addEventListener('input', () => {
if (originalUrl.value < 1) {
list.classList.add('hidden');
macParamListHeader.classList.add('hidden')
} else {
const parsedUrl = new URL(originalUrl.value) || "";
list.classList.remove('hidden');
macParamListHeader.classList.remove('hidden')
const potentialMacParams = [];
for (const [key, value] of parsedUrl.searchParams.entries()) {
if (isValidMac(value) && !['ap_mac', 'apmac'].includes(key)) {
console.log(key);
potentialMacParams.push(key);
}
}
populateMacParamRadioButtons(potentialMacParams);
}
})