-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
42 lines (37 loc) · 1.26 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
chrome.runtime.onInstalled.addListener(() => {
console.log("AI-Powered Content Summarizer installed");
});
const API_KEY = "YOUR API KEY"; // Replace this with your actual ApyHub API key
async function summarizeText(url, length) {
const apiUrl = "https://api.apyhub.com/ai/summarize-url";
try {
const response = await fetch(apiUrl, {
method: "POST",
headers: {
"apy-token": API_KEY, // Using the hardcoded API key
"Content-Type": "application/json",
},
body: JSON.stringify({
url: url,
summary_length: length, // "short", "medium", or "long"
}),
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(`API Error: ${errorData.message || response.statusText}`);
}
const data = await response.json();
return data.data.summary;
} catch (error) {
console.error("Error summarizing text:", error.message);
return "Error summarizing text. Please try again later.";
}
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "summarizePage") {
summarizeText(message.url, message.length).then((summary) => {
sendResponse({ summary });
});
return true; // Keeps the message channel open
}
});