-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlof.html
51 lines (42 loc) · 1.44 KB
/
lof.html
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
<?!= include('loader-style'); ?>
<script src='https://cdnjs.cloudflare.com/ajax/libs/pdf.js/1.10.81/pdf.js'>
</script>
<div class="loader">
<span class="l-1"></span>
<span class="l-2"></span>
<span class="l-3"></span>
<span class="l-4"></span>
<span class="l-5"></span>
<span class="l-6"></span>
</div>
<span>Long, complex documents can<br />take minutes to process</span>
<script>
PDFJS.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/1.10.81/pdf.worker.js'
window.onload = google.script.run.withSuccessHandler(countFigures).getDocAsPDF();
function getAllPageTexts(blob) {
const pdf = PDFJS.getDocument({ data: blob })
return pdf.then(pdf => {
const maxPages = pdf.pdfInfo.numPages
const pageTexts = []
for (let i = 1; i <= maxPages; i++) {
pageTexts.push(pdf.getPage(i).then(page => page.getTextContent()))
}
return Promise.all(pageTexts)
})
}
const countLabels = sign => pageText => JSON.stringify(pageText).split(sign).length - 1
function countFigures(blob) {
getAllPageTexts(blob).then(pageTexts => {
google.script.run.withSuccessHandler(restoreLabels).insertLoFNumbers({
'fig': pageTexts.map(countLabels('☙')),
'tab': pageTexts.map(countLabels('❆')),
})
})
}
function restoreLabels() {
google.script.run.withSuccessHandler(closeDialog).restoreLabels()
}
function closeDialog() {
google.script.host.close()
}
</script>