-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-script.js
More file actions
43 lines (36 loc) · 1.11 KB
/
content-script.js
File metadata and controls
43 lines (36 loc) · 1.11 KB
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
//using iife so that the content is immediately injected into the page
;(() => {
chrome.runtime.onMessage.addListener((message, sendResponse) => {
if (message.action === "getPageContent") {
const content = extractPageContent();
sendResponse({ content: content });
}
return true;
})
function extractPageContent() {
const mainContentSelectors = [
"article",
"main",
".article-content",
".post-content",
".entry-content",
"#content",
".content",
]
for (const selector of mainContentSelectors) {
const element = document.querySelector(selector)
if (element && element.textContent.trim().length > 100) {
return element.textContent.trim();
}
}
const paragraphs = document.querySelectorAll("p");
if (paragraphs.length > 0) {
return Array.from(paragraphs)
.map((p) => p.textContent.trim())
.filter((text) => text.length > 20)
.join("\n\n")
}
//else
return document.body.innerText
}
})()