-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
42 lines (40 loc) · 1.73 KB
/
background.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
// Listen for messages from other parts of the extension
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
// Check if the received message is requesting page content
if (request.action === "getPageContent") {
// Query for the active tab in the current window
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
// Execute a script in the active tab to get page content
chrome.scripting.executeScript({
target: {tabId: tabs[0].id},
function: getPageContent,
}, (results) => {
// Check for any errors during script execution
if (chrome.runtime.lastError) {
sendResponse({error: chrome.runtime.lastError.message});
} else {
// Send back the results of the script execution
sendResponse(results[0].result);
}
});
});
// Return true to indicate that the response will be sent asynchronously
return true;
}
});
// Function to extract content from the active page
function getPageContent() {
// Get the visible text content of the page
let visibleText = document.body.innerText;
// Get the meta description if available
let metaDescription = document.querySelector('meta[name="description"]');
metaDescription = metaDescription ? metaDescription.getAttribute('content') : '';
// Get the page title
let title = document.title;
// Return an object with the extracted content
return {
title: title,
metaDescription: metaDescription,
visibleText: visibleText.substring(0, 5000) // Limit text to 5000 characters
};
}