-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
55 lines (47 loc) · 1.39 KB
/
script.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
const fileInput = document.querySelector('#inputContactos');
const btn = document.querySelector('#btnDescargar');
fileInput.addEventListener('change', () => {
const file = fileInput.files[0];
const reader = new FileReader();
let i = 0;
reader.addEventListener('load', () => {
const lines = reader.result.split('\n');
let vcf = '';
console.log(lines[0].split(', ').length)
if (lines[0].split(', ').length == 2){
for (const line of lines) {
const [nombre, telefono] = line.split(', ');
vcf += `BEGIN:VCARD
VERSION:3.0
FN:${nombre}
TEL;TYPE=CELL:${telefono}
END:VCARD
`;
}
}else if(lines[0].split(', ').length == 1){
for (const line of lines) {
const telefono = line;
const nombrePred = document.getElementById('nombreContacto').value;
let nombre = `${nombrePred}${i}`
console.log(nombre, telefono)
vcf += `BEGIN:VCARD
VERSION:3.0
FN:${nombre}
TEL;TYPE=CELL:${telefono}
END:VCARD
`;
i++;
}
}else{
alert("Error en el archivo txt")
}
btn.addEventListener('click', () => {
const blob = new Blob([vcf], { type: 'text/vcard' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'contactos.vcf';
link.click();
})
});
reader.readAsText(file);
});