-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocubra.js
More file actions
430 lines (370 loc) · 16 KB
/
docubra.js
File metadata and controls
430 lines (370 loc) · 16 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// ==UserScript==
// @name Docer.ar & Docubra.com Downloader (by Etoshy)
// @namespace http://tampermonkey.net/
// @version 1.6
// @description Intercepta a requisição do link de download e adiciona um botão para baixar o arquivo na mesma aba.
// @author Etoshy
// @match https://docer.ar/doc/*
// @match https://docubra.com/doc/*
// @icon https://cdn-icons-png.flaticon.com/512/126/126472.png
// @grant GM_addStyle
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
const SCRIPT_PREFIX = '[Etoshy Downloader v1.6]';
console.log(`${SCRIPT_PREFIX} Script iniciado. Armadilha dupla (Fetch & XHR) preparada.`);
const BUTTON_ID = 'etoshy-downloader-btn';
const BUTTON_CONTAINER_SELECTOR = '#action-panel-details .dwn-contain';
// --- ESTILOS (com a fonte ajustada e barra de progresso) ---
GM_addStyle(`
#${BUTTON_ID} {
background-color: #04BB9C !important;
color: white !important;
border: none;
border-radius: 4px;
padding: 12px;
width: 100%;
text-align: center;
font-family: 'YouTube Noto', 'Roboto', 'Arial', sans-serif;
cursor: pointer;
display: block;
text-decoration: none;
margin-bottom: 10px;
transition: background-color 0.3s;
position: relative;
overflow: hidden;
}
#${BUTTON_ID}:hover { background-color: #03a086 !important; }
#${BUTTON_ID} .main-text {
font-size: 1.2em;
font-weight: bold;
display: block;
position: relative;
z-index: 2;
}
#${BUTTON_ID} .sub-text {
font-size: 0.8em;
font-weight: normal;
display: block;
margin-top: 4px;
opacity: 0.9;
position: relative;
z-index: 2;
}
#${BUTTON_ID}:disabled {
background-color: #cccccc !important;
cursor: not-allowed;
}
#${BUTTON_ID} .progress-bar {
position: absolute;
top: 0;
left: 0;
height: 100%;
background-color: rgba(255, 255, 255, 0.2);
transition: width 0.3s ease;
border-radius: 4px;
z-index: 1;
}
#${BUTTON_ID} .progress-text {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 1.1em;
font-weight: bold;
z-index: 3;
text-shadow: 1px 1px 2px rgba(0,0,0,0.5);
color: white;
}
`);
// --- FUNÇÕES DO BOTÃO ---
/**
* Extrai o nome do arquivo da URL ou do título da página
* @param {string} url - A URL do arquivo
* @returns {string} - Nome do arquivo extraído
*/
function extractFileName(url) {
try {
// Primeiro, tenta extrair da URL
const urlObj = new URL(url);
const pathname = urlObj.pathname;
let fileName = pathname.substring(pathname.lastIndexOf('/') + 1);
// Se encontrou um nome válido na URL, usa ele
if (fileName && fileName.indexOf('.') !== -1) {
return decodeURIComponent(fileName);
}
// Se não encontrou na URL, tenta extrair do título da página
const pageTitle = document.title;
if (pageTitle && pageTitle.trim() !== '') {
// Remove caracteres inválidos para nomes de arquivo
const cleanTitle = pageTitle
.replace(/[<>:"/\\|?*]/g, '') // Remove caracteres inválidos
.replace(/\s+/g, ' ') // Normaliza espaços
.trim();
if (cleanTitle.length > 0) {
return cleanTitle + '.pdf'; // Adiciona extensão padrão
}
}
// Último recurso: tenta extrair do meta título ou h1
const h1Element = document.querySelector('h1');
if (h1Element && h1Element.textContent.trim()) {
const cleanH1 = h1Element.textContent
.replace(/[<>:"/\\|?*]/g, '')
.replace(/\s+/g, ' ')
.trim();
if (cleanH1.length > 0) {
return cleanH1 + '.pdf';
}
}
return 'documento.pdf'; // nome padrão
} catch (e) {
console.warn(`${SCRIPT_PREFIX} Erro ao extrair nome do arquivo:`, e);
return 'documento.pdf';
}
}
/**
* Tenta extrair o nome do documento dos elementos da página
* @returns {string} - Nome do documento ou null se não encontrar
*/
function extractDocumentNameFromPage() {
// Seletores comuns para título de documento em sites como docer.ar e docubra.com
const titleSelectors = [
'h1.document-title',
'h1',
'.document-name',
'.doc-title',
'.file-name',
'[data-document-title]',
'.title'
];
for (const selector of titleSelectors) {
const element = document.querySelector(selector);
if (element && element.textContent.trim()) {
const title = element.textContent
.replace(/[<>:"/\\|?*]/g, '') // Remove caracteres inválidos
.replace(/\s+/g, ' ') // Normaliza espaços
.trim();
if (title.length > 0) {
console.log(`${SCRIPT_PREFIX} Título encontrado no seletor '${selector}': ${title}`);
return title;
}
}
}
return null;
}
/**
* Adiciona o crédito ao nome do arquivo
* @param {string} originalName - Nome original do arquivo
* @returns {string} - Nome do arquivo com crédito
*/
function addCreditToFileName(originalName) {
const lastDotIndex = originalName.lastIndexOf('.');
if (lastDotIndex === -1) {
// Arquivo sem extensão
return `${originalName}_Github-Etoshy_`;
}
const nameWithoutExtension = originalName.substring(0, lastDotIndex);
const extension = originalName.substring(lastDotIndex);
return `${nameWithoutExtension}_Github-Etoshy_${extension}`;
}
/**
* Atualiza a barra de progresso do botão
* @param {number} percentage - Porcentagem do progresso (0-100)
*/
function updateProgressBar(percentage) {
const button = document.getElementById(BUTTON_ID);
if (!button) return;
let progressBar = button.querySelector('.progress-bar');
let progressText = button.querySelector('.progress-text');
if (!progressBar) {
progressBar = document.createElement('div');
progressBar.className = 'progress-bar';
button.appendChild(progressBar);
}
if (!progressText) {
progressText = document.createElement('div');
progressText.className = 'progress-text';
button.appendChild(progressText);
}
progressBar.style.width = `${percentage}%`;
progressText.textContent = `${Math.round(percentage)}%`;
}
/**
* Função que baixa o arquivo do servidor e depois envia para o usuário com nome modificado
* @param {string} url - A URL do arquivo a ser baixado
*/
async function triggerDownload(url) {
console.log(`${SCRIPT_PREFIX} Iniciando download para: ${url}`);
const button = document.getElementById(BUTTON_ID);
if (button) {
button.disabled = true;
button.innerHTML = ''; // Limpa o conteúdo do botão para mostrar apenas a barra de progresso
}
try {
// 1. Baixar o arquivo do servidor com controle de progresso
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
// Obter o tamanho total do arquivo
const contentLength = response.headers.get('content-length');
const totalSize = contentLength ? parseInt(contentLength, 10) : 0;
// 2. Obter o nome original do arquivo
let originalFileName = 'documento.pdf'; // padrão inicial
// Tentar obter o nome do Content-Disposition header primeiro
const contentDisposition = response.headers.get('content-disposition');
if (contentDisposition) {
const fileNameMatch = contentDisposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);
if (fileNameMatch && fileNameMatch[1]) {
originalFileName = fileNameMatch[1].replace(/['"]/g, '');
console.log(`${SCRIPT_PREFIX} Nome extraído do Content-Disposition: ${originalFileName}`);
}
}
// Se não conseguiu do header, tenta extrair da página
if (originalFileName === 'documento.pdf') {
// Primeiro tenta dos elementos específicos da página
const documentTitle = extractDocumentNameFromPage();
if (documentTitle) {
originalFileName = documentTitle + '.pdf';
console.log(`${SCRIPT_PREFIX} Nome extraído dos elementos da página: ${originalFileName}`);
} else {
// Se não conseguir, usa o método de extração da URL/título
originalFileName = extractFileName(url);
console.log(`${SCRIPT_PREFIX} Nome extraído do título da página: ${originalFileName}`);
}
}
// 3. Ler o stream com controle de progresso
const reader = response.body.getReader();
const chunks = [];
let receivedLength = 0;
updateProgressBar(0);
while (true) {
const { done, value } = await reader.read();
if (done) break;
chunks.push(value);
receivedLength += value.length;
// Atualizar barra de progresso
if (totalSize > 0) {
const percentage = (receivedLength / totalSize) * 100;
updateProgressBar(percentage);
} else {
// Se não souber o tamanho total, simula progresso
const simulatedProgress = Math.min(95, (receivedLength / 1024 / 1024) * 10); // 10% por MB
updateProgressBar(simulatedProgress);
}
}
// 4. Combinar chunks em um blob
const blob = new Blob(chunks);
updateProgressBar(100);
// 5. Criar nome modificado com crédito
const modifiedFileName = addCreditToFileName(originalFileName);
// 6. Criar link de download com nome modificado
const link = document.createElement('a');
const objectURL = URL.createObjectURL(blob);
link.href = objectURL;
link.download = modifiedFileName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// 7. Limpar o objeto URL para liberar memória
URL.revokeObjectURL(objectURL);
console.log(`${SCRIPT_PREFIX} Download concluído! Arquivo: ${modifiedFileName}`);
// Resetar o botão após um pequeno delay para mostrar 100%
setTimeout(() => {
if (button) {
button.disabled = false;
button.innerHTML = `<span class="main-text">Baixar Arquivo</span><span class="sub-text">Cortesia de Etoshy</span>`;
}
}, 1000);
} catch (error) {
console.error(`${SCRIPT_PREFIX} Erro durante o download:`, error);
// Em caso de erro, tenta o método original
console.log(`${SCRIPT_PREFIX} Tentando método de download alternativo...`);
const link = document.createElement('a');
link.href = url;
link.download = addCreditToFileName('documento.pdf');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// Resetar o botão
if (button) {
button.disabled = false;
button.innerHTML = `<span class="main-text">Baixar Arquivo</span><span class="sub-text">Cortesia de Etoshy</span>`;
}
}
}
function processFoundUrl(fileUrl) {
if (!fileUrl) {
console.warn(`${SCRIPT_PREFIX} URL do arquivo não foi encontrada na resposta.`);
return;
}
console.log(`${SCRIPT_PREFIX} URL do arquivo encontrada: ${fileUrl}`);
waitForElementAndCreateButton(fileUrl);
}
function waitForElementAndCreateButton(downloadUrl) {
if (document.getElementById(BUTTON_ID)) {
console.log(`${SCRIPT_PREFIX} Botão já existe. Ignorando.`);
return;
}
console.log(`${SCRIPT_PREFIX} Aguardando contêiner '${BUTTON_CONTAINER_SELECTOR}' aparecer...`);
const intervalId = setInterval(() => {
const container = document.querySelector(BUTTON_CONTAINER_SELECTOR);
if (container) {
clearInterval(intervalId);
console.log(`${SCRIPT_PREFIX} Contêiner encontrado! Inserindo botão...`);
createDownloadButton(container, downloadUrl);
}
}, 500);
}
function createDownloadButton(container, downloadUrl) {
const downloadButton = document.createElement('button');
downloadButton.id = BUTTON_ID;
downloadButton.type = 'button';
downloadButton.innerHTML = `<span class="main-text">Baixar Arquivo</span><span class="sub-text">Cortesia de Etoshy</span>`;
// Adiciona o evento de clique que chama nossa função de download aprimorada
downloadButton.addEventListener('click', () => {
triggerDownload(downloadUrl);
});
container.prepend(downloadButton);
console.log(`${SCRIPT_PREFIX} Botão de download inserido com sucesso!`);
}
// --- LÓGICA DE INTERCEPTAÇÃO (XMLHttpRequest) ---
const originalXhrOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(method, url, ...args) {
if (typeof url === 'string' && url.includes('/start/show')) {
console.log(`${SCRIPT_PREFIX} (XHR) Interceptada chamada para: ${url}`);
this.addEventListener('load', function() {
if (this.readyState === 4 && this.status === 200) {
try {
const data = JSON.parse(this.responseText);
processFoundUrl(data?.response?.url);
} catch (e) {
console.error(`${SCRIPT_PREFIX} (XHR) Erro ao analisar JSON:`, e);
}
}
});
}
return originalXhrOpen.apply(this, [method, url, ...args]);
};
// --- LÓGICA DE INTERCEPTAÇÃO (Fetch) ---
const originalFetch = window.fetch;
window.fetch = async function(...args) {
const requestInfo = args[0];
const url = (typeof requestInfo === 'string') ? requestInfo : requestInfo.url;
if (typeof url === 'string' && url.includes('/start/show')) {
console.log(`${SCRIPT_PREFIX} (Fetch) Interceptada requisição para: ${url}`);
try {
const response = await originalFetch(...args);
const clonedResponse = response.clone();
const data = await clonedResponse.json();
processFoundUrl(data?.response?.url);
return response;
} catch (error) {
console.error(`${SCRIPT_PREFIX} (Fetch) Erro ao processar:`, error);
return originalFetch(...args);
}
}
return originalFetch(...args);
};
})();