-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpdf.js
66 lines (59 loc) · 1.95 KB
/
pdf.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
import * as pdfjsLib from '../dist/pdfjs/pdf.min.mjs'
// noinspection JSUnresolvedReference
pdfjsLib.GlobalWorkerOptions.workerSrc = '../dist/pdfjs/pdf.worker.min.mjs'
/**
* @function getPDF
* @param {String} url
* @return {Promise<String[]>}
*/
export async function getPDF(url) {
// const response = await fetchPDF(url)
const response = await fetch(url)
const arrayBuffer = await response.arrayBuffer()
const pdf = await pdfjsLib.getDocument({ data: arrayBuffer }).promise
const lines = []
for (let i = 1; i <= pdf.numPages; i++) {
const page = await pdf.getPage(i)
const textContent = await page.getTextContent()
// Extracting text
textContent.items.forEach((item) => {
if (item.str) {
lines.push(item.str)
}
})
// Extracting annotation URLs
const annotations = await page.getAnnotations()
annotations.forEach((annotation) => {
// console.log('annotation:', annotation)
if (annotation.url) {
lines.push(annotation.url)
}
})
}
return lines
}
// /**
// * @function fetchPDF
// * @param {String} pdfUrl
// * @return {Promise<Response>}
// */
// async function fetchPDF(pdfUrl) {
// console.debug(`fetchPDF: ${pdfUrl}`)
// try {
// return await fetch(pdfUrl)
// } catch (e) {
// if (pdfUrl.startsWith('file://')) {
// throw e
// }
// console.log(`%cPDF Fetch Error: ${e.message}`, 'color: OrangeRed')
// const { options } = await chrome.storage.sync.get(['options'])
// if (!options.proxyUrl) {
// throw e
// }
// showToast('Fetch Failed, Trying Proxy...')
// const url = new URL(options.proxyUrl)
// url.searchParams.append('url', pdfUrl)
// console.log(`%cTrying Proxy URL: ${url.href}`, 'color: LimeGreen')
// return await fetch(url.href)
// }
// }