-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
95 lines (85 loc) · 3.71 KB
/
script.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
let documents = [];
function updateFileLabel() {
let fileInput = document.getElementById('fileUpload');
let fileCount = fileInput.files.length;
let label = fileCount === 0 ? 'Choose Files' : 'Selected Files: ' + fileCount;
document.querySelector('.custom-file-upload').textContent = label;
}
document.getElementById('fileUpload').addEventListener('change', function() {
updateFileLabel();
let files = this.files;
for (let i = 0; i < files.length; i++) {
let file = files[i];
let reader = new FileReader();
reader.onload = function() {
pdfjsLib.getDocument({data: this.result}).promise.then(function(pdf) {
let numPages = pdf.numPages;
let promises = [];
for (let j = 1; j <= numPages; j++) {
promises.push(pdf.getPage(j).then(function(page) {
return page.getTextContent().then(function(textContent) {
return { text: textContent.items.map(function(item) { return item.str; }).join(' '), page: j };
});
}));
}
Promise.all(promises).then(function(texts) {
documents.push({name: file.name, text: texts});
document.getElementById('fileNames').textContent += file.name + '\n';
});
});
};
reader.readAsArrayBuffer(file);
}
});
document.getElementById('caseSensitive').addEventListener('click', function() {
this.classList.toggle('active');
handleSearch();
});
document.getElementById('submitContextLength').addEventListener('click', function() {
handleSearch();
});
function handleSearch() {
let query = document.getElementById('searchQuery').value;
let caseSensitive = document.getElementById('caseSensitive').classList.contains('active');
let contextLength = parseInt(document.getElementById('contextLength').value);
let resultsDiv = document.getElementById('results');
resultsDiv.innerHTML = '';
for (let i = 0; i < documents.length; i++) {
let doc = documents[i];
for (let j = 0; j < doc.text.length; j++) {
let text = doc.text[j].text;
let pos;
let start = 0;
while (true) {
if (caseSensitive) {
pos = text.indexOf(query, start);
} else {
pos = text.toLowerCase().indexOf(query.toLowerCase(), start);
}
if (pos !== -1) {
let contextStart = Math.max(0, pos - contextLength);
let contextEnd = Math.min(text.length, pos + query.length + contextLength);
let resultText = text.substring(contextStart, contextEnd);
let regex = new RegExp(query, caseSensitive ? "" : "i");
resultText = resultText.replace(regex, '<span class="highlight">$&</span>');
let resultDiv = document.createElement('div');
resultDiv.innerHTML = '<span class="bold">' + doc.name + ' (Page ' + doc.text[j].page + '):</span> ' + resultText;
resultsDiv.appendChild(resultDiv);
start = pos + query.length;
} else {
break;
}
}
}
}
}
updateFileLabel();
document.getElementById('githubButton').addEventListener('click', function() {
window.location.href = 'https://github.com/sankeer28/PDF-Searcher';
});
function setContainerWidth(width) {
var container = document.getElementById('results');
container.style.width = width;
container.style.marginLeft = 'auto';
container.style.marginRight = 'auto';
}