-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_script.js
More file actions
41 lines (39 loc) · 1.5 KB
/
Copy pathcontent_script.js
File metadata and controls
41 lines (39 loc) · 1.5 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
// This script is injected into the page to extract its main content.
// We check if Readability is available. If not, this script was likely injected
// without its dependency, which is an error condition we can't recover from.
if (typeof Readability === 'undefined') {
// eslint-disable-next-line no-alert
alert('Error: Readability library not found. The extension cannot proceed.');
// In a real-world scenario, you'd want more robust error handling,
// but for this example, an alert is sufficient to indicate a problem.
} else {
/*
* We are cloning the document element because Readability.js can modify
* the DOM, and we don't want to break the original page.
*/
const documentClone = document.cloneNode(true);
const reader = new Readability(documentClone);
const article = reader.parse();
/*
* The 'article' object contains the extracted content.
* - title: The article title
* - content: An HTML string of the main content
* - textContent: A string of the main content, stripped of HTML
* - length: The length of the text content
*
* We'll send the most useful parts back to our background script.
*/
if (article) {
chrome.runtime.sendMessage({
type: 'CONTENT_PARSED',
payload: {
title: article.title,
textContent: article.textContent,
},
});
} else {
chrome.runtime.sendMessage({
type: 'PARSING_FAILED'
});
}
}