1
0
Fork 0
CalMerger/script.js

78 lines
3.1 KiB
JavaScript
Raw Normal View History

2024-09-30 12:55:03 +00:00
const form = document.getElementById('merge-form');
2024-10-21 16:47:18 +00:00
const calendars = document.getElementById('calendars');
const addCalendarButton = document.getElementById('add-calendar');
const result = document.getElementById('result');
2024-09-30 12:55:03 +00:00
2024-10-21 17:03:01 +00:00
let calendarIndex = 1;
2024-10-21 20:22:45 +00:00
let mergedUrl = '';
2024-10-21 16:35:12 +00:00
2024-10-21 16:47:18 +00:00
// Function to validate URL format
function isValidUrl(url) {
const urlPattern = /^(https?:\/\/[^\s$.?#].[^\s]*)$/; // Regex for URL validation
return urlPattern.test(url);
}
2024-09-30 12:55:03 +00:00
addCalendarButton.addEventListener('click', () => {
const newCalendar = document.createElement('div');
newCalendar.className = 'calendar';
newCalendar.innerHTML = `
<input type="text" id="prefix-${calendarIndex}" placeholder="Prefix">
<input type="checkbox" id="override-${calendarIndex}">
<label for="override-${calendarIndex}">Override</label>
<input type="url" id="url-${calendarIndex}" placeholder="Calendar URL">
`;
calendars.appendChild(newCalendar);
calendarIndex++;
});
2024-10-21 16:47:18 +00:00
form.addEventListener('submit', (event) => {
event.preventDefault();
const linkGroupName = document.getElementById('link-group-name').value;
const calendarsData = [];
2024-10-21 16:38:05 +00:00
let valid = true; // Flag to track URL validity
2024-10-21 16:41:41 +00:00
for (let i = 0; i < calendarIndex; i++) {
const prefix = document.getElementById(`prefix-${i}`);
const override = document.getElementById(`override-${i}`);
const url = document.getElementById(`url-${i}`);
2024-10-21 16:47:18 +00:00
2024-10-21 12:15:26 +00:00
if (prefix && override && url) {
2024-10-21 16:38:05 +00:00
// Validate the URL
2024-10-21 16:41:41 +00:00
if (!isValidUrl(url.value)) {
valid = false; // Set flag to false if any URL is invalid
alert(`Invalid URL format for calendar ${i + 1}: ${url.value}`);
} else {
calendarsData.push({
prefix: prefix.value,
override: override.checked,
url: url.value
});
}
2024-10-21 12:15:26 +00:00
}
}
2024-10-21 16:47:18 +00:00
if (valid) {
fetch('/merge', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ linkGroupName, calendars: calendarsData })
})
.then((response) => {
if (!response.ok) {
return response.json().then(err => { throw err; });
}
return response.json();
})
.then((data) => {
2024-10-21 20:22:45 +00:00
mergedUrl = data.url;
result.innerHTML = `Merged calendar URL: <a href="${data.url}">${data.url}</a>`;
2024-10-15 11:01:04 +00:00
console.log('Links added successfully!');
})
.catch((error) => {
console.error('Error:', error);
result.innerHTML = `Error merging calendars: ${error.message || 'Unknown error'}`
});
2024-10-21 16:47:18 +00:00
}
2024-10-21 20:22:45 +00:00
});