forked from chathub-dev/bing-chat-sidebar-for-chrome
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent-script.js
44 lines (40 loc) · 1.15 KB
/
content-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
async function extractPDFText(url) {
const pdf = await pdfjsLib.getDocument(url).promise;
console.debug("PDF pages", pdf.numPages);
const countPromises = [];
for (let currentPage = 1; currentPage <= pdf.numPages; currentPage++) {
const page = pdf.getPage(currentPage);
countPromises.push(
page.then(async (page) => {
const text = await page.getTextContent();
return text.items.map((s) => s.str).join("");
})
);
}
const texts = await Promise.all(countPromises);
return texts.join("");
}
function isPDFPage() {
if (/\.pdf$/i.test(location.href)) {
return true;
}
if (document.querySelector("pdf-viewer")) {
return true;
}
const embed = document.querySelector("embed");
if (embed && embed.type === "application/pdf") {
return true;
}
return false;
}
chrome.runtime.onMessage.addListener((request, _sender, sendResponse) => {
if (request.action === "getPageData") {
const text = document.body.innerText;
if (text) {
sendResponse({ text });
} else if (isPDFPage()) {
extractPDFText(location.href).then((text) => sendResponse({ text }));
return true;
}
}
});