-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
100 lines (91 loc) · 3.31 KB
/
main.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
const toggleMenu = document.querySelector(".toggle-menu");
const modal = document.querySelector(".modal");
const nav = document.querySelector(".nav");
const sectionsNav = document.querySelector(".nav__left__links");
const sectionsNavClone = sectionsNav.cloneNode(true);
const logging = document.querySelector(".nav__right__links");
const loggingClone = logging.cloneNode(true);
const inputLink = document.querySelector(".input-form");
const boxUrl = document.querySelector(".box-url");
const boxSend = document.querySelector(".btn-form");
let arrayLinks = [];
toggleMenu.addEventListener("click", () => {
modal.classList.toggle("active");
modal.appendChild(sectionsNavClone);
modal.appendChild(loggingClone);
});
boxUrl.addEventListener("submit", (e) => {
e.preventDefault();
if (inputLink.value.trim() == "") {
inputLink.classList.add("errorLink");
} else {
inputLink.classList.remove("errorLink");
boxSend.classList.add("btn-load");
obtenerLink(inputLink.value);
console.log("Procesando...");
}
});
const obtenerLink = async (convertirLink) => {
try {
const res = await fetch(
`https://api.shrtco.de/v2/shorten?url=${convertirLink}`
);
data = await res.json();
guardarInformacion(data);
inputLink.value = "";
} catch (error) {
alert("It seems that an error occurred, please try another url");
console.log(error);
} finally {
boxSend.classList.remove("btn-load");
}
};
const guardarInformacion = (link) => {
const obj = {
linkTextOriginal: link.result.original_link,
linkTextConvertido: link.result.full_short_link,
linkHrefConvertido: link.result.full_short_link,
};
pintarLink(obj)
arrayLinks = [...arrayLinks, obj];
localStorage.setItem("linksCortados", JSON.stringify(arrayLinks));
};
const pintarLink = (element) => {
//template
const containerInfo = document.querySelector(".short-container");
const templete = document.querySelector(".templete").content;
const templeteClone = templete.cloneNode(true);
const fragment = document.createDocumentFragment();
// Crea e insertar el Link
templeteClone.querySelector(".short-content__link").textContent =
element.linkTextOriginal;
templeteClone.querySelector(".short-content__short-link").textContent =
element.linkTextConvertido;
templeteClone.querySelector(".short-content__short-link").href =
element.linkHrefConvertido;
fragment.appendChild(templeteClone);
containerInfo.appendChild(fragment);
// Copiar Enlace
const copyPaste = document.querySelectorAll(".short-content__copy");
copyPaste.forEach((element) => {
element.addEventListener("click", () => {
const prevSiblings = element.previousElementSibling;
element.classList.add("active-btn");
element.textContent = "Copied!";
setTimeout(() => {
element.textContent = "Copy";
element.classList.remove("active-btn");
}, 3500);
navigator.clipboard.writeText(prevSiblings.textContent);
});
});
};
//Verificar si existenten links en el local storage
if (localStorage.getItem("linksCortados")) {
const listaDeLinks = JSON.parse(localStorage.getItem("linksCortados"));
arrayLinks = listaDeLinks
//Si hay los pintara
arrayLinks.forEach(item => {
pintarLink(item)
});
}