-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf_deep_link.htm
224 lines (207 loc) · 10.5 KB
/
pdf_deep_link.htm
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PDF.js with text deep linking</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.7.570/pdf.min.js"></script>
<style>
#pdfContainer {
width: 100%;
height: 100vh;
overflow: auto;
position: relative;
cursor: grab;
}
.pdfPage {
width: 100%;
position: relative;
}
#loadingIndicator {
position: absolute;
top: 10px;
left: 10px;
background: rgba(255, 255, 255, 0.8);
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
z-index: 1000;
}
</style>
</head>
<body>
<div id="loadingIndicator">Loading PDF...</div>
<div id="pdfContainer"></div>
<script>
const params = new URLSearchParams(window.location.search);
const url = params.get('site');
const proxyUrl = `https://api.allorigins.win/get?url=${encodeURIComponent(url)}`;
const searchParam = params.get('search');
const hash = window.location.hash;
const searchString = searchParam ? decodeURIComponent(searchParam) :
hash ? decodeURIComponent(hash.replace('#:~:text=', '')) : '';
const searchRegex = new RegExp(searchString.replace(/\s+/g, '\\s*'), 'i');
const loadingIndicator = document.getElementById('loadingIndicator');
const pdfContainer = document.getElementById('pdfContainer');
const pages = []; // Initialise pages array
console.log(`Fetching PDF from: ${proxyUrl}`);
console.log(`Search string: ${searchString}`);
fetch(proxyUrl)
.then(response => {
console.log('Received response from proxy');
loadingIndicator.textContent = 'Received response from proxy';
return response.json();
})
.then(data => {
console.log('Parsing response data');
// Strips the data URL prefix:
const base64Data = data.contents.replace(/^data:application\/pdf;base64,/, '');
console.log('Base64 data length:', base64Data.length);
const binaryString = atob(base64Data);
const len = binaryString.length;
console.log('Binary string length:', len);
const pdfData = new Uint8Array(len);
for (let i = 0; i < len; i++) {
pdfData[i] = binaryString.charCodeAt(i);
}
console.log('PDF data created, length:', pdfData.length);
return pdfjsLib.getDocument({data: pdfData}).promise;
})
.then(pdf => {
console.log('PDF loaded');
const numPages = pdf.numPages;
console.log('Number of pages:', numPages);
let totalCharacters = 0;
let totalWords = 0;
let targetPageNum = null;
let targetViewport = null;
let targetTx = null;
const findTextInPage = (page, pageNum) => {
return page.getTextContent().then(textContent => {
// Normalises text by removing excessive spaces:
const text = textContent.items.map(item => item.str).join(' ').replace(/\s+/g, ' ').trim();
totalCharacters += text.length;
totalWords += text.split(/\s+/).length;
if (searchRegex.test(text)) {
console.log(`Found "${searchString}" on page ${pageNum}`);
targetPageNum = pageNum;
const viewport = page.getViewport({scale: 1.5});
targetViewport = viewport;
textContent.items.forEach(item => {
const normalisedItemStr = item.str.replace(/\s+/g, ' ').trim();
if (searchRegex.test(normalisedItemStr)) {
const tx = pdfjsLib.Util.transform(viewport.transform, item.transform);
targetTx = tx;
}
});
}
});
};
const renderPage = pageNum => {
return pdf.getPage(pageNum).then(page => {
console.log(`Rendering page ${pageNum}`);
const viewport = page.getViewport({scale: 1.5});
const pageDiv = document.getElementById(`page-${pageNum}`);
const canvas = document.createElement('canvas');
canvas.className = 'pdfViewer';
const context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
pageDiv.appendChild(canvas);
return page.render({canvasContext: context, viewport: viewport}).promise.then(() => {
if (pageNum === targetPageNum) {
page.getTextContent().then(textContent => {
textContent.items.forEach(item => {
const normalisedItemStr = item.str.replace(/\s+/g, ' ').trim();
if (searchRegex.test(normalisedItemStr)) {
const tx = pdfjsLib.Util.transform(viewport.transform, item.transform);
context.save();
context.globalAlpha = 0.5;
context.fillStyle = 'yellow';
context.fillRect(tx[4], tx[5] - item.height, item.width, item.height);
context.restore();
}
});
// Scroll to the highlighted text
pdfContainer.scrollTop = pageDiv.offsetTop + targetTx[5] - pdfContainer.clientHeight / 2;
pdfContainer.scrollLeft = targetTx[4] - pdfContainer.clientWidth / 2 + targetViewport.width / 2;
});
}
});
});
};
const handleScroll = () => {
const containerTop = pdfContainer.scrollTop;
const containerBottom = containerTop + pdfContainer.clientHeight;
pages.forEach(pageDiv => {
const pageTop = pageDiv.offsetTop;
const pageBottom = pageTop + pageDiv.clientHeight;
if ((pageTop >= containerTop && pageTop < containerBottom) || (pageBottom > containerTop && pageBottom <= containerBottom)) {
const pageNum = parseInt(pageDiv.dataset.pageNumber);
if (!pageDiv.hasChildNodes()) {
renderPage(pageNum);
}
}
});
};
const searchPages = async () => {
for (let pageNum = 1; pageNum <= numPages; pageNum++) {
const page = await pdf.getPage(pageNum);
await findTextInPage(page, pageNum);
const pageDiv = document.createElement('div');
pageDiv.className = 'pdfPage';
pageDiv.id = `page-${pageNum}`;
pageDiv.dataset.pageNumber = pageNum;
pageDiv.style.height = `${page.getViewport({scale: 1.5}).height}px`; // Use actual page height
pdfContainer.appendChild(pageDiv);
pages.push(pageDiv); // Add to pages array
}
loadingIndicator.style.display = 'none';
handleScroll(); // Initial call to load visible pages
// Log total characters and word counts after all pages are processed
console.log(`Total characters: ${totalCharacters}`);
console.log(`Estimated word count: ${totalWords}`);
console.log(`Average words per page: ${(totalWords / numPages).toFixed(2)}`);
if (targetPageNum !== null) {
// Ensure target page is rendered
await renderPage(targetPageNum);
}
};
pdfContainer.addEventListener('scroll', handleScroll);
searchPages();
// Enable usual PDF interactions after highlighting
let isDragging = false;
let startX, startY, scrollLeft, scrollTop;
pdfContainer.addEventListener('mousedown', e => {
isDragging = true;
startX = e.pageX - pdfContainer.offsetLeft;
startY = e.pageY - pdfContainer.offsetTop;
scrollLeft = pdfContainer.scrollLeft;
scrollTop = pdfContainer.scrollTop;
pdfContainer.style.cursor = 'grabbing';
});
pdfContainer.addEventListener('mouseleave', () => {
isDragging = false;
pdfContainer.style.cursor = 'grab';
});
pdfContainer.addEventListener('mouseup', () => {
isDragging = false;
pdfContainer.style.cursor = 'grab';
});
pdfContainer.addEventListener('mousemove', e => {
if (!isDragging) return;
e.preventDefault();
const x = e.pageX - pdfContainer.offsetLeft;
const y = e.pageY - pdfContainer.offsetTop;
const walkX = (x - startX);
const walkY = (y - startY);
pdfContainer.scrollLeft = scrollLeft - walkX;
pdfContainer.scrollTop = scrollTop - walkY;
});
})
.catch(error => {
console.error('Error:', error);
loadingIndicator.textContent = 'Error loading PDF.';
});
</script>
</body>
</html>