-
Notifications
You must be signed in to change notification settings - Fork 0
/
content-script.js
39 lines (38 loc) · 1.03 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
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) => {
switch (request.action) {
case "getPageData":
const text = document.body.innerText;
if (text) {
sendResponse({ text });
} else if (isPDFPage()) {
// extractPDFText(location.href).then((text) => sendResponse({ text }));
chrome.runtime.sendMessage({ action: "getPdfText", url: location.href }, (response) => {
// console.log("PDF text", response.text);
sendResponse({ text: response.text });
});
return true;
}
break;
case "insertToPage":
const ele = document.activeElement;
if (ele) {
ele.value = request.text;
}
break;
default:
break;
}
});