-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
179 lines (157 loc) · 4.97 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const seasons = {
1: 13,
2: 22,
3: 24,
4: 22,
5: 22,
6: 25,
7: 25,
8: 25,
9: 25,
10: 23,
11: 22,
12: 21,
13: 22,
14: 22,
15: 22,
16: 21,
17: 22,
18: 22,
19: 20,
20: 21,
21: 23,
22: 22,
23: 22,
24: 22,
25: 22,
26: 22,
27: 22,
28: 21,
29: 21,
30: 23,
31: 22,
32: 22,
33: 22,
34: 22,
};
function saveToLocalStorage() {
localStorage.setItem('fuente', fuente);
localStorage.setItem('temporada', document.getElementById('temporadaInput').value);
}
let fuente = 'simpsonslatino.online';
function obtenerNumeroRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function copyToClipboard(text) {
var textarea = document.createElement('textarea');
textarea.textContent = text;
document.body.appendChild(textarea);
var selection = document.getSelection();
var savedRanges = [];
for (let i = 0; i < selection.rangeCount; i++) {
savedRanges[i] = selection.getRangeAt(i).cloneRange();
}
selection.removeAllRanges();
textarea.select();
document.execCommand('copy');
selection.removeAllRanges();
for (let i = 0; i < savedRanges.length; i++) {
selection.addRange(savedRanges[i]);
}
document.body.removeChild(textarea);
showToast('Enlace copiado al portapapeles');
}
function showToast(text) {
const toast = document.getElementById('fuenteToast');
const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toast);
toast.querySelector('.toast-body').textContent = text;
toastBootstrap.show();
}
const getSimpsonsOnlineUrl = (season, episode) =>
`https://simpsonslatino.online/episodes/los-simpson-${season}x${episode}/`;
const getSimpsonizadosUrl = (season, episode) =>
`https://simpsonizados.me/capitulo/los-simpson-${season}x${episode}/`;
const botonEncontrarHandler = () => {
const spinner = document.getElementById('spinner');
const card = document.getElementById('card');
if (spinner.classList.contains('visually-hidden')) {
spinner.classList.remove('visually-hidden');
}
if (!card.classList.contains('visually-hidden-focusable')) {
card.classList.add('visually-hidden-focusable');
}
const seasonMax = document.getElementById('temporadaInput').value;
const season = obtenerNumeroRandom(1, seasonMax);
const episode = obtenerNumeroRandom(1, seasons[season]);
const proxy = '/.netlify/edge-functions/proxy?url=';
let link;
if (fuente === 'simpsonslatino.online') {
link = getSimpsonsOnlineUrl(season, episode);
} else if (fuente === 'simpsonizados.me') {
link = getSimpsonizadosUrl(season, episode);
}
const encodedLink = encodeURIComponent(link);
return new Promise((resolve) => {
fetch(proxy + encodedLink)
.then((response) => response.text())
.then((html) => {
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const metaTags = Array.from(doc.getElementsByTagName('meta'));
const openGraphData = metaTags.reduce((data, tag) => {
if (tag.getAttribute('property') && tag.getAttribute('property').startsWith('og:')) {
data[tag.getAttribute('property')] = tag.getAttribute('content');
}
return data;
}, {});
if (!openGraphData['og:image'] || openGraphData['og:title'].includes('no se encuentra')) {
resolve(false);
}
if (card) {
card.classList.remove('visually-hidden-focusable');
}
spinner.classList.add('visually-hidden');
document.querySelector('.card-img-top').src = openGraphData['og:image'];
document.querySelector('.card-title').textContent = openGraphData['og:title'];
document.querySelector('.card-text').textContent = openGraphData['og:description'];
document.getElementById('episodio-link').href = link;
document.getElementById('copiar').addEventListener('click', () => {
copyToClipboard(link);
});
resolve(true);
})
.catch((error) => {
console.error('Error: ' + error);
resolve(false);
});
});
};
document.getElementById('temporadaInput').addEventListener('change', () => {
saveToLocalStorage();
});
document.addEventListener('DOMContentLoaded', () => {
const fuenteLocal = localStorage.getItem('fuente');
const temporadaLocal = localStorage.getItem('temporada');
if (temporadaLocal) {
document.getElementById('temporadaInput').value = temporadaLocal;
}
if (fuenteLocal) {
fuente = fuenteLocal;
}
});
const botonEncontrar = document.getElementById('encontrar');
botonEncontrar.addEventListener('click', () => {
botonEncontrarHandler().then((isOk) => {
if (!isOk) {
document.getElementById('spinner').classList.add('visually-hidden');
showToast('No se encontró el episodio, intenta de nuevo');
}
});
});
document.querySelectorAll('.source').forEach((item) => {
item.addEventListener('click', (event) => {
fuente = event.target.textContent;
saveToLocalStorage();
showToast(`Fuente seleccionada: ${fuente}`);
});
});