-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
74 lines (65 loc) · 2.52 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
function decodeFromUrl() {
const url = document.getElementById('urlInput').value;
if (!url) {
displayError('Por favor, insira uma URL');
return;
}
fetch(url)
.then(response => {
if (!response.ok) {
throw new Error('Erro ao obter o texto da URL');
}
return response.arrayBuffer();
})
.then(arrayBuffer => {
const decoder = new TextDecoder('utf-8');
const texto_utf8 = decoder.decode(arrayBuffer);
displayText(texto_utf8);
})
.catch(error => {
displayError(error.message);
});
}
function displayText(texto_utf8) {
clearError();
// Abrir uma nova aba do navegador para exibir o resultado
const newWindow = window.open();
newWindow.document.write('<pre style="padding: 10px;">' + texto_utf8 + '</pre>');
newWindow.document.head.innerHTML = '<title>Arrumado!</title>';
newWindow.document.body.style.backgroundColor = '#282828';
newWindow.document.body.style.color = 'white';
// Função para criar botão de download
function createDownloadButton(extension) {
const button = newWindow.document.createElement('button');
button.textContent = `Baixar arquivo com a extensão .${extension}`;
button.onclick = function () {
const blob = new Blob([texto_utf8], { type: 'text/plain;charset=utf-8' });
const fileName = `resultado.${extension}`;
const downloadLink = newWindow.document.createElement('a');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = fileName;
newWindow.document.body.appendChild(downloadLink);
downloadLink.click();
URL.revokeObjectURL(downloadLink.href);
newWindow.document.body.removeChild(downloadLink);
};
return button;
}
// Criar e adicionar botões de download para .txt e .srt
const downloadButtonTxt = createDownloadButton('txt');
const downloadButtonSrt = createDownloadButton('srt');
// Ajustar margens e adicionar botões à nova aba
newWindow.document.body.insertBefore(downloadButtonSrt, newWindow.document.body.firstChild);
downloadButtonSrt.style.marginLeft = '10px';
newWindow.document.body.insertBefore(downloadButtonTxt, newWindow.document.body.firstChild);
}
function displayError(errorMessage) {
const errorMessages = document.getElementById('errorMessages');
errorMessages.textContent = errorMessage;
}
function clearError() {
const errorMessages = document.getElementById('errorMessages');
errorMessages.textContent = '';
}
const ano = document.getElementById('ano')
ano.textContent = new Date().getFullYear()