forked from davidrthorn/cross_reference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlof.html
54 lines (44 loc) · 1.46 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
52
53
54
<?!= include('lof-loader'); ?>
<script src='https://cdnjs.cloudflare.com/ajax/libs/pdf.js/1.10.81/pdf.js'>
</script>
<script>
PDFJS.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/1.10.81/pdf.worker.js'
page_labs = [];
window.onload = google.script.run.withSuccessHandler(countFigures).getDocAsPDF();
function logIt(blob) {
var pdf = PDFJS.getDocument({data: blob});
return pdf.then(function(pdf) {
var max_pages = pdf.pdfInfo.numPages,
promise_count = [];
for (var i = 1; i <= max_pages; i++) {
var page = pdf.getPage(i);
promise_count.push(page.then(function(page) {
var text_content = page.getTextContent();
return text_content.then(function(page){
return text_content
});
}));
}
// Wait for all pages and sum counts
return Promise.all(promise_count).then(function (page_counts) {
var final_page_count = [];
for (var j = 0; j < page_counts.length; j++) {
var page_as_string = JSON.stringify(page_counts[j]);
final_page_count.push(page_as_string.split('☙').length - 1)
}
return final_page_count;
});
});
}
function countFigures(blob) {
logIt(blob).then(function (count) {
google.script.run.withSuccessHandler(restoreLabels).insertLoFNumbers(count);
});
}
function restoreLabels() {
google.script.run.withSuccessHandler(closeDialog).restoreLabels();
}
function closeDialog() {
google.script.host.close();
}
</script>