-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoceru.js
More file actions
519 lines (447 loc) · 19.6 KB
/
doceru.js
File metadata and controls
519 lines (447 loc) · 19.6 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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// ==UserScript==
// @name Doceru.com Downloader (by Etoshy)
// @namespace http://tampermonkey.net/
// @version 1.1
// @description Intercepta a requisição do link de download e adiciona um botão para baixar o arquivo do doceru.com.
// @author Etoshy
// @match https://doceru.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 Doceru Downloader v1.1]';
console.log(`${SCRIPT_PREFIX} Script iniciado para doceru.com`);
const BUTTON_ID = 'etoshy-doceru-btn';
const BUTTON_CONTAINER_SELECTOR = '.dwn-contain';
// --- ESTILOS (com 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;
font-size: 16px;
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} .icon {
margin-right: 8px;
}
#${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 UTILITÁRIAS ---
/**
* 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 específicos para doceru.com
const titleSelectors = [
'h1.document-title',
'h1',
'.doc-title',
'.document-name',
'.file-name',
'[data-document-title]',
'.title',
'.doc-info h1',
'.document-header h1'
];
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;
}
/**
* 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';
}
}
/**
* 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 = `<i class="icon icon_download"></i> <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 = `<i class="icon icon_download"></i> <span class="main-text">Baixar Arquivo</span><span class="sub-text">Cortesia de Etoshy</span>`;
}
}
}
// --- FUNÇÕES DE CRIAÇÃO DO BOTÃO ---
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}`);
window.lastFoundDownloadUrl = fileUrl; // Salva para uso posterior
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 = `<i class="icon icon_download"></i> <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);
});
// Insere o botão no topo do container (antes do botão original)
container.insertBefore(downloadButton, container.firstChild);
console.log(`${SCRIPT_PREFIX} Botão de download inserido com sucesso!`);
}
// --- LÓGICA DE INTERCEPTAÇÃO ---
// Intercepta requisições XMLHttpRequest
const originalXhrOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(method, url, ...args) {
// Padrões de URL que podem conter o link de download no doceru.com
const downloadPatterns = [
'/download',
'/start/show',
'/get_file',
'/file_download',
'/pdf_download'
];
if (typeof url === 'string' && downloadPatterns.some(pattern => url.includes(pattern))) {
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);
// Tenta diferentes estruturas de resposta
const fileUrl = data?.response?.url || data?.url || data?.download_url || data?.file_url;
processFoundUrl(fileUrl);
} catch (e) {
console.error(`${SCRIPT_PREFIX} (XHR) Erro ao analisar JSON:`, e);
// Se não for JSON, talvez seja uma URL direta
if (this.responseText && this.responseText.startsWith('http')) {
processFoundUrl(this.responseText);
}
}
}
});
}
return originalXhrOpen.apply(this, [method, url, ...args]);
};
// Intercepta requisições Fetch
const originalFetch = window.fetch;
window.fetch = async function(...args) {
const requestInfo = args[0];
const url = (typeof requestInfo === 'string') ? requestInfo : requestInfo.url;
const downloadPatterns = [
'/download',
'/start/show',
'/get_file',
'/file_download',
'/pdf_download'
];
if (typeof url === 'string' && downloadPatterns.some(pattern => url.includes(pattern))) {
console.log(`${SCRIPT_PREFIX} (Fetch) Interceptada requisição para: ${url}`);
try {
const response = await originalFetch(...args);
const clonedResponse = response.clone();
try {
const data = await clonedResponse.json();
const fileUrl = data?.response?.url || data?.url || data?.download_url || data?.file_url;
processFoundUrl(fileUrl);
} catch (e) {
// Se não for JSON, tenta como texto
const textResponse = await clonedResponse.text();
if (textResponse && textResponse.startsWith('http')) {
processFoundUrl(textResponse);
}
}
return response;
} catch (error) {
console.error(`${SCRIPT_PREFIX} (Fetch) Erro ao processar:`, error);
return originalFetch(...args);
}
}
return originalFetch(...args);
};
// Intercepta cliques no botão original para capturar a URL
document.addEventListener('click', function(event) {
const target = event.target;
// Verifica se o clique foi no botão de download original
if (target.id === 'dwn_btn' || target.closest('#dwn_btn')) {
console.log(`${SCRIPT_PREFIX} Clique interceptado no botão original de download`);
// Tenta extrair informações do botão
const button = target.closest('#dwn_btn') || target;
const dataId = button.getAttribute('data-id');
if (dataId) {
console.log(`${SCRIPT_PREFIX} Data-ID encontrado: ${dataId}`);
// Aqui você pode construir a URL de download se souber o padrão
// Por exemplo: https://doceru.com/download/${dataId}
}
}
});
// Também monitora mudanças no DOM para garantir que o botão seja adicionado
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === Node.ELEMENT_NODE) {
const container = node.querySelector ? node.querySelector(BUTTON_CONTAINER_SELECTOR) : null;
if (container && !document.getElementById(BUTTON_ID)) {
console.log(`${SCRIPT_PREFIX} Container encontrado via MutationObserver`);
// Aguarda um pouco para garantir que temos a URL de download
setTimeout(() => {
if (window.lastFoundDownloadUrl) {
createDownloadButton(container, window.lastFoundDownloadUrl);
}
}, 1000);
}
}
});
});
});
observer.observe(document.body, { childList: true, subtree: true });
})();