-
Notifications
You must be signed in to change notification settings - Fork 12
/
content.js
305 lines (262 loc) · 13.4 KB
/
content.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
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
function enviarProgresso(progress){
chrome.runtime.sendMessage({ type: 'progressUpdate', progress: progress });
}
function clicarEcapturarConteudoBruto(indice, pagefinal) {
const time = 2000
return new Promise((resolve, reject) => {
var elementoIndex = document.querySelector('[data-test-locator="Epub-ChapterRow-Index-'+indice+'"] [tabindex="0"]');
if (elementoIndex) {
elementoIndex.click();
setTimeout(() => {
function verificarElemento() {
var elementoCapturado = document.evaluate("//div[@class='chapter-loaded highlighter-context' and @id='p"+indice+"--0']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (elementoCapturado) {
resolve(elementoCapturado.outerHTML+'<br>\n\n');
} else {
setTimeout(verificarElemento, 1000);
}
}
verificarElemento();
}, time);
} else {
try {
const elemento = document.evaluate("//div[@data-test-locator='Pdf-SubChapterRow-Page-"+indice+"']/div", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
const elementosub = document.evaluate("//div[@data-test-locator='Pdf-SubSubChapterRow-Page-"+indice+"']/div", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (elemento || elementosub) {
if (elemento) {
elemento.click();
} else {
elementosub.click();
}
setTimeout(() => {
function verificarElemento() {
var elementoCapturado = document.evaluate("//div[@class='chapter-loaded highlighter-context' and @id='p"+indice+"--0']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (elementoCapturado) {
resolve(elementoCapturado.outerHTML+'<br>\n\n');
} else {
setTimeout(verificarElemento, 1000);
}
}
verificarElemento();
}, time);
} else {
var elementoCapturado = document.evaluate("//div[@class='chapter-loaded highlighter-context' and @id='p"+indice+"--0']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (elementoCapturado) {
resolve(elementoCapturado.outerHTML+'<br>\n\n');
}
try {
const num = indice+1
const elemento = document.evaluate("//div[@data-test-locator='Epub-ChapterRow-Page-"+num+"']/div", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (elemento) {
let stopButton = document.createElement('button');
stopButton.textContent = 'PDF file, use manual mode to download';
stopButton.style.position = 'fixed';
stopButton.style.top = '54%';
stopButton.style.right = '46%';
stopButton.style.zIndex = '9999';
stopButton.style.backgroundColor = 'white';
stopButton.style.color = 'green';
stopButton.style.fontWeight = 'bold';
document.body.appendChild(stopButton);
} else{
reject('Elemento não encontrado ' + indice);
}
} catch(error){
reject(error);
}
}
} catch(error){
reject('Elemento não encontrado ' + indice);
}
}
});
}
async function abrirbotao(){
const botaoTOC = document.querySelector('button[data-test-locator="Icon-TOC"]');
if (!botaoTOC) {
console.log("Botão do Table of Contents não encontrado");
} else {
let conteudoTOC = document.querySelector('[data-test-locator="ToolbarPanel"]');
if (!conteudoTOC) {
console.log("Conteúdo do Table of Contents não encontrado. Abrindo agora...");
botaoTOC.click();
setTimeout(() => {
conteudoTOC = document.querySelector('[data-test-locator="ToolbarPanel"]');
if (conteudoTOC) {
criarArquivoDownloadComConteudo();
console.log("Conteúdo do Table of Contents foi aberto com sucesso.");
} else {
console.log("Falha ao abrir o conteúdo do Table of Contents.");
}
}, 2000);
} else {
criarArquivoDownloadComConteudo();
}
}
}
async function criarArquivoDownloadComConteudo() {
let todoConteudoBruto = '';
const elementos = document.querySelectorAll('[data-test-locator^="Epub-ChapterRow-"]');
const db = await openIndexedDB();
if (elementos.length > 0) {
let lastProcessedIndex = await getLastProcessedIndex(db) || 0;
console.log(lastProcessedIndex)
let todoConteudoBruto = await getTodoConteudo(db) || '';
//const ultimoElemento = elementos[elementos.length - 1];
//const ultimoIndicecont = ultimoElemento.getAttribute('data-test-locator').match(/\d+$/)[0];
//const result = document.evaluate("/html/body/div[1]/main/div[1]/div[2]/div[3]/div/text()[3]",document,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue;
//const result2 = document.evaluate("/html/body/div[1]/main/div[1]/div[2]/div[4]/div/text()[3]",document,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue;
//let text = '';
//let node = result || result2;
//text = node.textContent.trim();
var divs = document.querySelectorAll('div[data-test-locator]');
var maiorNumero = -1;
divs.forEach(function(elemento) {
var valor = elemento.getAttribute('data-test-locator');
var match = valor.match(/\d+/);
if (match !== null) {
var numero = parseInt(match[0]);
if (numero > maiorNumero) {
maiorNumero = numero;
}
}
});
const pagefinal = maiorNumero;
ultimoIndice = pagefinal
console.log("Última página é: " + ultimoIndice);
for (let i = lastProcessedIndex; i <= ultimoIndice; i++) {
try {
const conteudoBruto = await clicarEcapturarConteudoBruto(i, ultimoIndice);
todoConteudoBruto += conteudoBruto;
const progressoAtual = Math.floor((i / ultimoIndice) * 100);
enviarProgresso(progressoAtual);
lastProcessedIndex = i;
await putLastProcessedIndex(db, lastProcessedIndex+1);
await putTodoConteudo(db, todoConteudoBruto);
} catch (error) {
console.log(error);
}
}
let img_tags = todoConteudoBruto.split('</picture>');
let modified_html = '';
for (let index = 0; index < img_tags.length; index++) {
let img = img_tags[index];
if (img.includes('source data-srcset=')) {
let srcset_start = img.indexOf('source data-srcset=') + 'source data-srcset="'.length;
let srcset_end = img.indexOf('"', srcset_start);
let srcset_value = img.substring(srcset_start, srcset_end);
img = img.replace(srcset_value, '') + srcset_value + '"</picture>';
}
modified_html += img;
if (index !== img_tags.length - 1) {
modified_html += '</picture>';
}
}
let modificado = modified_html.replace(/">https:\/\/api/g, '" src="https://api').replace(/opacity: 0/g, 'opacity: 1').replace(/<.picture><.picture>/g, '</picture>').replace(/data-src=/g,'src=');
var blob = new Blob([`<!DOCTYPE html><head><script>function scrollWithinBook(identifier, pageNumber) {var element;if (identifier === '') {element = document.getElementById('p' + pageNumber + '--0');} else {element = document.querySelector('[data-originalid="' + identifier + '"]');}if (element) {var yOffset = element.getBoundingClientRect().top + window.pageYOffset;window.scrollTo({ top: yOffset, behavior: 'smooth' });} else {}}function LoadChapter(identifier) {}function showImage(identifier) {}function scrollToDataOriginalId() {var hash = window.location.hash;if (hash) {var dataOriginalId = hash.substr(1);var element = document.querySelector('[data-originalid="' + dataOriginalId + '"]');if (element) {var yOffset = element.getBoundingClientRect().top + window.pageYOffset;window.scrollTo({ top: yOffset, behavior: 'smooth' });} else {}}}window.onload = function() {scrollToDataOriginalId();};</script><meta charset="UTF-8"></head><div id="content" class="content highlighter-context" col-centered="true" style="max-width: 792.952px;">`+modificado.replace(/Georgia; object-fit: contain; width: 100%; height: 100%;/g,'Georgia; object-fit: contain; width: 100%;')], { type: 'text/html' });
var link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'perlego.html';
link.click();
resetarEIniciarCaptura()
}
}
async function getTodoConteudo(db) {
return new Promise((resolve, reject) => {
const transaction = db.transaction(['conteudo'], 'readonly');
const store = transaction.objectStore('conteudo');
const request = store.get('todoConteudo');
request.onsuccess = function(event) {
resolve(event.target.result);
};
request.onerror = function(event) {
reject(event.error);
};
});
}
async function putTodoConteudo(db, content) {
return new Promise((resolve, reject) => {
const transaction = db.transaction(['conteudo'], 'readwrite');
const store = transaction.objectStore('conteudo');
const request = store.put(content, 'todoConteudo');
request.onsuccess = function(event) {
resolve();
};
request.onerror = function(event) {
reject(event.error);
};
});
}
async function openIndexedDB() {
return new Promise((resolve, reject) => {
const dbOpenRequest = window.indexedDB.open('MeuBancoDeDados', 1);
dbOpenRequest.onupgradeneeded = function(event) {
const db = event.target.result;
db.createObjectStore('conteudo', { autoIncrement: true });
};
dbOpenRequest.onsuccess = function(event) {
resolve(event.target.result);
};
dbOpenRequest.onerror = function(event) {
reject(event.error);
};
});
}
async function getLastProcessedIndex(db) {
return new Promise((resolve, reject) => {
const transaction = db.transaction(['conteudo'], 'readonly');
const store = transaction.objectStore('conteudo');
const lastIndexRequest = store.get('lastProcessedIndex');
lastIndexRequest.onsuccess = function(event) {
resolve(event.target.result || 0);
};
lastIndexRequest.onerror = function(event) {
reject(event.error);
};
});
}
async function putLastProcessedIndex(db, index) {
return new Promise((resolve, reject) => {
const transaction = db.transaction(['conteudo'], 'readwrite');
const store = transaction.objectStore('conteudo');
const putRequest = store.put(index, 'lastProcessedIndex');
putRequest.onsuccess = function(event) {
resolve();
};
putRequest.onerror = function(event) {
reject(event.error);
};
});
}
async function resetLastProcessedIndex(db) {
return new Promise((resolve, reject) => {
const transaction = db.transaction(['conteudo'], 'readwrite');
const store = transaction.objectStore('conteudo');
const deleteRequest = store.delete('lastProcessedIndex');
deleteRequest.onsuccess = function (event) {
resolve();
};
deleteRequest.onerror = function (event) {
reject(event.error);
};
});
}
async function resetAllContent(db) {
return new Promise((resolve, reject) => {
const transaction = db.transaction(['conteudo'], 'readwrite');
const store = transaction.objectStore('conteudo');
const request = store.clear();
request.onsuccess = function(event) {
resolve();
};
request.onerror = function(event) {
reject(event.error);
};
});
}
async function resetarEIniciarCaptura() {
const db = await openIndexedDB();
await resetAllContent(db);
await resetLastProcessedIndex(db);
}
abrirbotao()