-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
81 lines (66 loc) · 2.8 KB
/
script.js
File metadata and controls
81 lines (66 loc) · 2.8 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
import { funciones, parse_calendar, build_type_subtype_map } from "./funciones.js";
const type_subtype_map = build_type_subtype_map();
document.getElementById('pasteButton').addEventListener('click', async () => {
try {
// Solicita acceso al portapapeles y pega el contenido
const text = await navigator.clipboard.readText();
// Mostrar el contenido pegado
document.getElementById('output').textContent = text;
// Convertir el texto a formato ICS
const icsData = convertToICS(text);
// Crear un archivo descargable
const blob = new Blob([icsData], { type: 'text/calendar' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'eventos.ics';
// Agregar el enlace al DOM temporalmente y hacer click
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} catch (err) {
console.error('Failed to read clipboard contents: ', err);
}
});
// Función para convertir el texto pegado en formato ICS
function convertToICS(text) {
// const lines = text.split('\n').filter(line => line.trim() !== '');
const lines = parse_calendar(text);
let icsContent = `BEGIN:VCALENDAR\nVERSION:2.0\nCALSCALE:GREGORIAN\n`;
lines.forEach(line => {
const [code_ser, code_fun, startDate, endDate, from, to, tip_cur, flightNumber, loc] = line.map(item => item.trim());;
const start = formatICSTime(startDate);
const end = formatICSTime(endDate);
icsContent += `BEGIN:VEVENT\n`;
icsContent += `UID:${start}${end}\n`;
icsContent += `DTSTAMP:${start}\n`;
icsContent += `DTSTART:${start}\n`;
icsContent += `DTEND:${end}\n`;
if (code_fun) {
icsContent += `DESCRIPTION:${funciones[code_fun]}\n`;
}
if (flightNumber) {
icsContent += `SUMMARY:${flightNumber}\n`;
} else if (code_ser === "CO") {
icsContent += `SUMMARY:Vuelo\n`;
} else {
icsContent += `SUMMARY:${code_ser}\n`;
icsContent += `DESCRIPTION:${type_subtype_map[code_ser].description.trim()}\n`
}
if (from === to) {
icsContent += `LOCATION:${from}\n`;
} else {
icsContent += `LOCATION:${from} a ${to}\n`;
}
icsContent += `END:VEVENT\n`;
});
icsContent += `END:VCALENDAR`;
return icsContent;
}
// Función para formatear fecha y hora en formato ICS (YYYYMMDDTHHMMSSZ)
function formatICSTime(datetime) {
const date = datetime.split(' ')[0];
const time = datetime.split(' ')[1]
const [day, month, year] = date.split('/');
const [hour, minute] = time.split(':');
return `${year}${month}${day}T${hour}${minute}00Z`; // Ajustar según sea necesario
}