-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgetContentScript.js
48 lines (44 loc) · 1.53 KB
/
getContentScript.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
(async () => {
const url = location.href;
console.log("url", url);
const selectedLanguage = localStorage.getItem("selectedLanguage");
console.log("language", selectedLanguage);
if (!url.endsWith(".pdf")) {
console.error("No PDF found at this URL.");
return;
}
const workerSrc = chrome.runtime.getURL("lib/pdf.worker.mjs");
const pdfjsLib = await import(chrome.runtime.getURL("lib/pdf.min.mjs"));
pdfjsLib.GlobalWorkerOptions.workerSrc = workerSrc;
try {
const loadingTask = pdfjsLib.getDocument(url);
const pdfDoc = await loadingTask.promise;
// Get the current selection
const selection = window.getSelection();
console.log("Selected text: ", selection.toString());
if (selection.toString()) {
// If there's a selection, use that
chrome.runtime.sendMessage({
command: "sendPdfContent",
content: selection.toString(),
language: selectedLanguage,
});
} else {
// Otherwise, extract all text from the PDF
let fullText = "";
for (let pageNum = 1; pageNum <= pdfDoc.numPages; pageNum++) {
const page = await pdfDoc.getPage(pageNum);
const textContent = await page.getTextContent();
const pageText = textContent.items.map((item) => item.str).join(" ");
fullText += pageText + "\n";
}
chrome.runtime.sendMessage({
command: "sendPdfContent",
content: fullText,
language: selectedLanguage,
});
}
} catch (err) {
console.error("Error extracting PDF:", err);
}
})();