-
Notifications
You must be signed in to change notification settings - Fork 0
/
document.js
executable file
·111 lines (58 loc) · 2.29 KB
/
document.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
document.getElementById('dropZone').addEventListener('click', function() {
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = 'application/pdf';
fileInput.onchange = (e) => {
const file = e.target.files[0];
if (file) {
loadPdf(file);
}
};
fileInput.click();
});
document.getElementById('dropZone').addEventListener('dragover', function(event) {
event.preventDefault();
event.stopPropagation();
event.dataTransfer.dropEffect = 'copy';
});
document.getElementById('dropZone').addEventListener('drop', function(event) {
event.preventDefault();
event.stopPropagation();
const files = event.dataTransfer.files;
if (files && files[0]) {
loadPdf(files[0]);
}
});
function loadPdf(file) {
const fileReader = new FileReader();
fileReader.onload = function() {
const typedarray = new Uint8Array(this.result);
pdfjsLib.getDocument(typedarray).promise.then(pdf => {
console.log('PDF loaded');
console.log(`Number of pages: ${pdf.numPages}`);
let courseIds = [];
const processPage = (pageNum) => {
return pdf.getPage(pageNum).then(function(page) {
console.log('Page loaded', pageNum);
return page.getTextContent().then(function(textContent) {
const regex = /\b[A-Z]{4}\d{3,4}\b/g;
const pageText = textContent.items.map(item => item.str).join(' ');
const foundIds = pageText.match(regex);
if (foundIds) {
courseIds = courseIds.concat(foundIds);
}
});
});
};
const promises = [];
for (let i = 1; i <= pdf.numPages; i++) {
promises.push(processPage(i));
}
Promise.all(promises).then(() => {
const uniqueCourseIds = [...new Set(courseIds)];
document.getElementById('textOutput').value = uniqueCourseIds.join('\n');
});
});
};
fileReader.readAsArrayBuffer(file);
}