-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
executable file
·64 lines (62 loc) · 3.02 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Start listening to messages coming from other scripts
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
// When we receive a message with the 'run_script' command
if (request.msg === 'run_script') {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
function: function() {
var title = document.querySelector('title')?.innerText || "";
var metaDescription = document.querySelector('meta[name="description"]')?.getAttribute('content') || "";
var canonicalUrl = document.querySelector('link[rel="canonical"]')?.getAttribute('href') || "";
var metaRobots = document.querySelector('meta[name="robots"]')?.getAttribute('content') || "";
var headers = Array.from(document.querySelectorAll('h1,h2,h3,h4,h5,h6')).map(header => {
return {
tag: header.tagName,
text: header.innerText
};
});
var bodyText = document.querySelector('body')?.innerText || "";
var wordCount = bodyText.split(/\s+/).length; // count the words in the body
return {
title: title,
metaDescription: metaDescription,
canonicalUrl: canonicalUrl,
metaRobots: metaRobots,
currentUrl: window.location.href,
headers: headers,
wordCount: wordCount
};
}
}).then((results) => {
// Handle the results from the page script
if (results && results.length > 0) {
// Fetch the robots.txt file
var url = new URL(results[0].result.currentUrl);
fetch(url.origin + '/robots.txt')
.then(response => {
if (response.ok) return response.text();
else throw new Error('No robots.txt found');
})
.then(text => {
results[0].result.robotsTxt = text;
sendResponse(results[0].result);
})
.catch(error => {
results[0].result.robotsTxt = null;
sendResponse(results[0].result);
});
} else {
sendResponse({
error: 'No results returned from script execution.'
});
}
}).catch((error) => {
sendResponse({
error: `Error executing script: ${error.message}`
});
});
});
return true; // This is required to use sendResponse asynchronously
}
});